I am not able to find what's wrong with the following code. There is a form on which the selected start date and finish date , as well as a button transition to a new form where DataGridView populated the database , it is necessary to conclusions regarding the selected date , but that he does not want to issue ORA- 00933.No "where" all is well.
void renewOtchet()
{
dgvOtchet.Rows.Clear();
OracleCommand ocm = Oracle.DBConneciton.CreateCommand();
ocm.CommandText = "select num, date_start, date_finish, " +
"trim(name_video), price_video, " +
"trim(fam_client), " +
"trim(kind_zal) from video.allData" +
"where date_start >= ('"+ date1 + "', 'DD.MM.YYYY' ) and date_finish <= ('" + date2+ "', 'DD.MM.YYYY') ";
string[] str = new string[7];
OracleDataReader ord = ocm.ExecuteReader();
while (ord.Read())
{
for (int i = 0; i < 7; i++)
{
str[i] = ord[i].ToString();
if (i == 1 || i == 2)
{
str[i] = str[i].Remove(10);
}
}
dgvOtchet.Rows.Add(str);
}
ord.Dispose();
ord.Close();
}
You can try this.
"select num, date_start, date_finish, " +
"trim(name_video), price_video, " + "trim(fam_client), " +
"trim(kind_zal) from video.allData " +
"where date_start >= TO_DATE('"+ date1 + "', 'DD.MM.YYYY' )
and date_finish <= TO_DATE('" + date2+ "','DD.MM.YYYY') ";
string[] str = new string[7];
Related
My C# application is significantly slower than I would like. The program accesses an Excel sheet and loops through each row on a sheet / does the following:
Collects variables from that row
Creates an SQL query based off those variables
Executes that query
then a reader goes out and puts it in its proper column on that same row.
*Note, each row has 6 different SQL queries/ final numbers that are calculated and input into the sheet, the code below is just showing the first 2 for brevity's sake. The sheet has around 300 rows, so the program is executing 300 * 6= 1,800 SQL queries each time its run. For each one of those 1,800 numbers, the program is accessing the sheet and inputting it into the sheet.
Instead of doing the excelWorksheet.get_Range and inputting the number into the Excel sheet for each number, one at a time, would it be faster to store each number into an array and then go back later and mass dump all of the numbers into the sheet, once all of the queries have been executed and the array is full?
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "Data Source=WRDXPVSEPIRPT00;DATABASE=Epicor905;Workstation ID=SMEBPPL204TN;Trusted_Connection=true";
try
{
//initiate the connection
conn.Open();
}
catch(SqlException ex)
{
throw new ApplicationException(string.Format("You're not connected to the database, please close the program, re-connect to the network, and try again."), ex);
}
progressBar1.Value = 60;
statusLabel.Text = "Retrieving account balances from database...";
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//iterate through each row and pull information
for (int i = 2; i < lastUsedRow + 1; i++)
{
//string iString = Convert.ToString(i);
statusLabel.Text = "Retrieving balances for " + i + " of " + lastUsedRow + " accounts...";
//declare excel sheet range variables
var CompanyVar = excelWorksheet.get_Range("A" + i, "A" + i).Text;
var FiscalYear = excelWorksheet.get_Range("B" + i, "B" + i).Text;
var FiscalPeriod = excelWorksheet.get_Range("C" + i, "C" + i).Text;
var segValue1 = excelWorksheet.get_Range("F" + i, "F" + i).Text;
var segValue2 = excelWorksheet.get_Range("G" + i, "G" + i).Text;
var segValue3 = excelWorksheet.get_Range("H" + i, "H" + i).Text;
int FiscalYearHelper = Convert.ToInt32(FiscalYear);
var FiscalYearPYY = FiscalYearHelper - 1;
//begin filling in CY YTD column
string SQLCode = "SELECT SUM(DebitAmount-CreditAmount) as BalanceAmt FROM GLJrnDtl WITH (NOLOCK) WHERE" +
" FiscalPeriod between 1 AND " + FiscalPeriod + "AND Company =" + "'" + CompanyVar + "'" +
"AND FiscalYear =" + "'" + FiscalYear + "'" + "AND SegValue1 LIKE " + "'" + segValue1 + "'" +
"AND SegValue2 LIKE " + "'" + segValue2 + "'" + "AND SegValue3 LIKE " + "'" + segValue3 + "'";
SqlCommand command = new SqlCommand(SQLCode, conn);
// Create new SqlDataReader object and read data from the command.
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
string cyAct = reader["BalanceAmt"].ToString();
if (cyAct == "")
{
goto CYM;
//cyAct = "0.00";
}
var cyAct1 = (float)Convert.ToDouble(cyAct);
int one = Convert.ToInt32(excelWorksheet.get_Range("E" + i, "E" + i).Text);
double cyAct2 = (cyAct1 * one);
string cyAct3 = cyAct2.ToString("0.##");
excelWorksheet.get_Range("I" + i, "I" + i).Value = cyAct3;
}
}
//end filling in column
//begin filling in CY Month column
CYM:
string SQLCodeMonth = "SELECT SUM(DebitAmount-CreditAmount) as BalanceAmt FROM GLJrnDtl WITH (NOLOCK) WHERE" +
" FiscalPeriod = " + FiscalPeriod + "AND Company =" + "'" + CompanyVar + "'" +
"AND FiscalYear =" + "'" + FiscalYear + "'" + "AND SegValue1 LIKE " + "'" + segValue1 + "'" +
"AND SegValue2 LIKE " + "'" + segValue2 + "'" + "AND SegValue3 LIKE " + "'" + segValue3 + "'";
SqlCommand commandMonth = new SqlCommand(SQLCodeMonth, conn);
// Create new SqlDataReader object and read data from the command.
using (SqlDataReader reader = commandMonth.ExecuteReader())
{
while (reader.Read())
{
string cyAct = reader["BalanceAmt"].ToString();
if (cyAct == "")
{
goto APY;
//cyAct = "0.00";
}
var cyAct1 = (float)Convert.ToDouble(cyAct);
int one = Convert.ToInt32(excelWorksheet.get_Range("E" + i, "E" + i).Text);
double cyAct2 = (cyAct1 * one);
string cyAct3 = cyAct2.ToString("0.##");
excelWorksheet.get_Range("J" + i, "J" + i).Value = cyAct3;
}
}
//end filling in column
//begin filling in Act PY month column
THIS IS NOT A FULL ANSWER, there is not enough space to write this as a comment, do not mark this as an answer its a comment.
Please try to use objects (instances of classes or structs) it will make your programming more efficient and easy to maintain.
for example:
private void UseObjects()
{
List<ExcelObjects> ListOfvarsForQuery = new List<ExcelObjects>();
for (int i = 2; i < lastUsedRow + 1; i++)
{
ExcelObjects obj = new ExcelObjects();
obj.CompanyVar = ws.Cells[i, 1];
obj.FiscalYear = ws.Cells[i, 2];
obj.FiscalPeriod = ws.Cells[i, 3];
obj.segValue1 = ws.Cells[i, 4];
obj.segValue2 = ws.Cells[i, 5];
obj.segValue3 = ws.Cells[i, 6];
ListOfvarsForQuery.Add(obj);
}
string SQLCode = // use the list of ExcelObjects and write a better query.
}
}
struct ExcelObjects
{
public string CompanyVar;
public string FiscalYear;
public string FiscalPeriod;
public string segValue1;
public string segValue2;
public string segValue3;
}
Instead of looping through each row and running a SQL query for each row, I found that it is faster to outer join the table I speak of in my question with another SQL table that has the account balances on it. I'm doing all of it within memory because I've been coding for 3 weeks and I don't have time to learn how to create a temp table in SQL server yet.
I have a small problem regarding the Access Database when it has data inserted.
I have a button that inserts data when it's clicked. Here it is the code:
string[] read = File.ReadAllLines("Harta_Distantelor.txt");
string[] orase = { "Constanta", "Varna", "Burgas", "Istambul", "Kozlu", "Samsun", "Batumi", "Sokhumi", "Soci", "Anapa", "Yalta", "Sevastopol", "Odessa" };
OleDbCommand cmd;
for (int i = 0; i < read.Length; i++)
{
int j = 0;
string[] numbers = read[i].Split(' ');
while (j < read.Length)
{
//cmd = new OleDbCommand("UPDATE Distante SET ID_Port='" + (i + 1) + "', ID_Port_Destinatie='" + (j + 1) + "', Nume_Port_Destinatie='" + orase[j] + "' WHERE Distanta='" + numbers[j] + "' ", conn);
cmd = new OleDbCommand("INSERT INTO [Distante]([ID_Port], [ID_Port_Destinatie], [Nume_Port_Destinatie], [Distanta]) VALUES ('" + (i + 1).ToString() + "', '" + (j + 1).ToString() + "', '" + orase[j] + "', '" + numbers[j] + "')", conn);
cmd.ExecuteNonQuery();
j++;
}
}
The problem is that instead of having something like ID_Port=1, ID_Port_Destinatie=1, Nume_Port_Destinatie="Constanta", ID_Port=1, ID_Port_Destinatie=2, Nume_Port_Destinatie="Varna" and so on, I get this...
https://i.stack.imgur.com/OjMQm.png
The file "Harta_Distantelor.txt" is a matrix that has the distances, so that's why I tried to split every number from a line(string[] numbers = read[i].Split(' ');). I have to mention that [Distanta] data is correctly inserted, but the rest of the data is totally randomly inserted. I tried to figure out where the problem is, but I have no clue. Where is the mistake? What should I try to do in order to have the data showed correctly?
1.) Create a table called HartaDistantelor with columns for ID_Port, ID_Port_Destinatie, and Distanta
2.) Fill the table HartaDistantelor with your matrix values. You may be able to copy and paste directly into Access
3.) Create a table called Orase with columns for ID_Port and Nume_Port
4.) Fill the table Orase with the 13 values from your array. It should look like this:
1 Constanta
2 Varna
3 Burgas
4 ...
5.) Change your code:
OleDbCommand cmd;
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cmd = new OleDbCommand("INSERT INTO Distante (ID_Port, ID_Port_Destinatie, Nume_Port_Destinatie, Distanta) SELECT " + (i + 1).ToString() + ", " + (j + 1).ToString() + ", o.Nume_Port, h.Distanta FROM Orase o JOIN HartaDistantelor h ON h.ID_Port = o.ID_Port WHERE h.ID_Port = i AND h.ID_Port_Destinatie = j", conn);
cmd.ExecuteNonQuery();
}
}
My approach joins records from two tables using ID values that match. I don't think you made any mistake, but used a method with more complexity than was necessary.
This question already has answers here:
What is the best way to modify a list in a 'foreach' loop?
(11 answers)
Closed 6 years ago.
So i got this errorin the word in "in" in the foreach cycle and i surfed the net but i didnt found any answers or understood it, here is the code.
foreach (string items in lstitems.Items)
{
connection.Open();
OleDbCommand comando = new OleDbCommand();
comando.Connection = connection;
string[] caracteresnastring = items.Split(new char[] { ',' }).ToArray();
string codproduto = caracteresnastring[0];
string nome = caracteresnastring[1];
int quantidade = Convert.ToInt32(caracteresnastring[2]);
decimal preco = Convert.ToDecimal(caracteresnastring[3]);
int tamanho = Convert.ToInt32(caracteresnastring[4]);
string total = caracteresnastring[5];
lstitems.Items.Add(txtcodproduto.Text + "," + txtnomeprod.Text + "," + txtquantidade.Text + "," + txtpreco.Text + "," + txttamanho.Text + "," + total);
comando.CommandText = "INSERT INTO detalhes_encomendas_fornecedores (cod_encomenda_forn, cod_produto,quantidade,tamanho, total) VALUES('" + codencomendaforn + "','" + codproduto + "', '" + quantidade + "', '" + tamanho + "', '" + total + "'); ";
comando.ExecuteNonQuery();
connection.Close();
}
You can't modify a List while you're using a foreach to iterate over it.
Instead, you can use a normal for loop.
Example:
foreach(int item in mylist)
{
if (item % 2 == 0)
mylist.Remove(item); // Error here
Console.WriteLine(item);
}
But you can:
for(int i = 0; i < mylist.Count; i++)
{
if (mylist[i] % 2 == 0)
{
mylist.RemoveAt(i);
i--;
continue;
}
Console.WriteLine(mylist[i]);
}
I am trying to filter multiple string names on one datagridview. I am appending the filter string like so
filter = " (DateTime >= #" + startDateTime.ToString("MM/dd/yyyy HH:mm:ss") + "# And DateTime <= #"
+ endDateTime.ToString("MM/dd/yyyy HH:mm:ss") + "#";
for (int i = 0; i < productNumber.Count; i++)
{
filter += " And TagIndex ='" + productNumber[i] + "'";
}
filter += " )";
where productNumber is a List with the ID tags in it in string format. The output is something like this:
(DateTime >= #11/02/2014 01:00:58# And DateTime <= #12/01/2014 00:00:00# And TagIndex ='0' And TagIndex ='1' And TagIndex ='2' )
I'm getting a Syntax Error when I try to pass this a the filter for the datagridview.
Maybe the problem is that your filter is not returning anything. Try changing the last ANDs with ORs so your filter will look like this (DateTime >= #11/02/2014 01:00:58# And DateTime <= #12/01/2014 00:00:00# And TagIndex ='0' Or TagIndex ='1' Or TagIndex ='2')
I had to initialize filters for every instance of the tagname. The code is as follows:
for (int oh = 0; oh < productIndexes.Count; oh++)
{
if (oh == 0)
{
filter += "(DateTime >= #" + startDateTime.ToString("MM/dd/yyyy HH:mm:ss") + "# And DateTime <= #"
+ endDateTime.ToString("MM/dd/yyyy HH:mm:ss") + "#)" + " And (TagIndex = '" + productIndexes[oh] + "'";
indexFilter = "(;Tagname = '" + productIndexes[oh] + "'";
}
else
{
filter += " And (DateTime >= #" + startDateTime.ToString("MM/dd/yyyy HH:mm:ss") + "# And DateTime <= #"
+ endDateTime.ToString("MM/dd/yyyy HH:mm:ss") + "#)" + " Or TagIndex ='" + productIndexes[oh] + "'";
indexFilter += " Or ;Tagname = '" + productIndexes[oh] + "'";
}
}
filter += " )";
indexFilter += " )";
Console.WriteLine(filter);
Console.WriteLine(indexFilter);
forgive the poor naming convention for the for loop.
I have advanced search in my matrimony college project.
I had following query as a result:
select *
from tbl_FreeUserProfile
where Gender='Male' and Age>= '18'
and Age<= '40' and heightf<='182'
and heightf>='152' and MaritalStatus='Never Married'
and MotherTongue = 'xyz' or 'Gujarati' or 'Urdu' or 'Hindi'"
But when I execute the query it shows the following error:
An expression of non-boolean type specified in a context where a condition is expected, near 'or'
Can anyone tell me what is wrong in the format of the sql query?
My coding is like this
protected void ImageButton11_Click(object sender, ImageClickEventArgs e)
{
string sql = "select * from tbl_FreeUserProfile where Gender='" + RadioButtonList2.SelectedItem.Text + "' and Age>= '" + DropDownList25.SelectedItem.Text + "' and Age<= '" + DropDownList26.SelectedItem.Text + "' and heightf<='" + TextBox23.Text + "' and heightf>='" + TextBox22.Text + "' and MaritalStatus='" + DropDownList35.SelectedItem.Text + "'";
if (ListBox2.Items.Count >= 0)
{
sql = sql + " and MotherTongue = 'xyz'";
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
{
string mt = ListBox2.Items[i].ToString();
sql = sql + " or '" + mt + "'";
}
}
I think you wanted to do something like
and MotherTongue IN ('xyz', 'Gujarati', 'Urdu', 'Hindi')
If you want to use or you'll have to specify the field name each time.
You could do this, using
AND (MotherTongue = 'xyz' OR MotherTongue = 'abc' ...)
changing your code to
if (ListBox2.Items.Count >= 0)
{
sql = sql + " and ( MotherTongue = 'xyz'";
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
{
string mt = ListBox2.Items[i].ToString();
sql = sql + " or MotherTongue = '" + mt + "'";
}
sql = sql + ")"
}
or
AND MotherTongue IN ('xyz', 'abc', ...)
changing your code to
if (ListBox2.Items.Count >= 0)
{
sql = sql + " and MotherTongue IN (" +
string.Join(",", ListBox2.Items[i].Select(i => i.ToString())) + ")";
}
Currently the query being executed ends with:
and MotherTongue = 'xyz' or 'Gujarati' or 'Urdu'... so on.
The actual query should be:
and MotherTongue = 'xyz' or MotherTongue = 'Gujarati' or MotherTongue = 'Urdu'
The best way to query multiple languages would be as C.Evenhuis has mentioned i.e. use:
and MotherTongue IN ('xyz', 'Gujarati', 'Urdu', 'Hindi').