Minions
Minions

Reputation: 5487

MYSQL SQL syntax error c#

I have an error in my SQL statement. This is the query:

 MySqlCommand cmd = new MySqlCommand("insert into bills (bill_Number,bill_Date,bill_From,bill_Note,bill_TaxRate,bill_DisRate,bill_EntryDate,cus_Sup,by,archived) values(" + txbBillNumber.Text + ",'" + DateTime.Parse(txbBillDate.Text).Year + "-" + DateTime.Parse(txbBillDate.Text).Month + "-" + DateTime.Parse(txbBillDate.Text).Day + "'," + sup_Id + ",'" + txb_Note.Text + "'," + taxRate + "," + disRate + ",'" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "'," + Bills.cus_Sup + ",0,0)", objConn);
                        cmd.ExecuteNonQuery();

And in this image the error and the bills table structure: Screebshot1

Upvotes: 0

Views: 52

Answers (3)

jpw
jpw

Reputation: 44911

One problem is that the word by that you use as a column is a reserved keyword (both in MySQL and the SQL standard in general). To use it you have to enclose it in backticks `` or double-quotes " ".

Also, the string values need to be between single-quotes where applicable, but I think you got that.

On a side note you should look into using parametrized queries instead of concatenating. See this question for an example: C# with MySQL INSERT parameters

Upvotes: 4

jmcclure
jmcclure

Reputation: 122

My first thoughts is that "by" is a reserved word, and "by" needs to be encapsulated in single/double quotes...whatever mysql requires. This is from sql server experience.

Upvotes: 0

bluemax
bluemax

Reputation: 51

Your string values must be enclosed in quotes in the SQL statement, like this: MySqlCommand cmd = new MySqlCommand("insert into bills (bill_Number,bill_Date,bill_From,bill_Note,bill_TaxRate,bill_DisRate,bill_EntryDate,cus_Sup,by,archived) values(\"" + txbBillNumber.Text + "\"...

Upvotes: 1

Related Questions