c# - How to insert text box value in database on combo box selected item? -
i designed 1 login form , in used combobox1, textbox1, textbox2 , button. when select "admin" in combobox username , password in textbox1 , textbox2 checked admin table in database if username , password correct show admin form.
how can this?
code:
private void button1_click(object sender, eventargs e) { if (combobox1.selectedtext == "admin") { sqlcommand cmd = new sqlcommand("select * admin"); sqldatareader dr = cmd.executereader(); if (dr.read()) { textbox1.text = dr["username"].tostring(); textbox2.text = dr["password"].tostring(); admin ad = new admin(); ad.showdialog(); } } }
based on code you're trying set value of textbox1
, textbox2
from values have in database. i'm guessing you're trying check if they're equal?
if you're better off modifying query returns details admin based on details entered user rather returning admin table.
also, per alfie goodacre's answer, want selecteditem
.
private void button1_click(object sender, eventargs e) { if (combobox1.selecteditem.tostring() == "admin") { sqlcommand cmd = new sqlcommand("select * admin username = @un , password = @pw"); var unparam = new sqlparameter("@un", sqldbtype.varchar); unparam.value = textbox1.text; cmd.parameters.add(unparam); var pwparam = new sqlparameter("@pw", sqldbtype.varchar); pwparam.value = textbox2.text; cmd.parameters.add(pwparam); sqldatareader dr = cmd.executereader(); //check if data has returned based on user inputs if (dr.hasrows) { if (dr.read()) { admin ad = new admin(); ad.showdialog(); } } } }
Comments
Post a Comment