Click here to Skip to main content
12,399,426 members (57,260 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# SQL-Server
my code is...........
{
    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 26-Jan-13 18:54pm
Updated 26-Jan-13 19:32pm
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

Secondly, it looks like there is no space after tablename.
Try:
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[^]
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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
Web02 | 2.8.160721.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2016
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