combobox - C# Passing Values between dependent Comboboxes -
i'm relatively new i've been researching issue on 2 days, think i've done due diligence ... if has been answered before apologize. basic issue i'm trying create dependent combo boxes. wrinkle displayed value typically not lookup value next query/combo box (i'm using oledb compliant data base) example: table1 (t1) contains id (int) & nm (string), table2 (t2) contains id (int) & status (string). run query1 (q1) display t1.nm in combobox1 (cb1), when selected run query1a lookup/get selected table1.id pass query2 populates combobox2. connection string , q1 work fine, cb1 displays properly, once select error thrown: "oledbexception .. sql passthru expression ... using equals (=) has components of different data types"
// ** initial connection & populate cb1 - works fine **
public void comboboxload() { string conn3str = <connection string >; string query1 = "select nm table1 refvalue=1 ; "; oledbconnection conn3 = new oledbconnection(conn3str); oledbcommand tblrow1 = new oledbcommand(query1, conn3); oledbdatareader rdrow1; try { conn3.open(); lblconnstate.text = "connection successful"; rdrow1 = tblrow1.executereader(); while (rdrow1.read()) { int colindx1 = rdrow1.getordinal("nm"); string sitbl = rdrow1.getstring(colindx1); cb1.items.add(sitbl); } } catch (exception ex) { messagebox.show("error " + ex); } }
// ** value cb1, create query populate cb2 **
private void cb1_selectedindexchanged(object sender, eventargs e) { string conn3str = <connection string >; oledbconnection conn3 = new oledbconnection(conn3str); conn3.open(); // pass selected value cb1 (string) equal table1.nm (string) string query1a = "select id table1 nm = '" + cb1.text + "' ; "; oledbcommand tabid = new oledbcommand(query1a, conn3); int tabid2 = convert.toint32(tabid.executescalar()); // pass variable tabid2 (int) equal table2.id (int) string query2 = "select status table2 id = '" + tabid2 + "'; "; oledbcommand tblrow2 = new oledbcommand(query2, conn3); // oledbdatareader rdtabid; // oledbdatareader rdrow2; try { oledbdatareader rdrow2 = tabid.executereader(); oledbdatareader rdtabid = tblrow2.executereader(); // ** error points line ** while (rdrow2.read()) { int tabididx = rdtabid.getordinal("id"); string tabidval = rdtabid.getstring(tabididx); // pass reference id label on form lblbtableid.text = tabid2.tostring(); int colindx1 = rdrow2.getordinal("status"); string sintval = rdrow2.getstring(colindx1); cmblowlvl.items.add(sintval); } } catch (exception ex) { messagebox.show("error " + ex); } }
are positive you're getting value on line int tabid2 = convert.toint32(tabid.executescalar());
?
convert.toint32
doesn't throw argumentnullexception
int.parse
it's possible variable not getting set.
also may want consider changing queries use parameterized sql rather concatenation security purposes. https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters(v=vs.110).aspx
Comments
Post a Comment