Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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 18-Nov-12 23:13pm
Edited 18-Nov-12 23:25pm
v2
Comments
Bitla Phanindra at 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 at 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 at 19-Nov-12 4:18am
   
What error message do u get when you run this??
Rate this: bad
good
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);
...
cmd2.ExecuteNonQuery();
It also makes the code a whole lot more readable.
  Permalink  
Comments
Abhishek Pant at 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
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,488
1 OriginalGriff 6,516
2 Peter Leow 3,647
3 Zoltán Zörgő 3,416
4 Richard MacCutchan 2,397


Advertise | Privacy | Mobile
Web02 | 2.8.150123.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