c# 4.0 - Select Items in Listbox -
i have 2 listboxes , corresponding data stored in sql server. able load data of listbox1 sql server. not able populate corresponding data of selected item listbox 2 listbox 1. when click on items in listbox 1 no data displayed in listbox2.
private void form2_load(object sender, eventargs e) { sqlconnection cs = new sqlconnection(@"data source=.\sqlexpress;initial catalog=details;user id=sa;password=p@ssw0rd"); sqlcommand cmd = new sqlcommand(); cmd.connection = cs; cmd.commandtype = commandtype.text; cmd.commandtext = "select memo new"; dataset objds = new dataset(); sqldataadapter da = new sqldataadapter(); da.selectcommand = cmd; // cs.open(); da.fill(objds); //cs.close(); listmemo.valuemember = "memo"; listmemo.displaymember = "memo"; listmemo.datasource = objds.tables[0]; } private void listmemo_selectedindexchanged(object sender, eventargs e) { // string memo = convert.toint32(listmemo.selectedvalue.tostring()); if (listmemo.selectedvalue.tostring() != "") { string memo = convert.tostring(listmemo.selectedvalue.tostring()); filltext(memo); } } private void filltext(string memo) { sqlconnection cs = new sqlconnection(@"data source=.\sqlexpress;initial catalog=details;user id=sa;password=p@ssw0rd"); sqlcommand cmd = new sqlcommand(); cmd.connection = cs; cmd.commandtype = commandtype.text; cmd.commandtext = "select text new memo = '"+ listmemo.selectedvalue.tostring() +"'" ; //cmd.parameters.addwithvalue("@memo", listmemo); dataset objds = new dataset(); sqldataadapter da = new sqldataadapter(); da.selectcommand = cmd; // cs.open(); da.fill(objds); // cs.close(); if (objds.tables[0].rows.count >0) { listtext.valuemember = "text"; listtext.displaymember = "text"; listtext.datasource = objds.tables[0]; } }
if (dt.rows.count > 0) { objds.tables.add(dt); listbox1.datasource = ds.tables[0]; listbox1.datatextfield = "text";// display member listbox1.datavaluefield = "";// id listbox1.databind(); }
bind list box..!
Comments
Post a Comment