Dependent drop downs in grid view - c#

I have two drop down Country & University are dependent drop downs. If country will select pass Countryd to University table countryid and University list should come to universtiy dropdown.
Below is Country Drop drown list inside Gridview
<asp:DropDownList ID="ddl_Country" runat="server" AppendDataBoundItems="true" Width="60px"
DataSourceID="SqlDataSource6" DataTextField="Country" DataValueField="CountryID"
AutoPostBack="True" OnSelectedIndexChanged="ddl_Country_SelectedIndexChanged">
Below is University Drop drown list inside Gridview.
<asp:DropDownList ID="ddl_University" runat="server" AppendDataBoundItems="true" Width="60px"
DataSourceID="SqlDataSource7" DataTextField="University" DataValueField="University">
My two datasource:
<asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="Get_Country" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource7" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="Get_University" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Below is my two stored procedure
Country Stored Procedure:
ALTER procedure [dbo].[Get_Country]
as
select Country,CountryId from Table_LKP_Country order by Country ASC
University Stored Procedure:
ALTER procedure [dbo].[Get_University]
as
select University,CountryId from Table_LKP_University order by University ASC
I want to pass CountryID to LKP university table countryid and base on countryid fill university list in dropdown list. I dont know how to achieve this using country select index or using two sqldatasouce.

Try This
<asp:SqlDataSource ID="SqlDataSource7" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="Get_University" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
<SelectParameters>
<asp:ControlParameter ControlID="ddl_Country" PropertyName="SelectedValue" Name="CountryID" Type="Int32" DefaultValue="0" />
</SelectParameters>
ALTER procedure [dbo].[Get_University]
as
select University,CountryId from Table_LKP_University where CountryId=#CountryID order by University ASC

Related

SqlDataSource, 3rd dropdown in chain fails

