c# - Using textbox value for a class -
i'm trying use textbox value form class insert items database. tried creating instance of form1 name of form want value returns 0 items database when click submit button there anyway this?
public void insert() { form1 mform = new form1(); string query = "insert parts (item_id, description, brand, model, color, quantity) values('0', '" + mform.description.text + "','" + mform.brand.text + "','" + mform.model.text + "','" + mform.color.text + "','" + mform.quantity.text + "')"; if (this.openconnection() == true) { mysqlcommand cmd = new mysqlcommand(query, connection); cmd.executenonquery(); this.closeconnection(); } }
when instantiate new instance of form1()
object, assuming new instance's "description, brand, model, color , quantity" textbox's
contains text? default value of textbox.text
default value of string
property type, null
.
ideally, take values user has populated form instance , pass them db so:
public void insert() { using (var mform = new form1()) { // ensure user entered values... if (mform.showdialog() == dialogresult.ok) { string query = "insert parts (item_id, description, brand, model, color, quantity) values('0', '" + mform.description.text + "','" + mform.brand.text + "','" + mform.model.text + "','" + mform.color.text + "','" + mform.quantity.text + "')"; if (this.openconnection() == true) { var cmd = new mysqlcommand(query, connection); cmd.executenonquery(); this.closeconnection(); } } } }
additionally, should avoid inline sql , instead use stored procedures, or @ least use sqlparameter's
.
Comments
Post a Comment