Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
c1.con.Open();
string k = "NO";
DateTime start = DateTime.ParseExact(TextBox10.Text, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
DateTime end = DateTime.ParseExact(TextBox11.Text, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
c1.cmd.CommandText = "update" + Session["no"].ToString() + "set avail='" + k + "'where date between'" + start + "'and'" + end + "'";
ab = c1.cmd.ExecuteReader();
Label5.Visible = true;
c1.con.Close();

gives an error incorrect syntax near "="
Posted
Updated 10-Apr-14 22:56pm
v3
Comments
CHill60 10-Apr-14 17:37pm    
On which line? the commandtext?
[no name] 10-Apr-14 17:42pm    
Sure.... seen this same code several times in the past few days in homework assignments. You are missing a space in your where, (k + "' where)

It is not a good idea to concatenate string together to form queries. That leaves you susceptible to SQL Injection. You should be using parameterized queries. Also, your syntax is not spaced correctly for your query.

Use something like this:
c1.cmd.CommandText = "UPDATE tablename SET avail=@k WHERE date between @start and  @end";
c1.cmd.Parameters.AddWithValue("@k","NO");
c1.cmd.Parameters.AddWithValue("@start",DateTime.ParseExact(TextBox10.Text, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture));
c1.cmd.Parameters.AddWithValue("@end",DateTime.ParseExact(TextBox11.Text, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture));


The "tableName" should be replaced with the name of the table you want to updated. If the table is dynamic as well, then you can use the initial method you tried with the tablename I suppose.
 
Share this answer
 
v2
Comments
Aarti Meswania 11-Apr-14 4:25am    
5+! :)
Richard C Bishop 11-Apr-14 9:26am    
Thank you Aarti!
Aarti Meswania 13-Apr-14 2:48am    
welcome :)
Just debug that and take a look at c1.cmd.CommandText: you should immediately see that that is not a valid SQL query. There are no spaces between inidvidual parts of the query, it is something like
updatesession42set avail='NO'where date between'2014-04-08 00:00:00'and'2014-04-10 00:00:00'
Richard's answer shows you an excellent way for using SQL queries from code - but also there make sure that there are spaces!
 
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