How to select cascading DropDownList with SqlDataSource - c#

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>

You need to use ControlParameter in SelectParameters.
Make sure that AutoPostBack="True" for DropDownListSpecies
FYI: You have typo in Speecies
<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>

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.

Related

Preloading a SQL databound listview in ASP.NET

I've been looking online for a while and I may not be asking my question right but I can't find what I am looking for.
I have a dropdownlist on a web form that is databound to a SQL select statement that returns 3 items. I appended the list with a blank item at the top so that I am not selecting the other options on page load.
I also have a listview that has a ControlParameter of the DDL.
What I want to do is when the page loads to have all items selected instead of none. Do I need to set Selected to True on each of the items in the list and how would I do that if the information is fetched from a SQL query.
A snippet of the code with the queries:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSourceDockNumber"
DataTextField="DockID" DataValueField="DockID"
AutoPostBack="True" AppendDataBoundItems ="true">
<asp:ListItem Selected="False" Text="" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceDockNumber" runat="server"
ConnectionString="<%$ ConnectionStrings:MarinaConnectionString %>"
SelectCommand="SELECT DISTINCT [DockID] FROM [Slip]">
</asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="SqlDataSourceAvailSlips" runat="server"
ConnectionString="<%$ConnectionStrings:MarinaConnectionString %>"
SelectCommand="SELECT * FROM [avilableSlips] WHERE ([DockID] = #DockID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="DockID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

How to fill dropdownlist from database based on selected item of another dropdownlist in asp.net

