Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i pass date in txtdate1 it give me the error
and when i pass date in both textbox then it give me the error "Incorrect syntax near 'between'
C#
string search = "SELECT Id,Category,Subject,Date FROM News ";
if (txtdate1.Text != "" && txtdate2.Text != "")
{
    search = search + "Where Date between '" + date1 + "' and '" + date2 + "'";
}
else if (txtdate1.Text != "" && txtdate2.Text == "")
{
    search = search + "Where Date = '" + date1 +  "'";
}
else if (txtdate2.Text != "")
{
    search = search + "Where Date = '" + date2 + "'";
}
cmd.CommandText = search;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
gvImages.DataSource = ds;
gvImages.DataBind();
Posted
Updated 26-Jan-15 13:48pm
v5
Comments
Wonde Tadesse 26-Jan-15 19:58pm    
Your query syntax was having some issue with spacing and it's fixed. Should work as expected.
Harshit Wadhera 26-Jan-15 20:11pm    
i had just write the where clause at wrong place and now its working but i have another problem that is
i had write
string date1 = Request.Form[txtdate1.UniqueID];
string.Format("dd/MM/yyyy", date1);
and it is giving me error that "Conversion failed when converting date and/or time from character string."
may you help me
Wonde Tadesse 26-Jan-15 20:19pm    
Read this article. http://www.aspsnippets.com/Articles/Validate-date-string-in-ddMMyyyy-format-in-ASPNet.aspx. It will help you for this issue
Santosh K. Tripathi 26-Jan-15 23:14pm    
Hi,
Could you tell, what values are you passing in txtdate1 and txtdate2?

The problem originates from the fact that you're not using parameters. You should never concatenate values directly to the SQL statement but use parameters instead. Concatenating values introduces this kind of conversion problems but also leaves you wide open to SQL injections (see SQL Injection)[^]

For more information see SqlParameter Class[^]
 
Share this answer
 
First thing you should do is Parse the Text from the textboxes with DateTime before trying to but in you SQL query. Or even better use DatePickers which will give you a DateTime object.
Secondly, fields should never be added in-line to your query because of user errors and injection attacks. They should be added as parameters and make sure they are the correct SQL data type.

The SQL is query looks like you're using MySQL. Isn't apostrophe only needed for strings?
 
Share this answer
 
Comments
Harshit Wadhera 26-Jan-15 20:12pm    
yeah i have written like
string date1 = Request.Form[txtdate1.UniqueID];
string.Format("dd/MM/yyyy", date1);
but
it is giving me error that "Conversion failed when converting date and/or time from character string."
may you help me
thanx in advance

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