简体   繁体   中英

How to add data into mysql using c# (edit table column on combo box selection )

I want to add item in a table in a database in mysql based upon what the user has selected in the items combobox.

Eg: if a person chooses tea (this event should populate tea column in my table) and fills a quantity as 2 then I want to add 2 under the column name tea in mysql table . I'm trying to use the update statement, but it gives an error the syntax is not correct.The column to be filled in table changes if the user chooses tea or coffee that's why I have used "+this.items.Text+"

 "UPDATE employee.transaction SET department = ' " + this.department.Text + " ', ' " + this.items.Text + " ' = ' " + this.quantity.Text + " ' , billno = ' " + this.bill_no_txt.Text + "' where billno = ' " + this.bill_no_txt.Text + " ' ;";

I might see several points in your query that could cause problems. So lets go through it:

SET   department = ' " + this.department.Text + " ',

Please make sure you enter the right data into the database. In SQL queries you need to make sure you have no spaces if you enter a text into a text or a varchar field. What you enter above is " TEXT " and not "TEXT" (mind the spaces). The following will enter the text without a space at the beginning and the end:

SET   department = '" + this.department.Text + "',

What's most likely causing the error is:

billno = ' " + this.bill_no_txt.Text + "'

I assume your billno column is defined as an int (which would be correct). But you have to make sure you insert it as such. By using ' (single brakets) you are trying to enter text into the int field, which would cause an error. I can not say it for sure since I can't see the table definitions of the table you want to update the data in.

What else could cause the problem is that the column name is wrong here:

 ' " + this.items.Text + " ' =  ' " + this.quantity.Text + " ' ,

 ' " + this.items.Text + " ' 

will basickly enter "' Columnname '" which is wrong (with "'" AND spaces). It probabely should be "Columnname". Try it without spaces and without singlebackets:

 " + this.items.Text + " =  '" + this.quantity.Text + "' ,

But despite all of that I think you have an architectural problem in your database... If you have 30 different beverages you will need to have 30 columns which can be done with an to n relationship between a three column table and a two column table. I suggest you look a bit into that first.

Heres the corrected query but make sure you understand the changes:

UPDATE employee.transaction SET  department = '" + this.department.Text + "'," + this.items.Text +" =  '" + this.quantity.Text + "' , billno = " + this.bill_no_txt.Text + " where billno = " + this.bill_no_txt.Text + ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM