Drop down not retrieved data from database - c#

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"

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

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

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

how to show details of user into gridview where 1st girdview contain list of link button

I have 2 grid-view on the same page
1st contain the link button where text on the link button is the name of the user comes from database to show user specific details.
2nd grid-view to show the selected user details
Now when user click on link from first grid-view then it can view the details of that user in second grid-view.
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="ID" AutoGenerateSelectButton="true" AllowPaging="True" pagesize ="5" />
<br /><b><u>Store Details</u></b><br /><br />
<asp:DetailsView id="DetailsView1" DataSourceID="SqlDataSource2"
DataKeyNames="ID" AllowPaging ="true" Runat="server" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestingConnectionString %>"
SelectCommand="select * from UserDetails" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:TestingConnectionString %>"
SelectCommand="select * from UserDetails WHERE ID=#ID" >
<SelectParameters>
<asp:ControlParameter Name="ID" ControlID="GridView1" />
</SelectParameters>
</asp:SqlDataSource>
By using above code i get what i want but i have one more query i.e. i want to use link button because i also using url rewriting so for that i need to link button for every user so that i can change my url also for that user
Thanks in advance
here are two Master/Detail Example with asp.net
http://www.vkinfotek.com/detailsview/gridview-detailsview-master-detail-example.html
http://www.codeproject.com/Articles/16779/GridView-DetailsView-Master-Detail-Control

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