SqlDataSource, 3rd dropdown in chain fails - c#

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

Related

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.

Dependent drop downs in grid view

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

The server tag is not well formed asp.net C#

I'm doing the following query, and trying to use a code that come from a previous query.
But is giving me the following error: The server tag is not well formed.
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:TesteConnectionString %>" SelectCommand="SELECT * FROM [Equipa] where idAssemb=1 and idDept=<%# Eval("idDept") %>"></asp:SqlDataSource>
I'm using C# in Web forms asp.net
Can somebody help me on this?
Eval is used in DataBound controls to evaluate a field value in a row from the data source. You are trying to use it inside a Data Source control itself (SQLDataSource in this case). You should use parameterized query by specifying the value of parameter inside SelectParameters tag like this:-
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:TesteConnectionString %>"
SelectCommand="SELECT * FROM [Equipa] where idAssemb=1 AND idDept=#DeptId>
<SelectParameters>
<asp:ControlParameter ControlID="lblDeptId" Name="DeptId"
PropertyName="Text" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Please note, here I have shown the example of a control present inside your WebForm. You can specify where the value of DeptId is coming from via Cookie, QueryString, Form, Session etc.

Initialize a GridView with an initially unused SelectParameter

Is there a way to define SessionParamters in the SelectParameters of a DataSource that aren't called in the SelectCommand?
I want the parameter to be defined so it can be used later, after initial page generation, but when it's not used in the SelectCommand the DataSource doesn't seem to work - the GridView which calls it appears empty, yet generates fine when the unused Parameters are excluded.
For example, the following definition will fill the GridView successfully:
<asp:SqlDataSource ID="DataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" SelectCommand="SELECT * FROM Customers WHERE #UsedParameter='Green'">
<SelectParameters>
<asp:SessionParameter Name="UsedParameter" SessionField="Parameter1" />
</SelectParameters>
</asp:SqlDataSource>
Whereas the following would not fill the GridView:
<asp:SqlDataSource ID="DataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" SelectCommand="SELECT * FROM Customers WHERE #UsedParameter='Green'">
<SelectParameters>
<asp:SessionParameter Name="UsedParameter" SessionField="Parameter1" />
<asp:SessionParameter Name="UnUsedParameter2" SessionField="Parameter2" />
</SelectParameters>
</asp:SqlDataSource>
That is interesting... I think I've seen that problem before when I forgot to remove a parameter. Since you are likely going to change the select command later to use the other parameter, why don't you just add the parameter at that time?
VB
Dim p As New SessionParameter("UnUsedParameter2", "Parameter2")
DataSource1.SelectParameters.Add(p)
C#
SessionParameter p = new SessionParameter("UnUsedParameter2", "Parameter2")
DataSource1.SelectParameters.Add(p);

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

Categories