Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
my code is...........
C#
{
    SqlConnection con = new SqlConnection("Data Source=RAMESH-PC;Initial Catalog=edburg;Integrated Security=True;Pooling=False");
    con.Open();
    SqlCommand cm = new SqlCommand("insert into table(name,num,sal,phone)  values('" + textBox1.Text + "'," + textBox2.Text + "," + textBox3.Text + "," + textBox4.Text + ")", con);
    MessageBox.Show(cm.ExecuteNonQuery().ToString());
    con.Close();
}

ERROR:Incorrect syntax near the keyword 'table'.
Posted
Updated 26-Jan-13 19:32pm
v3

Firstly, you should not use keywords[^] like 'table' for a table name.

Secondly, it looks like there is no space after tablename.
Try:
C#
SqlCommand cm = new SqlCommand("insert into myTable (name,num,sal,phone)  values('" + textBox1.Text + "'," + textBox2.Text + "," + textBox3.Text + "," + textBox4.Text + ")", con);


Third thing, it's bad practice to pass on the controls value directly in query. This can lead to SQL Injection. Read about protecting from SQL Injection here: SQL Injection Mitigation: Using Parameterized Queries[^]
 
Share this answer
 
Comments
p.shakthivel 27-Jan-13 7:16am    
than k you sir... finally it's work
Sandeep Mewara 27-Jan-13 8:03am    
Good to know.
You've not entered the table name...the right statement is

insert into actual_tablename (.....) values(...)

Since table is the keyword, so it is throwing the error... so replace table with actual table name.
 
Share this answer
 
v3

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