2

I have the following in my data base -

breedId Species Breed
0       dog      Alsatian
1       dog      pitbull
2       dog      Shetland sheepdog
3       dog      Boxer
4       cat      Dragon Li
5       cat      Australian Mist
6       cat      Korat

In the c# designer view, I have 2 drop-down list one which has species and other for breed.

What I want is that when user picks 'dog' in species list, the breed list should have the following Alsatian, pitbull, Shetland sheepdog,Boxer

At the moment when I pick 'dog', all the breed from the database is shown.

<asp:DropDownList ID="DropDownListSpecies" runat="server" 
     Height="27px" Width="107px" DataSourceID="hs330" 
     DataTextField="Species" DataValueField="Species">
</asp:DropDownList>
<asp:SqlDataSource ID="Species" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     SelectCommand="SELECT DISTINCT [Species] FROM [Breed]">
</asp:SqlDataSource>

<asp:DropDownList ID="DropDownListBreed" runat="server" Height="20px" 
    Width="110px" DataSourceID="breed" DataTextField="Breed" 
    DataValueField="Breed">
</asp:DropDownList>
<asp:SqlDataSource ID="breed" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT DISTINCT [Breed] FROM [Breed]">
</asp:SqlDataSource>
2
  • This should probably be tagged ASP .NET instead of C#, unless you're looking for a code behind solution. Commented Nov 6, 2013 at 22:56
  • @Mathieson your most likly to be right but i dont knoe how to change the tags, if a moderator can change c# to asp.net id be happy Commented Nov 6, 2013 at 23:05

2 Answers 2

3

You need to use ControlParameter in SelectParameters.

Make sure that AutoPostBack="True" for DropDownListSpecies

FYI: You have typo in Speecies

enter image description here enter image description here

<asp:DropDownList ID="DropDownListSpecies" runat="server"
    Height="27px" Width="107px" DataSourceID="Species"
    DataTextField="Species" DataValueField="Species" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="Species" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT DISTINCT [Species] FROM [Breed]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownListBreed" runat="server"
    Height="20px" Width="110px"
    DataSourceID="breed" DataTextField="Breed" DataValueField="Breed">
</asp:DropDownList>
<asp:SqlDataSource ID="breed" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT DISTINCT [Breed] FROM [Breed] WHERE Species=@Species">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownListSpecies" PropertyName="SelectedValue"
            Name="Species " Type="String" DefaultValue="cat" />
    </SelectParameters>
</asp:SqlDataSource>
Sign up to request clarification or add additional context in comments.

Comments

1

You are not filtering the data in the second drop down list, based on the selection made in the first drop down list (which is what you want).

    <asp:DropDownList ID="DropDownListBreed" runat="server" Height="20px" Width="110px" DataSourceID="breed" DataTextField="Breed" DataValueField="Breed">
    </asp:DropDownList>
    <asp:SqlDataSource ID="breed" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT DISTINCT [Breed] FROM [Breed] WHERE Species = @Species">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownListSpecies" PropertyName="SelectedValue"
            Name="Species " Type="String" DefaultValue="cat" />
    </SelectParameters>
</asp:SqlDataSource>

Also, you need to add the AutoPostBack="True" property to each DropDownList, if you want the changes to be reflected as soon as you change the value of each DropDownList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.