Click here to Skip to main content
15,921,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
con.Open();
          string query = "insert into staff_doc values (" + textBox1.Text + " , '" + textBox2.Text + "' , '" + textBox3.Text + "' , '" + textBox4.Text + "' , '" + comboBox1.Text + "' , '" + comboBox2.Text + "' , '" + comboBox3.Text + "' , '" + textBox5.Text + "' , '" + textBox6.Text + "' , '" + textBox7.Text + "' , '" + textBox8.Text + "' , '" + textBox9.Text + "')";
          cmd = new SqlCommand(query, con);
          cmd.ExecuteNonQuery();
          MessageBox.Show("Data Added");
          clear();
          cmd.Dispose();
          con.Close();

      }
      public void clear()
      {
          textBox1.Text = "";
          textBox2.Text = "";
          textBox3.Text = "";
          textBox4.Text = "";
          textBox5.Text = "";
          textBox6.Text = "";
          textBox7.Text = "";
          textBox8.Text = "";
          textBox9.Text = "";

      }










while submitting
JavaScript
Incorrect syntax near ','.


What I have tried:

I tried at my knowledge level but i cant rectified it.
Posted
Updated 28-Aug-16 0:55am
Comments
Member 12245539 27-Aug-16 6:12am    
Send your SQL_Table querry of all columns...
Richard MacCutchan 28-Aug-16 8:04am    
It's still not possible to know what is wrong. And you are never going to fix this unless you use SQL correctly, by using SQL parameters and not concatenated strings.

Always use SQLParameters to pass values to the sql statement, dont concatenate the strings. It will lead to SQL Injection[^]

C#
con.Open();
          string query = "insert into staff_doc values (@value1,@value2,@value3,@value4,@value5,@value6,@value7,@value8,@value9,@value10,@value11,@value12)";
          cmd = new SqlCommand(query, con);
          cmd.Parameters.Add("@value1", textBox1.Text);
          cmd.Parameters.Add("@value2", textBox2.Text);
          cmd.Parameters.Add("@value3", textBox3.Text);
          cmd.Parameters.Add("@value4", textBox4.Text);
          cmd.Parameters.Add("@value5", comboBox1.Text);
          cmd.Parameters.Add("@value6", comboBox2.Text);
          cmd.Parameters.Add("@value7", comboBox3.Text);
          cmd.Parameters.Add("@value8", textBox5.Text);
          cmd.Parameters.Add("@value9", textBox6.Text);
          cmd.Parameters.Add("@value10", textBox7.Text);
          cmd.Parameters.Add("@value11", textBox8.Text);
          cmd.Parameters.Add("@value12", textBox9.Text);
 
Share this answer
 
Comments
GKRISH04 27-Aug-16 2:40am    
Same error is coming on
Karthik_Mahalingam 27-Aug-16 2:42am    
try this
insert into staff_doc(column1name,column2name,.......,column12name) values (@value1,@value2,@value3,@value4,@value5,@value6,@value7,@value8,@value9,@value10,@value11,@value12)"
It is not possible to tell why you get this error because you didn't gave the database structure and the values of the textboxes/comboboxes.

By building the request the way you do, the validity depend on the value of the textboxes/comboboxes. if one contain a ', it is enough to ensure an error and it can be worst if the value is crafted for an injection.
SQL Injection[^]

It is always recommended to use parameters as described by Karthik.
 
Share this answer
 
if you encounter that error message, some variables is empty. Empty variables is cause to come side by side commas. You have to use the SqlCommand Parameters. Either you should before check correct value in variables.
 
Share this answer
 
this error appeared when you add string value for integer field in database table.

for example:

create table emp
(
integer emp_id primary key,
varchar2(30) ename
);

when you try to add values from textboxes to your database table do below:

str query = "insert into emp values("+textbox1.text",'"+textbox2.text"')";

invalid insertion as below

str query = "insert into emp values('"+textbox1.text"','"+textbox2.text"')";
 
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