0

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

Community
  • 1
  • 1
Minions
  • 5,104
  • 5
  • 50
  • 91

3 Answers3

4

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

Community
  • 1
  • 1
jpw
  • 44,361
  • 6
  • 66
  • 86
1

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 + "\"...

bluemax
  • 51
  • 2
0

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.

jmcclure
  • 122
  • 11