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

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

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

How to populate a textbox based on the selected value of a dropdown list in an asp.net c# web forms application?

I have a dropdown list (doc_rvw_sub_recip_list) that is populated with names from tbl_ad_users. tbl_ad_users contains a field titled "domain_user". I would like to populate an asp:hiddenfield (hdn_domain_user) with the "domain_user" based on the selected value in the doc_rvw_sub_recip_list dropdown list. For testing I used a dropdown list (domain_user_ddl) instead of a hidden field, and it works as needed. But I don't know how to get the value into a hidden field as opposed to using a dropdown list.
The following is doc_rvw_sub_recip_list:
<asp:DropDownList ID="doc_rvw_sub_recip_list" runat="server" DataSourceID="sdc_ad_user_list" DataTextField="name" DataValueField="email_address" AppendDataBoundItems="true" AutoPostBack="true"><asp:ListItem Value="">Please Select</asp:ListItem></asp:DropDownList>
The following is the sql data source for domain_user_ddl:
<asp:SqlDataSource ID="sdc_domain_user_ddl" runat="server" ConnectionString='<%$ ConnectionStrings:idrfConnectionString %>' SelectCommand="SELECT [domain_user] FROM [tbl_ad_users] WHERE ([email_address] = #email_address)">
<SelectParameters>
<asp:ControlParameter ControlID="doc_rvw_sub_recip_list" PropertyName="SelectedValue" Name="email_address" Type="String"></asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource>
The following is domain_user_ddl:
<asp:DropDownList ID="domain_user_ddl" runat="server" DataSourceID="sdc_domain_user_ddl" DataTextField="domain_user" DataValueField="domain_user" AutoPostBack="true"></asp:DropDownList>
How do I get this to work for hdn_domain_user just as it works for domain_user_ddl?
ASPX
Add a OnSelectedIndexChanged event to your dropdown...
<asp:DropDownList ID="doc_rvw_sub_recip_list" runat="server" DataSourceID="sdc_ad_user_list" DataTextField="name" DataValueField="email_address" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="doc_rvw_sub_recip_list_SelectedIndexChanged"><asp:ListItem Value="">Please Select</asp:ListItem></asp:DropDownList>
Add your hidden field...
<asp:HiddenField runat="server" ID="hdn_domain_user" />
Code Behind
Handle the SelectedIndexChanged event...
protected void doc_rvw_sub_recip_list_SelectedIndexChanged(Object sender, EventArgs e)
{
// Populate the hidden field if the dropdown has a selected value
if (doc_rvw_sub_recip_list.SelectedValue != null)
hdn_domain_user.Value = doc_rvw_sub_recip_list.SelectedValue;
}

Error when changing selectedValue in dropdown

I have 2 dropdown menus which are populated using tableAdapters, the list_model dropdown menu is fully dependent on the list_make dropdown menu as it determines which models to show depending on which make has been clicked on, here is the dropdown menus:
<asp:DropDownList ID="list_make" class="form-control padding" runat="server" AutoPostBack="True" DataSourceID="makeODS" DataTextField="make" DataValueField="ID" onselectedindexchanged="list_make_SelectedIndexChanged" SelectedValue='<%# Bind("makeID") %>'></asp:DropDownList>
<asp:ObjectDataSource ID="makeODS" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="getMakes" TypeName="list_dataTableAdapters.MakesTableAdapter"></asp:ObjectDataSource>
<asp:DropDownList ID="list_model" class="form-control padding" runat="server" AutoPostBack="True" DataSourceID="modelODS" DataTextField="model" DataValueField="ID" SelectedValue='<%# Bind("modelID") %>'>
</asp:DropDownList>
<asp:ObjectDataSource ID="modelODS" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetModelsByMakeID" TypeName="list_dataTableAdapters.ModelsTableAdapter">
<SelectParameters>
<asp:ControlParameter ControlID="list_make" Name="make_id" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
Note that these dropdown lists are in my EditItemTemplate and when the edit button is clicked the SelectedValue is correctly retrieved however when I select a new item in the list_make I get this error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control
Does anyone know why I am getting this error?
Also if it helps I have this code in the list_make_SelectedIndexChanged event:
protected void list_make_SelectedIndexChanged(object sender, EventArgs e)
{
var listView = listview_car.Items[0].FindControl("list_model") as DropDownList;
listView.DataBind();
}

