i'm coding program allows user search customers order using customers name user types in name in textbox
, search results displayed in listbox
control user has select name list box , customer's orders displayed in datagridview
problem once customer clicks search button selectedindex
changed event fires , causes program crash.
private void btnsearch_click(object sender, eventargs e) { string query = "select customerid, companyname customers (companyname ”+ “like'%"+ txtsearch.text + "%')"; clsdatatools.cmdcomand = clsdatatools.con.createcommand(); clsdatatools.cmdcomand.commandtext = query; clsdatatools.dtadataadapter = new sqldataadapter(); clsdatatools.dtadataadapter.selectcommand = clsdatatools.cmdcomand; dsordersbycusname = new dataset(); clsdatatools.con.close(); clsdatatools.con.open(); clsdatatools.dtadataadapter.fill(dsordersbycusname); clsdatatools.con.close(); dsordersbycusname.tables[0].tablename = "orderbcusname"; lstresults.datasource = dsordersbycusname.tables[0]; lstresults.displaymember = "companyname"; lstresults.valuemember = "customerid"; } private void lstresults_selectedindexchanged(object sender, eventargs d) { string query = "select * orders custometid = '" + lstresults.selectedvalue + "'"; datagriddataset = new dataset(); clsdatatools.dtadataadapter = new sqldataadapter(); clsdatatools.cmdcomand = clsdatatools.con.createcommand(); clsdatatools.cmdcomand.commandtext = query; clsdatatools.con.close(); clsdatatools.con.open(); clsdatatools.dtadataadapter.selectcommand = clsdatatools.cmdcomand; clsdatatools.dtadataadapter.fill(datagriddataset); clsdatatools.con.close(); datagriddataset.tables[0].tablename = "orders"; dgvcusorders.datasource = datagriddataset; dgvcusorders.datamember = datagriddataset.tables["orders"].tostring(); }
i don't understand why selctedindexchanged
event fires when click search button there missing maybe?
since set datasource of lstresults
call selectedindexchanged
, can below
private void btnsearch_click(object sender, eventargs e) { //remove handler this.lstresults.selectedindexchanged -= lstresults_selectedindexchanged; // // code // this.lstresults.selectedindexchanged += lstresults_selectedindexchanged; // add handler }
Comments
Post a Comment