I am trying to insert textbox value into the database. But I was prompted with this error of "Incorrect syntax near the keyword 'table' " There is error with the "cmd.ExecuteNonQuery();" What is wrong with my codes?
Below is my code:
string query;
string str = "Data Source=blank blank blank;Initial Catalog=test;User ID=hello;Password=password";
SqlConnection con = new SqlConnection(str);
con.Open();
query = "INSERT INTO table dbo.url_map Values ('" + tbLongURL.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
the correct syntax is
query = "INSERT INTO dbo.url_map Values ('" + tbLongURL.Text + "')";
Basically you added the word "table" into the query which is not SQL syntax. It is
INSERT INTO tablename
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With