c# - How to capture values from panel placed on a form -
i not understanding how capture values in text box placed in panel on form. trying update database table these values.
can point me in right direction?
code:
private void btnsupplier_click(object sender, eventargs e) { try { panel pnladdsupplier = new panel(); textbox txtsupplierid = new textbox(); textbox txtsuppliername = new textbox(); button btnaddsupplier = new button(); label lblsupplierid = new label(); label lblsuppliername = new label(); // initialize panel control. pnladdsupplier.location = new point(56, 74); pnladdsupplier.size = new size(200, 200); // set borderstyle panel three-dimensional. pnladdsupplier.borderstyle = system.windows.forms.borderstyle.fixed3d; // initialize label , textbox controls. lblsupplierid.location = new point(60, 0); lblsupplierid.text = "supplier id"; lblsupplierid.size = new size(104, 20); txtsupplierid.location = new point(20, 40); txtsupplierid.text = ""; txtsupplierid.size = new size(152, 20); lblsuppliername.location = new point(20, 70); lblsuppliername.text = "supplier name"; lblsuppliername.size = new size(150, 20); txtsuppliername.location = new point(20, 90); txtsuppliername.text = ""; txtsuppliername.size = new size(152, 20); lblsupplierid.location = new point(16, 16); lblsupplierid.text = "supplier id"; btnaddsupplier.location = new point(60, 120); btnaddsupplier.text = "add"; btnaddsupplier.click += btnaddsupplier_click; // add panel control form. this.controls.add(pnladdsupplier); // add label , textbox controls panel. pnladdsupplier.controls.add(lblsupplierid); pnladdsupplier.controls.add(txtsupplierid); pnladdsupplier.controls.add(lblsuppliername); pnladdsupplier.controls.add(txtsuppliername); pnladdsupplier.controls.add(btnaddsupplier); } catch { } } public void btnadd_click(object sender, eventargs e) { string username= //how assign textbox value?; int age = //how assign textbox value; mysql.data.mysqlclient.mysqlcommand cmd = new mysql.data.mysqlclient.mysqlcommand(); string query = "insert table(username, age) values ('@xxx', '@xxx') "; //please not these values demonstration. using (connection) { objdb.openconnection(); objdb.executenonquery(query, connection); } }
as can see code open sql injection, , want use parameters. in order need capture details input. how can achieved?
i have tried assign supplierid = txtsupplierid.text
message saying txtsupplier.text
not exist.
thank you.
i don't know textbox used age, hope gives start point.
string username = txtsupplierid.text; // how assign textbox value?; int age = int32.parse(...); // how assign textbox value;
you need more validation on age. therefore, better use:
int age; if (!int32.tryparse(..., out age)) { messagebox.show("incorrect age!"); return; }
change txtsupplierid field:
private textbox txtsuppliername;
and update these line:
txtsuppliername = new textbox(); // remove textbox
Comments
Post a Comment