I have a SQL table named doctor search and have 2 dropdownlists one shows expertise of the doctor and i want to fill dropdownlist 2 with the information of those doctor which are related to that expertise section.
This can be done easily if you are using the designer interface.
Try using the below code after making necessary changes (Connection String, Table name and Column name). If that doesn't work, I will share the steps
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Your Connection String %>" SelectCommand="SELECT [Expertise] FROM [Your Table]"></asp:SqlDataSource>
<asp:DropDownList ID="ddlExpertise" runat="server" DataSourceID="SqlDataSource1" DataTextField="Expertise" DataValueField="Expertise">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Your Connection String %>" SelectCommand="SELECT [DoctorDetails] FROM [Your Table] WHERE ([Expertise] = #Expertise)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlExpertise" DefaultValue="" Name="Expertise" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="ddlDoctorDetails" runat="server" DataSourceID="SqlDataSource2" DataTextField="DoctorDetails" DataValueField="DoctorDetails">
</asp:DropDownList>

default selection on drop-down list

I have a list of names to select from a dropdown list. The names are being pulled from a data source and not from a ListItem. I want to leave the box blank while no selection has been. How can I do this?
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource2" DataTextField="director"
DataValueField="director">
<!--<asp:ListItem Text="Please Select Director" Value="-1"/>-->
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$
ConnectionStrings:dvdsConnectionString %>" ProviderName="<%$
ConnectionStrings:dvdsConnectionString.ProviderName %>"
SelectCommand="SELECT [director] FROM [dvds]">
</asp:SqlDataSource>
Try add AppendDataBoundItems="True" to the tag and add <asp:ListItem Selected="True"></asp:ListItem> as an item
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource2" DataTextField="director"
DataValueField="director" AppendDataBoundItems="True">
<asp:ListItem Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$
ConnectionStrings:dvdsConnectionString %>" ProviderName="<%$
ConnectionStrings:dvdsConnectionString.ProviderName %>"
SelectCommand="SELECT [director] FROM [dvds]">
</asp:SqlDataSource>

Sqldatasource where parameter

I have a sqldatasource, in select command i have #status parameter. The parameter take the value from the textbox at runtime.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [project_details] WHERE ([status] = #status)"
FilterExpression="title='{4}'"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
EnableCaching="True">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
my problem is that when i run the page without entring the parameter in text box sqldatasource is not returing any row.
Looking at the documentation on MSDN, you have to alter how you have setup the SqlDataSource.
Try this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [project_details]"
FilterExpression="title='{0}'"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
EnableCaching="True">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
I have removed the Where clause from the query as this will be applied by the filter expression. Also, I have changed the filter expression from title='{4}' to title='{0}'. The documentation states that the number is a placeholder to an item in the FilterParameters collection.
Updated
I have also changed the SelectParameters to FilterParameters
Update 2
I have created a working example to finish of this answer. This will filter the Title column using the text from the text box. If this text box is empty it will return all the rows from the table (a scary thought but OK for this example). It is querying the AdventureWorks database for which I set a connection string called AWorks.
<asp:SqlDataSource ID="SqlDataSource1"
ConnectionString="<%$ ConnectionStrings:AWorks %>"
SelectCommand="SELECT ContactId, Title, FirstName, LastName FROM Person.Contact"
FilterExpression="Title='{0}'"
runat="server">
<FilterParameters>
<asp:ControlParameter Name="Title" ControlID="txtTitle" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
<asp:TextBox runat="server" Id="txtTitle"></asp:TextBox>
<asp:Button runat="server" UseSubmitBehavior="true" Text="Submit" />
<asp:GridView
DataSourceID="SqlDataSource1"
AutoGenerateColumns="false"
runat="server">
<Columns>
<asp:BoundField Visible="false" DataField="ContactId"></asp:BoundField>
<asp:BoundField Visible="true" DataField="Title"></asp:BoundField>
<asp:BoundField Visible="true" DataField="FirstName"></asp:BoundField>
<asp:BoundField Visible="true" DataField="LastName"></asp:BoundField>
</Columns>
</asp:GridView>
try a condition like this:
(#Status is null or #Status ='' Or Status = #Status)
set ConvertEmptyStringToNull="true" and then try....
Add CancelSelectOnNullParameter="false" to your SqlDataSource.

How to implement nested DropDownLists insid a GridView in asp.NET?

I have two nested DropDownLists (the value selected in the 1st ddl will limit the content of the 2nd ddl).
The example attached here runs fine outside of a GridView. As soon as I try to insert this into a GridView, I get an error because I cannot identify the ControlID to use in the ControlParameter.
The code outside of the GridView looks like this:
<asp:DropDownList ID="ACTION1_DROPDOWNLIST"
AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource1" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST2"
ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource2" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
And the DataSources look like this:
<asp:Panel ID="HiddenFields" runat="server">
<asp:TextBox ID="DRAFT" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [Items]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [SubItems] WHERE [itemId]=:v_ItemId">
<SelectParameters>
<asp:ControlParameter Name="v_ItemId" ControlID="ACTION1_DROPDOWNLIST" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
Thanks!
I have tried something similar and it works fine. I can't tell from your post whether the data sources are together with the dropdowns inside the ItemTemplate of the GridView. At least the second one should be inside the ItemTemplate so that you will have a data source for each line in the grid.
I have posted my page below. It's using different tables, but it's the same idea.
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST"
AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource1" DataTextField="Surname" DataValueField="FamilyID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST2"
ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="PersonID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:Panel ID="HiddenFields" runat="server">
<asp:TextBox ID="DRAFT" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"
ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"
SelectCommand="SELECT * FROM [Person] WHERE ([FamilyId] = #FamilyId)">
<SelectParameters>
<asp:ControlParameter Name="FamilyId" ControlID="ACTION1_DROPDOWNLIST"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server">
</asp:LinqDataSource>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"
ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"
SelectCommand="SELECT * FROM [Family]">
</asp:SqlDataSource>
</form>
The actual id's are build based on where they are, so you will see something like $GridView1 inserted in the id's.
To solve this you can set ClientIDMode="Static" on your page properties (first line of your .aspx file).
Or use "javascript:var a = document.getElementById('" + ACTION1_DROPDOWNLIST.ClientID + "');

Categories

Resources