Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlConnection con = new SqlConnection("Server=(local);initial catalog=ServiceTax;Trusted_Connection=True");


private void button1_Click(object sender, EventArgs e)
{
      con.Open();
      SqlCommand cmd = new SqlCommand("insert into RSBIlls Values(" + cb_sno.Text + ",'" + cb_billno.Text + "','" + dateTimePicker1.Text + "','" + cb_name.Text + "','" + txt_Area.Text + "','" + txt_street.Text + "','" + txt_location.Text + "'," + textBox1.Text + "," + txt_dieselservice.Text + "," + txt_subtotal.Text + "," + txt_servicetax.Text + "," + txt_educess.Text + "," + txt_addcess.Text + "," + txt_grandtot.Text + ")", con);
 cmd.ExecuteNonQuery();
      con.Close();
}


When i run and click the insert button error shows as Incorrect syntax near ','.
Posted
Updated 5-Nov-12 23:15pm
v2
Comments
Nandakishore G N 6-Nov-12 8:41am    
one suggestion is dont use inline insert...better use stored procedure...there u can find problem easily..

My suggestion:
C#
// ...
String s = "insert into RSBIlls Values(" + cb_sno.Text + ",'" + cb_billno.Text + "','" + dateTimePicker1.Text + "','" + cb_name.Text + "','" + txt_Area.Text + "','" + txt_street.Text + "','" + txt_location.Text + "'," + textBox1.Text + "," + txt_dieselservice.Text + "," + txt_subtotal.Text + "," + txt_servicetax.Text + "," + txt_educess.Text + "," + txt_addcess.Text + "," + txt_grandtot.Text + ")";
Debug.WriteLine(s); // this is the line that will help you.
SqlCommand cmd = new SqlCommand(s, con);
// ...

Take the contents of the debug screen, run in Management Studio, and see the errors there.
My guess: either txtLocation.Text is something like 'Manchester, England' or one of the others is empty.
Also, don't forget to add single quotes around literal strings ('this is a string', this_is_a_database_object_name).
Tell me if the spirits were right...
:)
Hope this helps,
Pablo.
 
Share this answer
 
v2
hi dear
try below query

SQL
SqlCommand cmd = new SqlCommand("insert into RSBIlls
([ListYourFieldNameHere]) 
Values('" + cb_sno.Text + "'
,'" + cb_billno.Text + "'
,'" + dateTimePicker1.Text + "'
,'" + cb_name.Text + "'
,'" + txt_Area.Text + "'
,'" + txt_street.Text + "'
,'" + txt_location.Text + "'
,'" + textBox1.Text + "'
,'" + txt_dieselservice.Text + "'
,'" + txt_subtotal.Text + "'
,'" + txt_servicetax.Text + "'
,'" + txt_educess.Text + "'
,'" + txt_addcess.Text + "'
,'" + txt_grandtot.Text + "')", con);
 
Share this answer
 
Try this
C#
private void button1_Click(object sender, EventArgs e)
{
string strSqlQuery="insert into RSBIlls Values(" + cb_sno.Text + ",'" + cb_billno.Text + "','" + dateTimePicker1.Text + "','" + cb_name.Text + "','" + txt_Area.Text + "','" + txt_street.Text + "','" + txt_location.Text + "'," + textBox1.Text + "," + txt_dieselservice.Text + "," + txt_subtotal.Text + "," + txt_servicetax.Text + "," + txt_educess.Text + "," + txt_addcess.Text + "," + txt_grandtot.Text + ")";
Response.Write("Your Query:"+ strQuery);
//con.Open();
//SqlCommand cmd = new SqlCommand(", con);
//cmd.ExecuteNonQuery();
//con.Close();
}

Through you can view your SqlQuery than check the error.
Hope this helps.
 
Share this answer
 
Get the query part and run it in a query analyzer against your database. You will get the issue.
Most probably some of your parameter is not correct and causing a syntactical error.
 
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