Click here to Skip to main content
11,925,292 members (57,611 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
     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);
When I inserting data error occurs.

please help me.

from the above what is the problem.
Posted 18-Nov-12 23:13pm
Edited 18-Nov-12 23:25pm
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.
Milind Thakkar 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??
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:
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);
It also makes the code a whole lot more readable.
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100