Click here to Skip to main content
15,924,402 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Iam try to filter but query not working


private void btnFilter_Click(object sender, EventArgs e)
       {

           if (dtpFrom.Value.ToShortDateString() != "" && dtpTo.Value.ToShortDateString() != "" && comboBoxse.SelectedValue.ToString() != "")
           {
               sWhere = "Where  DocDate Between '" + dtpFrom.Text + "' AND '" + dtpTo.Text + "' AND '" +  " FirstName ='" + comboBoxse.SelectedValue.ToString()+ "'";
           }

           SqlDataAdapter objAdapter = new SqlDataAdapter(@"Select distinct DocEntry,SeriesName,DocNum,Docdate,U_C_Rcpt,TrnspName,CardCode,CardName,CustEMail,SlpName,SalesEmpEMail,FirstName,LastName,ExecMail  from  SAPSALES.ORDERSTATUS " + sWhere + "", SettingManger.Instance.Conn);
           DataTable objTable = new DataTable();
           objAdapter.Fill(objTable);

           dataGridView1.DataSource = objTable;
           dataGridView1.Columns[0].Width = 25;
           for (int i = 1; i < dataGridView1.Columns.Count; i++)
           {
               dataGridView1.Columns[i].ReadOnly = true;

           }





       }

Posted
Updated 22-Jan-14 21:57pm
v2

change this, one quote is extra...

C#
"' AND '" +

C#
" AND '" +


+ "' AND '" + " FirstName ='" +

Note: be aware of SQL_injection[^]

Its always safe to use sql parameterized query
how-do-i-create-a-parameterized-sql-query-why-should-i[^]
 
Share this answer
 
v4
Comments
Christian Graus 23-Jan-14 3:59am    
Yes, but it will still be a very stupid way to do things....
Master Vinu 23-Jan-14 4:02am    
ya i have removed but gives error:

Incorrect syntax near 'MOHAN'.
Unclosed quotation mark after the character string ''.
Karthik_Mahalingam 23-Jan-14 4:03am    
keep a break point on this line DataTable objTable = new DataTable(); and check what is the string on sWhere ???
Master Vinu 23-Jan-14 4:06am    
it gives:
Where DocDate Between '1/22/2014' AND '1/23/2014 FirstName ='MOHAN'
Karthik_Mahalingam 23-Jan-14 4:09am    
use this

sWhere = "Where DocDate Between '" + dtpFrom.Text + "' AND '" + dtpTo.Text + "' AND " + " FirstName ='" + comboBoxse.SelectedValue.ToString() + "'";
Don't do that! Why convert DateTime values to a (locale specific) string format, concatenate these to form an SQL command, and then send them to SQL to be converted back to DateTime values?

For that matter, why are you assuming that a DateTimePicker.Value property can ever generate a empty string?

Use a parameterized query, and pass the DateTime value in directly.
You will probably find your problem disappears...
 
Share this answer
 
This is horrible code. BURN it. I can erase your database if I can browse to this web page. Read up on SQL injection attacks.

Instead, write a proc that does this

where (@from is null or docdate > @from) and (@to is null or docdate < @to) and (@firstName is null or FirstName = @FirstName)


That's how you create optional parameters in SQL.

Once you do things properly, it will just start working, I am sure. I do recommend learning to use your debugger though. If you were to intercept the generated SQL and run it in your Management Studio, I bet you'd find out why it's not working as you expect.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900