Populate drop downlist from another dropdownlist from sql datasource in asp.net

Valyx

New Member
I am trying to learn how to fetch data from \[code\]database\[/code\] and load data in \[code\]dropdownlist\[/code\].What I have populated data for model of cars.Now on selection of model i want to populate another dropdown list of Cars.I am first fetching data into \[code\]sqldatasource\[/code\] and then feeding to \[code\]dropdownlist\[/code\] of Model.Now I want to popluate Cars \[code\]dropdownlist\[/code\] based on \[code\]selectedindexchangeproperty\[/code\] of Model,feed data in \[code\]sqldatasource\[/code\] and then populate data in Cars \[code\]dropdownlist\[/code\].\[code\]Table of Model: Model_id, Model_NameTable of Cars: Car_id, Car_Name, Model_Id_Reference.\[/code\]Model_Id_Reference refer to Model_id of Model Table Here is my code so far\[code\]<asp:DropDownList ID="ddlModel" runat="server" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="Model_Name" DataValueField="Model_Name" onselectedindexchanged="ddlModel_SelectedIndexChanged"> <asp:ListItem>-Select-</asp:ListItem> </asp:DropDownList> <br /> <br /> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Model]"></asp:SqlDataSource> <br /> <br /> Select Car&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:DropDownList ID="ddlCar" runat="server" DataSourceID="SqlDataSource3" DataTextField="Car_Name" DataValueField="Model_Id_Reference"> </asp:DropDownList> <br /> <br /> <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Cars] WHERE ([Model_Id_Reference] = @Model_Id_Reference)"> <SelectParameters> <asp:ControlParameter ControlID="ddlModel" DefaultValue="http://stackoverflow.com/questions/15591109/0" Name="Model_Id_Reference" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </asp:SqlDataSource>\[/code\]
 
Top