Registration form
[This is working fine. I had wrong information to test with. I will leave up because it is the way to do cascading DDL's using SQLDataSource albeit outdated]
My primary language is C# but the application is in VB.Net. I have appointment application students sitting for individual and group portraits on college campuses. The registration page has three dropdown lists:
(1) college Campuses, (2) Organizations that belong to a particular Campus, and a (3) Classification schema that belongs to a particular Organization. The Campus and Organization dropdown lists work as expected. The Classification will display the class schema for the first organization on the initial load of the page but will not change after that. A class schema can be [Freshman, Sophomore, Junior, Senior] or [Undergradutre, Masters, Doctoral, Faculty]. There are other class schemas. There is one class schema per organization.
Running the configure on all Datasource components shows them functioning properly. That is the Classification Data Source shows varying schema depending on the organization parameter entered. Here is the set up:
<asp:SqlDataSource ID="CampusSource" runat="server" ConnectionString="<%$ ConnectionStrings:XXXConnectionString %>"
SelectCommand="spCampusSel" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
<asp:SqlDataSource ID="OrgSource" runat="server" ConnectionString="<%$ ConnectionStrings:XXXConnectionString %>"
SelectCommand="spOrgByCampusSel" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="CampusDDL" Name="ParentOrg_ID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="ClassSource" runat="server" ConnectionString="<%$ ConnectionStrings:XXXConnectionString %>"
SelectCommand="spClassSchemaByOrgSel" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="OrgDDL" DefaultValue="" Name="OrgID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="CampusDDL"
TabIndex="1"
DataSourceID="CampusSource"
DataValueField="Org_ID"
DataTextField="LongName"
AutoPostBack="True"
RunAt="server">
</asp:DropDownList>
<asp:DropDownList ID="OrgDLL"
TabIndex="2"
DataSourceID="OrgSource"
DataTextField="LongName"
DataValueField="Org_ID"
AutoPostBack="True"
RunAt="server">
</asp:DropDownList>
<asp:DropDownList ID="ClassDDL"
TabIndex="8"
DataSourceID="ClassSource"
DataValueField="Class"
DataTextField="Class"
AutoPostBack="False"
runat="server">
</asp:DropDownList>
According the Microsoft Docs, this is all I need. But I have also tried setting the ClassSource's parameter value in code at various palaces including the Selecting event. At least I could see that the OrgDDL has the correct organization values (the primary key of the organization). Thus the correct values were being supplied to the datasource.
Protected Sub DMS_Class_Selecting(ByVal sender As Object, ByVal e As SqlDataSourceSelectingEventArgs) Handles DMS_Class.Selecting
e.Command.Parameters(0).Value = OrgDDL.SelectedValue
End Sub
All SQLDataSources are set to DataSet mode. The Class Schema store procedure returns 1 column with each class on a row. Such as:
Freshman
Sophomore
Junior
Senior

What this usage of '#' in ASP.NET mean

I am learning ASP.NET(C#) and came across this code:
<asp:DropDownList
id="ddlMovieCategory"
DataSourceID="srcMovieCategories"
DataTextField="Name"
DataValueField="Id"
Runat="server" />
<asp:Button
id="btnSelect"
Text="Select"
Runat="server" />
<asp:GridView
id="grdMovies"
DataSourceID="srcMovies"
CssClass="gridView"
Runat="server" />
<asp:SqlDataSource
id="srcMovieCategories"
SelectCommand="SELECT Id, Name FROM MovieCategories"
ConnectionString="<%$ ConnectionStrings:Movies %>"
Runat="server" />
<asp:SqlDataSource
id="srcMovies"
SelectCommand="SELECT Title,Director FROM Movies
WHERE CategoryId=#Id"
ConnectionString="<%$ ConnectionStrings:Movies %>"
Runat="server">
<SelectParameters>
<asp:ControlParameter
Name="Id"
ControlID="ddlMovieCategory"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
I am wondering How the # symbol in the last control's SelectCommand work. It works as expected but I don't understand how it gets the value following the # i.e. if it is to use a variable which is escaped in ASP tag then how does it know that Id refers to the DropDownList's currently selected item.
Edit: I think I might have found the solution. I think it's from this bit of code, Am I correct:
<asp:ControlParameter
Name="Id"
ControlID="ddlMovieCategory"
PropertyName="SelectedValue" />
</SelectParameters>
The following string
"SELECT Title,Director FROM Movies WHERE CategoryId=#Id"
represents a parameterized sql query. The value of parameter Id, #Id, would be assigned to CategoryId, before this query is sent to database, in order to be executed.
Parameterized queries are the first thing we use, in order we avoid SQL injections.

Asp.net update DropDownList selected item

I'm stuck into a problem using a DropDownList item into a form.
The main form is feed by an SQLDataSource ... all form widget are bound to a specific record field etc.
I changed one of the form widget (a TextBox) with a DropDownList to allow the user to only select between a number of values... I feed this control trough another SQLDataSource. While the form is in Insert mode there are no problems but when I switch into Edit mode I'm not able to set the DropDownList selected item with the value coming from the main record. Ho can I fix this?
Here's a sample of my .aspx code:
<asp:FormView ID="_frmData" runat="server" DataKeyNames="id" DataSourceID="_sdsTable1" DefaultMode="Insert">
<InsertItemTemplate>
<asp:TextBox ID="_txtField0" runat="server" Text='<%#Bind("field0")%>'/>
<asp:DropDownList ID="_ddlField1" runat="server" DataSourceID="_sdsTable2" DataTextField="field_text" DataValueField="field_value" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="None" Selected="True" />
</asp:DropDownList>
</InsertItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="_txtField0" runat="server" Text='<%#Bind("field0")%>'/>
<asp:DropDownList ID="_ddlField1" CssClass="input_medium" runat="server" DataSourceID="_sdsTable2" DataTextField="field_text" DataValueField="field_value" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="None" Selected="True" />
</asp:DropDownList>
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="_sdsTable1" runat="server" ConnectionString="<%$ ConnectionStrings:_db %>"
ProviderName="<%$ ConnectionStrings:_db.ProviderName %>"
SelectCommand=" SELECT
field0, field1
FROM
table1
WHERE
id=#id;"
UpdateCommand=" UPDATE
table1
SET
`field0` = #field0
`field1` = #field1
WHERE
id = #id;""
InsertCommandType="StoredProcedure"
InsertCommand="create_table1_entry">
<InsertParameters>
<asp:ControlParameter Name="field1" ControlID="_frmData$_ddlField1" Type="String" PropertyName="SelectedItem.Text" />
</InsertParameters>
<UpdateParameters>
<asp:ControlParameter Name="field1" ControlID="_frmData$_ddlField1" Type="String" PropertyName="SelectedItem.Text" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="_sdsTable2" runat="server" ConnectionString="<%$ ConnectionStrings:_db %>"
ProviderName="<%$ ConnectionStrings:_db.ProviderName %>"
SelectCommand=" SELECT
field_value, field_text
FROM
table2;" />
Try This
myDropDown.Items.FindByValue("Your Main Record Value").Selected=true;
OR
myDropDown.Items.FindByText("Your Main Record Text").Selected=true;
I CAN JUST use SelectedValue='<%#Bind("field1")%>' property in DDL to achieve the result, just as I was guessing. The only issue here is the why Intellisense is not suggesting SelectedValue as a propery for DropDownList ... thnx everyone.

Inserting multiple rows into SQL Server database from cascade dropdown box selection

I have a page I need to modify. The page was written by another developer. The page already had a combobox and function that allowed the user to select an author's name, click a button, then add that author's name to their wish list. I need to add another combobox that displays the library's genres, display the authors associated with that genre, then allow the user to click a button and add all those authors to their reading wish list. Here is the code with both the author and the genre comboboxes and the script that I'm attempting to use to insert all the authors when a genre is selected:
<asp:DropDownList ID="ddlGenres" runat="server" DataSourceID="SqlDataSourceLibros"
DataTextField="Genre_Name" DataValueField="GenreID"
onselectedindexchanged="genreList_SelectedIndexChanged" AutoPostBack="True" AppendDataBoundItems="true">
<asp:ListItem Value="0">Groups</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1Libros" runat="server"
ConnectionString="<%$ ConnectionStrings:Libros %>"
SelectCommand="SELECT [GenreID],[Genre_Name] FROM [Library_Genres] ORDER BY [Genre_Name]">
</asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
DataSourceID="SqlDataSourceLibros2" DataTextField="Author_Name" EnableViewState="false"
DataValueField="Genre_Name">
</asp:DropDownList>
<asp:ImageButton ImageUrl="~/buttons/addGenre.png" Height="18px"
OnMouseOver="this.src='../buttons/addGenreHover.png'"
OnMouseOut="this.src='../buttons/addGenre.png'"
ToolTip="Add Library Genre" runat="server" ID="btnAddGenre"
OnClick="btnAddGenre_Click" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:Libros %>"
SelectCommand="SELECT [Author_Name] FROM [Library_authors] WHERE ([Genre_Name] = #Genre_Name)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlGroups" Name="Author_Name"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
And now the script that is called by clicking the button:
protected void btnAddAuthors(object sender, EventArgs e)
{
odsAuthors.Insert();
AuthorList.ClearSelection();
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void btnAddGenre(object sender, EventArgs e)
{
SqlDataSourceLibros2.Insert();
ddlGenres.ClearSelection();
Response.Redirect(Request.Url.AbsoluteUri);
}
My problem is that when I click the Add Genres button, the event that adds the individual author fire and is inserted - not all of the authors that appear in the combo box - depending on what genre is selected.
How can I get the list of authors to be inserted into the database like the individual author is inserted into the database when the AddAuthors button is clicked?
Check this answer, you cannot use ordinary select box - as it really has only one value, you should use controls for multiple selection, after this - you cannot populate them with authors, them them all as selected/checked and then you will get all values in your C# code, then you will be able to insert them all with for

Drop down not retrieved data from database

Why drop down menu not showing ID from database? I have login page. when user enter their username and password, I will create a session from them.
Session["username"] = Login1.UserName;
I have two table in my database. 1 is aspnet_Users (keep all users info) and another one is CarReserve table (contains of ID, username and booking details which all is managed by admin)
In user page, when user click on MY BOOKING, it will have a drop down menu which only has ID (from CarReserve) then after user choose the ID from there, they get to view all the details from CarReserve that only belongs to ID. This is my code to retrieve the ID from database:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="ID" DataValueField="ID"
Height="21px" Width="147px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Connection %>"
SelectCommand="SELECT CarReserve.ID
FROM CarReserve
INNER JOIN aspnet_Users
ON CarReserve.UserName = aspnet_Users.UserName WHERE CarReserve.UserName = #UserName">
<SelectParameters>
<asp:SessionParameter Name="UserName"
SessionField="session["username"]" />
</SelectParameters>
</asp:SqlDataSource>
The problem is the drop down is not showing the ID of current login user. Can someone tell me what is wrong with my codes? Thanks
Your SessionField parameter should read
SessionField="username"

Categories