How to show listbox item in textBox using data from local database in C# -
hi im new programming , working on project lets user enter registration data local database using textboxes. code works adding items database , after press "show_users" button displays them in listbox_users listbox.
my problem when choose name listbox_users should dislay data selected user in upper textbox'es used enter data in first place using event created listbox_users, im getting error "can't read data database closed".
namespace userform { public partial class form1: form { sqlcedatareader rdr; public form1() { initializecomponent(); } // code between... private void button_showusers_click(object sender, eventargs e) //code shows users in listbox { var dt = new datatable(); string connectionstring2 = @"data source=mydatabase;password=xxxxxx;"; using (var cn = new sqlceconnection(connectionstring2)) using (var cmd = new sqlcecommand("select * users", cn)) { cn.open(); using (var reader = cmd.executereader()) { dt.load(reader); var results = (from row in dt.asenumerable() select new { //userid = row.field<int>("id"), firstname = row.field<string>("firsname"), lastname = row.field<string>("lastname"), fullname = row.field<string>("firstname") + " " + row.field<string>("lastname") }).tolist(); listbox_users.datasource = results; listbox_users.displaymember = "fullname"; rdr = cmd.executereader(); } } } //i made event listbox_users: private void listbox_users_selectedindexchanged(object sender, eventargs e) //event code should show listbox selected data in textboxes { if (listbox_inimesed.selecteditem != null && rdr != null) { try { if (rdr.read()) { textbox1_firstname.text = rdr.getstring(1); textbox2_lastname.text = rdr.getstring(2); textbox3_email.text = rdr.getstring(3); textbox4_address.text = rdr.getstring(4); datetimepicker1.value = rdr.getdatetime(5); richtextbox_info.text = rdr.getstring(6); } else messagebox.show("object not found"); } { rdr.close(); } } } }
the connection reader using dependent on has been closed in button_showusers_click event.
it not practice try share connections , datareaders across events. result in open connections not disposed of correctly. better practice have creation of connection, command, , datareader take place in each event method. using "using" statement closed , disposed of correctly in each method. can remove class level variable "rdr".
namespace userform { public partial class form1 : form { const string connectionstring2 = @"data source=mydatabase;password=xxxxxx;"; public form1() { initializecomponent(); } // code between... private void button_showusers_click(object sender, eventargs e) //code shows users in listbox { var dt = new datatable(); using (var cn = new sqlceconnection(connectionstring2)) using (var cmd = new sqlcecommand("select * users", cn)) { cn.open(); using (var reader = cmd.executereader()) { dt.load(reader); var results = (from row in dt.asenumerable() select new { //userid = row.field<int>("id"), firstname = row.field<string>("firsname"), lastname = row.field<string>("lastname"), fullname = row.field<string>("firstname") + " " + row.field<string>("lastname") }).tolist(); listbox_users.datasource = results; listbox_users.displaymember = "fullname"; } } } //i made event listbox_users: private void listbox_users_selectedindexchanged(object sender, eventargs e) //event code should show listbox selected data in textboxes { if (listbox_inimesed.selecteditem != null) { using (var cn = new sqlceconnection(connectionstring2)) using (var cmd = new sqlcecommand("select * users", cn)) { cn.open(); using (var reader = cmd.executereader()) { if (reader.read()) { textbox1_firstname.text = reader.getstring(1); textbox2_lastname.text = reader.getstring(2); textbox3_email.text = reader.getstring(3); textbox4_address.text = reader.getstring(4); datetimepicker1.value = reader.getdatetime(5); richtextbox_info.text = reader.getstring(6); } else messagebox.show("object not found"); } } } } } }
Comments
Post a Comment