melvintcs
melvintcs

Reputation: 551

cant find the error with MySQL + .net

Error msg:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc) VALUES('223', 'hhh')' at line 1

If i erase 'desc' from the query, then it code is functioning. so i believe there is nothing wrong with the code/query.

this is how i designed the front-end and MySQL

txtTitle is a Textbox with Single line, txtDesc is a Textbox with Multiline

i put title as VARCHAR(45) and desc as VARCHAR(1000) This is my code:

string connectionString = @"server=max-5.com.my;userid=user; password=123456;database=myDatabase";


MySqlConnection conn = new MySqlConnection(connectionString);

MySqlCommand cmd = conn.CreateCommand();



cmd.Parameters.Add("title", MySqlDbType.VarChar);

cmd.Parameters["title"].Value = this.txtTitle.Text;



cmd.Parameters.Add("desc", MySqlDbType.VarChar);

cmd.Parameters["desc"].Value = Server.HtmlEncode(this.txtDesc.Text);



cmd.CommandText = "INSERT INTO application(title, desc) VALUES(@title, @desc)";



conn.Open();

int numberOfRows = cmd.ExecuteNonQuery();

conn.Close();

Response.Redirect("Page2.aspx");

Upvotes: 0

Views: 59

Answers (1)

Guffa
Guffa

Reputation: 700222

You are using desc as a field name, which is a reserved keyword. Use backticks around it in the query:

cmd.CommandText = "INSERT INTO application(title, `desc`) VALUES(@title, @desc)";

Alternatively, rename the field.

Upvotes: 4

Related Questions