Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
con.Open();
     SqlCommand cmd2 = new SqlCommand("Update diesel set SNO = '" + cb_sno.Text + "',Date = '" + dateTimePicker1.Text + "',Dieselcost = " + textBox1.Text + ",Dieselservice=" + txt_dieselservice.Text + ",Servicetax= " + txt_servicetax.Text + ",Educess = " + txt_educess.Text + ",Addcess = " + txt_addcess.Text + ",Sertax= " + txt_sertaxtot.Text + ",Totbill= " + txt_totbillvalue.Text + ",Flag='" + checkedListBox1.Text + "' where SNO =  " + cb_sno.Text + " and Flag ="TRUE"", con);
   cmd2.ExecuteNonQuery();
   con.Close();

When I inserting data error occurs.

please help me.

from the above what is the problem.
Posted
Updated 18-Nov-12 22:25pm
v2
Comments
Bitla Phanindra 19-Nov-12 4:17am    
Check if the datatypes of the table's coulmns are matching in the query. If not, use proper Convert statements accordingly.
MT_ 19-Nov-12 4:17am    
You can display in an alert cmd2.Tostring() and try to run the queyr in the SSMS, that will give clue. Mostly one extra or missing ' - Milind
Bitla Phanindra 19-Nov-12 4:18am    
What error message do u get when you run this??

It could be anything, starting from a space character in one of your text boxes on up.
Please don't do it like that - you leave yourself wide open to an SQL Injection attack as well as to problems like this. Use a parametrized query instead:
C#
SqlCommand cmd2 = new SqlCommand("Update diesel set SNO = @SNO,Date = @DAT,...", con);
cmd2.Parameters.AddWithValue("@SNO", cb_sno.Text);
cmd2.Parameters.AddWithValue("@DAT", dateTimePicker1.Value);
...
cmd2.ExecuteNonQuery();
It also makes the code a whole lot more readable.
 
Share this answer
 
Comments
Abhishek Pant 19-Nov-12 4:29am    
My 5! Thats the way we declare.I don't what he has done above in question! and how he his accepting values.
Hi,

See the code snippet "and Flag = "TRUE""

Here if you want to use double quote then you should use like \" other wise it will treat as an extra string.
 
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