How to add local database items into textBox using listBox in C# -
and have question if knows: have add selected listbox values using "firstname" + "lastname", "email" , "address" textboxes?
this code here lets me add textbox values database id opposite.
private void button_add_click(object sender, eventargs e) { var insertsql = "insert inimesed (firstname, lastname, email, address) values (firstname, lastname, email, address)"; string connectionstring = @"data source=mydatabase;password=xxxxxx;"; using (var cn = new sqlceconnection(connectionstring)) using (var cmd = new sqlcecommand(insertsql, cn)) { cn.open(); cmd.parameters.add("@firstname", sqldbtype.nvarchar); cmd.parameters.add("@lastname", sqldbtype.nvarchar); cmd.parameters.add("@email", sqldbtype.nvarchar); cmd.parameters.add("@address", sqldbtype.nvarchar); cmd.parameters["firstname"].value = textbox1_firstname.text; cmd.parameters["lastname"].value = textbox2_lastname.text; cmd.parameters["email"].value = textbox3_email.text; cmd.parameters["address"].value = textbox4_address.text; cmd.executenonquery(); } }
there tons of tutorials on "how add textbox items local database in c#", none "how add local database items textbox". got values defined. should use "foreach" commands, "if" commands or "if" commands inside "foreach" commands?
any great!
how going decide row want retrieve?
something following should retrieve single row database based on email address, use values populate textboxes:
private void button_retrieve_click(object sender, eventargs e) { var selectsql = "select firstname, lastname, email, address inimesed email = @email"; string connectionstring = @"data source=mydatabase;password=xxxxxx;"; using (var cn = new sqlceconnection(connectionstring)) using (var cmd = new sqlcecommand(selectsql, cn)) { cn.open(); cmd.parameters.add("@email", sqldbtype.nvarchar); cmd.parameters["email"].value = "emailaddresstofind"; var rdr = cmd.executereader(); try { if (rdr.read()) { textbox1_firstname.text = rdr.getstring(0); textbox2_lastname.text = rdr.getstring(1); textbox3_email.text = rdr.getstring(2); textbox4_address.text = rdr.getstring(3); } else { messagebox.show("could not find record"); } } { rdr.close(); cn.close(); } }
}
Comments
Post a Comment