Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to insert into my database table

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();
like image 367
qU3st Avatar asked Dec 13 '25 22:12

qU3st


1 Answers

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

like image 177
logixologist Avatar answered Dec 15 '25 12:12

logixologist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!