First record in dropdown list will not update the other dropdownbox

I have a dropdown box that is populated from results from a SQL query. The selected value from dropdownlist1 successfully populates the lbAuthors dropdown list. During testing I realized that the first record from dropdownlist1 never updates into the lbAuthors dropdownlist. Here is an example: if I have three authors name in the 2nd dropdown box (Frost, Kipling, Poe) the first name - Frost - does not update into the first dropdown box. Kipling or Poe do - but not Frost.
My question is - What do I need to include in my event to allow Frost (or whatever the first record is) to update into the first dropdown box? –
Code-behind:
protected void update_SelectedItem(object sender, EventArgs e)
{
lbAuthorList.Items.Clear();
lbAuthorList.Items.Add(new ListItem(DropDownList1.SelectedItem.Text, DropDownList1.SelectedItem.Text));
lbAuthorList.Items.FindByValue(DropDownList1.SelectedItem.Text).Selected = true;
}
Markup:
<asp:DropDownList runat="server"
ID="lbAuthors"
style="float:left;"
DataSourceID="odsAuthorList"
DataTextField="DisplayAuthorName" DataValueField="AuthorID"
onselectedindexchanged="lbUserList_SelectedIndexChanged"
AppendDataBoundItems="True" >
</asp:DropDownList>
<asp:DropDownList ID="DropDownList1"
runat="server"
AppendDataBoundItems="True"
DataSourceID="SqlDataSource2"
DataTextField="Display_AuthorName"
EnableViewState="false"
DataValueField="Display_AuthorName"
OnSelectedIndexChanged="update_SelectedItem"
AutoPostBack="true">
</asp:DropDownList>
That is because when you select the first item, selcted index does not change. You need to insert a dummy item like this::
<asp:DropDownList ID="DropDownList1" runat="server"
AppendDataBoundItems="True"
DataSourceID="SqlDataSource2"
DataTextField="Display_AuthorName"
EnableViewState="false"
DataValueField="Display_AuthorName"
OnSelectedIndexChanged="update_SelectedItem"
AutoPostBack="true">
<asp:ListItem Text="--Select One--" Value="-1"></asp:ListItem>
</asp:DropDownList>
This issue rises because u wrote code for dropdownlist changed but initially first value selected at that time dropdownlist changed event not fired. If u choose second and then choose first one it will work fine.
Just add one dummy variable in dropdownlist item..
dropdownlist1.Items.Add("--Select--");

How to load the data in asp:grid after providing filters?

I have to load the data in asp:grid after filtering from two asp:dropdown list. For example following is the dropdown lists:
<asp:DropDownList ID="ddlb" runat="server" AutoPostBack="True"
CssClass="ddlb" Width="100px" DataSourceID="SqlDataSource3"
DataTextField="MonthYearName" DataValueField="MonthYear"
onselectedindexchanged="Changed">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ProdDB %>" SelectCommand="# some sql query">
</asp:SqlDataSource>
<asp:DropDownList ID="asd" runat="server" AutoPostBack="True"
CssClass="ddlb" Width="100px" DataSourceID="SqlDataSource4"
DataTextField="MonthYearName" DataValueField="MonthYear"
onselectedindexchanged="SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:ProdDB %>" SelectCommand="# some queries">
</asp:SqlDataSource>
Now, When i am loading the page. It shows the above defined dropdown list. What i want to know is "how should i load the data in aspx grid using the above two dropdown filter ?".
For example i choose A option from the first dropdown then it filter the result on the basis of it and the data will display on grid and so on...
Please tell me how should i connect the grid with these two dropdown list ?
How should i filter it ?
Place a grid and two drop down on the page.
Place an SQL Data Source
Provide two parameters to the sql datasource select command
Attached that sqldatasource to the gridview
Bind the two parameters of the SQLDataSource to the the two Dropdown boxes.

Categories