c# - fill dropdownlist from onselectindexchanged -
im using asp.net , c#
im using 2 dropdownlist dd1,dd2
how fill 2nd dropdownlist dd2 dd1 onselectindexchanged
my code is,
<asp:dropdownlist id="ddmedtype" runat="server" cssclass="drop" autopostback="true" onselectedindexchanged="ddmedtype_selectedindexchanged"> <asp:listitem value="0">-select-</asp:listitem> <asp:listitem value="tablet">tablet</asp:listitem> <asp:listitem value="tonic">tonic</asp:listitem> <asp:listitem value="capsules">capsules</asp:listitem> <asp:listitem value="dispotab">disposable tablet</asp:listitem> </asp:dropdownlist> <asp:dropdownlist id="ddmedname" runat="server" cssclass="drop" > <asp:listitem value="0">-select-</asp:listitem> </asp:dropdownlist> protected void ddmedtype_selectedindexchanged(object sender, eventargs e) { string medtype = ddmedtype.selecteditem.text; string str = "select medicinename,medicineid medicinemaster medicinetype = '" + medtype + "'"; cmd = new sqlcommand(str, con); sqldatareader reader = cmd.executereader(); while (reader.read()) { ddmedname.selectedvalue= reader["medicineid"].tostring(); } }
here condition returns 2 items, dropdownlist dd2 returns 1 ...
you setting selectedvalue, not mean you're adding or removing items in dropdownlist
datatable medicines= new datatable(); using (sqlconnection con = new sqlconnection(connectionstring)) { try { sqldataadapter adapter = new sqldataadapter("select medicinename,medicineid medicinemaster medicinetype = '" + medtype + "'", con); adapter.fill(subjects); ddmedname.datasource = subjects; ddmedname.datatextfield = "medicinename"; ddmedname.datavaluefield = "medicineid"; ddmedname.databind(); } catch (exception ex) { // handle error } } // add initial item - can add if options // db not loaded ddmedname.items.insert(0, new listitem("-select-", "0"));
you can reader follows:
ddmedname.items.clear(); ddmedname.items.add(new listitem("-select-", "0")); while (reader.read()) { ddmedname.items.add(new listitem(reader["medicinename"].tostring(), reader["medicineid"].tostring()); }
Comments
Post a Comment