GridView will not display when button is clicked - asp.net

I have an ASP.NET page I am writing in VB, and I am trying to only have the gridView show up when the button is clicked, and that it should display the product id, product name, supplier name, and quantity purchased for all products purchased by the customer (which comes from the drop down list). For some reason that I am unable to figure out, no matter what I do the gridView does not display. Ever. Any help would be greatly appreciated.
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [CompanyName] FROM [Customers] ORDER BY [CompanyName]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT Customers_1.CustomerID, Customers_1.CompanyName, (SELECT COUNT(Orders.OrderID) AS Expr1 FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID WHERE (Customers.CompanyName = #p1)) AS [Number of Orders], SUM([Order Details].UnitPrice * [Order Details].Quantity - [Order Details].Discount) AS [Total Cost of Order] FROM Customers AS Customers_1 INNER JOIN Orders AS Orders_1 ON Customers_1.CustomerID = Orders_1.CustomerID INNER JOIN [Order Details] ON Orders_1.OrderID = [Order Details].OrderID WHERE (Customers_1.CompanyName = #p1) GROUP BY Customers_1.CustomerID, Customers_1.CompanyName">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="p1" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT Products.ProductID, Products.ProductName, Suppliers.CompanyName, [Order Details].Quantity FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID INNER JOIN Products ON [Order Details].ProductID = Products.ProductID INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID WHERE (Customers.CustomerID = #p1)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="p1" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="CompanyName" DataValueField="CompanyName">
<asp:ListItem>Select A Customer</asp:ListItem>
</asp:DropDownList>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="CustomerID" DataSourceID="SqlDataSource2" Height="50px" Width="125px">
<EditRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<Fields>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="Number of Orders" HeaderText="Number of Orders" ReadOnly="True" SortExpression="Number of Orders" />
<asp:BoundField DataField="Total Cost of Order" HeaderText="Total Cost of Order" ReadOnly="True" SortExpression="Total Cost of Order" />
</Fields>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
</asp:DetailsView>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ProductID" DataSourceID="SqlDataSource3" EmptyDataText="N/A" Visible="False">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" Text="Order Summary" />
</div>
</form>
</body>
</html>
And my code-behind page looks like this:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GridView1.Visible = True
GridView1.DataSourceID = "SqlDataSource3"
GridView1.
End Sub
End Class
Any help would be greatly appreciated

After setting the DataSource or DataSourceID property of the GridView you need to call DataBind() as a final step to bind your grid to the data.

Related

CheckBox Field does not show checked or not in gridview

i'm used checkbox field in gridview to show bit value from database on my gridview
i used default editing row on gridview , when i update row (checked checkbox field)
in database update successfully but in gridview does not show checked or not checked
when i refresh page not show again or when i rerun page same problem as a whole does not show checkbox field bit value status check box filed always is empty and not checkedthis is my gridview html codes
<asp:SqlDataSource ID="sqldsReplys" runat="server" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' UpdateCommand="MNG_Forum_Update_Reply_Status" UpdateCommandType="StoredProcedure" SelectCommand="MNG_Forum_Select_Replys" SelectCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="id" Type="Int64" />
<asp:Parameter Name="PostStatus" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="grdReplys" runat="server" CssClass="GridViewStyle" DataKeyNames="id" AutoGenerateColumns="False" AllowPaging="True" Width="100%" DataSourceID="sqldsReplys">
<Columns>
<asp:CommandField ShowSelectButton="True" ButtonType="Image" SelectImageUrl="~/cpanel/assets/img/sel.png">
</asp:CommandField>
<asp:BoundField DataField="id" HeaderText="ردیف" ReadOnly="true" />
<asp:BoundField DataField="Ptitle" HeaderText="عنوان مطلب" ReadOnly="true" />
<asp:CheckBoxField DataField="PostStatus" HeaderText="وضعیت نمایش" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnDelete" runat="server" CommandName="delete" ToolTip="حذف این رکورد"
OnClientClick="return confirm('آیا مطمئن هستید؟')" ImageUrl="~/cpanel/assets/img/Trash.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" EditText="ویرایش" UpdateText="تائید" CancelText="لغو" />
</Columns>
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
</asp:GridView>

Need a Gridview to Change when DropDownList changes

I have a dropdownlist in ASP.Net (I am using VB.Net) and my ASP.Net code follows. What I would like to do is have that Girdview populate dynamically off of the DropDownList. If the first option (showing nothing) is chosen, I would like everything possible in the Gridview to show. If one of the other options is chosen, I would like the GridView to filter based on that option. I can see the DropDownList, but nothing shows up in the Gridview. I imagine the issue has to do with binding the DropDownList to the Gridview somehow.
Thanks in advance.
<%# Page Title="Home Page" Language="VB" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="SAP._Default" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Tab Container Tips & Tricks</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<cc1:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0">
<cc1:TabPanel ID="TabPanel1" runat="server" HeaderText="Screening">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Pull For Screening "></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server"><asp:ListItem>10</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Add Applicants" />
<asp:Label ID="Label3" runat="server" Text="Choose Campus: "></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem Text="All" Value="%"></asp:ListItem>
<asp:ListItem>RC</asp:ListItem>
<asp:ListItem>HS</asp:ListItem>
<asp:ListItem>TL</asp:ListItem>
<asp:ListItem>PH</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ScreenerView %>" ProviderName="<%$ ConnectionStrings:ScreenerView.ProviderName %>"
SelectCommand="SELECT [ScreenerID], [LEGAL_FNAME], [LEGAL_LNAME], [APPL_PERSON_ID], [AAMC_ID], [GENDER], [URM], [RecMCAT], [SciGPA], [LEGAL_RES_STATE_CD], [PullDate], [First] FROM [vw_Screener]"
FilterExpression="Screener ID LIKE '?' AND First LIKE '?'">
<SelectParameters>
<asp:Parameter Name="intS" />
</SelectParameters>
<FilterParameters>
<asp:ControlParameter ControlID="DropDownList2" PropertyName="SelectedValue" />
</FilterParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ScreenerID" HeaderText="ScreenerID" SortExpression="ScreenerID" Visible="False"></asp:BoundField>
<asp:BoundField DataField="LEGAL_FNAME" HeaderText="First Name" SortExpression="LEGAL_FNAME" />
<asp:BoundField DataField="LEGAL_LNAME" HeaderText="Last Name" SortExpression="LEGAL_LNAME" />
<asp:BoundField DataField="APPL_PERSON_ID" HeaderText="APPL_PERSON_ID" SortExpression="APPL_PERSON_ID" Visible="False" />
<asp:BoundField DataField="AAMC_ID" HeaderText="AAMC" SortExpression="AAMC_ID" />
<asp:BoundField DataField="GENDER" HeaderText="Gender" SortExpression="GENDER" />
<asp:BoundField DataField="URM" HeaderText="URM" SortExpression="URM" />
<asp:BoundField DataField="RecMCAT" HeaderText="MCAT" SortExpression="RecMCAT" />
<asp:BoundField DataField="SciGPA" HeaderText="Sci. GPA" SortExpression="SciGPA" />
<asp:BoundField DataField="LEGAL_RES_STATE_CD" HeaderText="State" SortExpression="LEGAL_RES_STATE_CD" />
<asp:BoundField DataField="PullDate" HeaderText="Date Pulled" DataFormatString = "{0:d}" SortExpression="PullDate" />
<asp:BoundField DataField="First" HeaderText="First Choice CC" SortExpression="First" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</ContentTemplate>
</cc1:TabPanel>
You need to fix your FilterExpression and FilterParements in your SQLDataSource:
In your ASPX File you need to make the following changes:
FilterParameters: The items in this list will be used by the FilterExpression. If this is emtpy then your FilterExpression will have nothing to work with. However, you have ONE item in there, now you must link it to the FilterExpression with its index. Since you have ONE item then its index will be 0 (see filterexpression below). I assume its the [ScreenerID] so its probably an Int32? Then you need to specify that and set a defaultvalue:
<FilterParameters>
<asp:ControlParameter Name="ScreenerID" ControlID="DropDownList2"
PropertyName="SelectedValue" Type="Int32" DefaultValue="" />
</FilterParameters>
FilterExpression: The way you have it now is not correct because it is NOT linked with any real value say User-Interace (Dropdown) . You must link it based on its index/position in the FilterParameter items. Like we said above, since ScreenerID is the first item then it has index 0:
FilterExpression="ScreenerID = {0}"
DropDownList2: Since you already have AutoPostBack="True" you just need add event OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"
If ScreenID is an integer, you need to remove the % from the value field in the ALL item like so: <asp:ListItem Value="" Text="ALL>
VB.NET Code-Behind: In your DropDownList2_SelectedIndexChanged event dont put any code in it. Like so:
Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
'Leave empty
End Sub
Let me know if this is not working for you, I can update this answer as I find out more from you.

Pass a session variable to filterParameter in ASP.Net

I am working in ASP.Net. I am attempting to pass a session variable to a gridview control. When I remove the session variable parameter, the dropdownlist I am using to feed the gridview works correctly. When I add the session variable, it shows everything.
Here is my code behind for the page load event (I am using VB.net):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim ScreenerID As Int32
Dim intS As Int32
Session("ScreenerID") = 20
intS = CType(Session.Item("ScreenerID"), Int32)
End Sub
My ASP.Net code is as follows:
<%# Page Title="Home Page" Language="VB" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="SAP._Default" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Screening Portal</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<cc1:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0">
<cc1:TabPanel ID="TabPanel1" runat="server" HeaderText="Screening">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Pull For Screening "></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server"><asp:ListItem>10</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Add Applicants" />
<asp:Label ID="Label3" runat="server" Text="Choose Campus: "></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem Text="All" Value=""></asp:ListItem>
<asp:ListItem>CRC</asp:ListItem>
<asp:ListItem>GHS</asp:ListItem>
<asp:ListItem>STL</asp:ListItem>
<asp:ListItem>WPH</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ScreenerView %>" ProviderName="<%$ ConnectionStrings:ScreenerView.ProviderName %>"
SelectCommand="SELECT [ScreenerID], [LEGAL_FNAME], [LEGAL_LNAME], [APPL_PERSON_ID], [AAMC_ID], [GENDER], [URM], [RecMCAT], [SciGPA], [LEGAL_RES_STATE_CD], [PullDate], [First] FROM [vw_Screener]"
FilterExpression="ScreenerID = {0} AND First='{1}'">
<FilterParameters>
<asp:Parameter Name="intS" Type="int32" />
<asp:ControlParameter ControlID="DropDownList2" PropertyName="SelectedValue" Type="String" DefaultValue=""/>
</FilterParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ScreenerID" HeaderText="ScreenerID" SortExpression="ScreenerID" Visible="False"></asp:BoundField>
<asp:BoundField DataField="LEGAL_FNAME" HeaderText="First Name" SortExpression="LEGAL_FNAME" />
<asp:BoundField DataField="LEGAL_LNAME" HeaderText="Last Name" SortExpression="LEGAL_LNAME" />
<asp:BoundField DataField="APPL_PERSON_ID" HeaderText="APPL_PERSON_ID" SortExpression="APPL_PERSON_ID" Visible="False" />
<asp:BoundField DataField="AAMC_ID" HeaderText="AAMC" SortExpression="AAMC_ID" />
<asp:BoundField DataField="GENDER" HeaderText="Gender" SortExpression="GENDER" />
<asp:BoundField DataField="URM" HeaderText="URM" SortExpression="URM" />
<asp:BoundField DataField="RecMCAT" HeaderText="MCAT" SortExpression="RecMCAT" />
<asp:BoundField DataField="SciGPA" HeaderText="Sci. GPA" SortExpression="SciGPA" />
<asp:BoundField DataField="LEGAL_RES_STATE_CD" HeaderText="State" SortExpression="LEGAL_RES_STATE_CD" />
<asp:BoundField DataField="PullDate" HeaderText="Date Pulled" DataFormatString = "{0:d}" SortExpression="PullDate" />
<asp:BoundField DataField="First" HeaderText="First Choice CC" SortExpression="First" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel ID="TabPanel2" runat="server" HeaderText="Query">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Search "></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel ID="TabPanel3" runat="server" HeaderText="Manage Invitations">
<ContentTemplate>
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel ID="TabPanel4" runat="server" HeaderText="Reports">
<ContentTemplate>
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel ID="TabPanel5" runat="server" HeaderText="Admin">
<ContentTemplate>
</ContentTemplate>
</cc1:TabPanel>
</cc1:TabContainer>
<br />
</div>
</form>
</body>
</html>
I imagine I am doing something simple that needs a quick fix, but it is beyond me. Thank you.
Since intS doesn’t change much, why don’t you feed it into the select statement instead of applying a filter parameter? what do you think?
ASPX SqlDataSource1:
SelectCommand="SELECT [ScreenerID], [LEGAL_FNAME], [LEGAL_LNAME],
[APPL_PERSON_ID], [AAMC_ID], [GENDER], [URM], [RecMCAT], [SciGPA], [LEGAL_RES_STATE_CD],
[PullDate], [First]
FROM [vw_Screener]
WHERE [First] = #First" <-- Notice the WHERE Clause
Remove <asp:Parameter Name="intS" Type="int32" /> from FilterParameters and then change your FilterExpression to:
FilterExpression="First='{0}'"
But you must add [First] as a parameter in the SelectParameters section like so:
<SelectParameters>
<asp:Parameter Name="First" />
</SelectParameters>
VB.NET Code-Behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim ScreenerID As Int32
Dim intS As Int32
Session("ScreenerID") = 20
intS = CType(Session.Item("ScreenerID"), Int32)
SqlDataSource1.SelectParameters.Add("#First ",intS) '<-- Here is where we set intS
End Sub

ASP.NET/VB dropdownlist not properly populating gridview

I'm rather new to ASP.NET, but I have to create a .aspx form with VB that when selecting a customer from the ddl, it will populate gridview with the customer's orders from an Access database.
So far, my problem is that when I try and select any other customer than the first one, the gridview doesn't repopulate with new information. Rather it stays stuck on the first customer's information.
Here's what I have so far:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="Technical_Challenge.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Technical Challenge</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Customers"></asp:Label>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="CustomerName" DataValueField="CustomerID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TechChallengeConnectionString %>" ProviderName="<%$ ConnectionStrings:TechChallengeConnectionString.ProviderName %>" SelectCommand="SELECT [CustomerID], [CustomerName] FROM [Customers] ORDER BY [CustomerName]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TechChallengeConnectionString %>" ProviderName="<%$ ConnectionStrings:TechChallengeConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Orders] WHERE ([CustomerID] = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="CustomerID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:Label ID="CustomerOrdersLabel" runat="server" Text="Customer Orders"> </asp:Label>
<br />
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="OrderID" DataSourceID="SqlDataSource2" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True" SortExpression="OrderID" />
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" />
<asp:BoundField DataField="OrderPrice" HeaderText="OrderPrice" SortExpression="OrderPrice" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
</form>
</body>
</html>
The code-behind:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub SqlDataSource1_Selecting(sender As Object, e As SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
End Sub
End Class
Any help would be greatly appreciated. Thanks!
I don't see either AutoPostBack=True set in your dropdown list or a submit button that would send the data for processing? or you haven't copied that code?
You have to make changes in your code as per below :-
.aspx changes :-
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="CustomerName" DataValueField="CustomerID" AutoPostBack="True">
</asp:DropDownList>
The Code-Behind :-
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
// write your query to select data from database
// bind with grid view
End Sub

dropdown list wont populate values from sql database

I've been strugglng with this one problem for a while now. I have an application as:
<asp:SqlDataSource runat="server" ID="categoriesDataSource"
SelectCommand="SELECT [CategoryID], [Name], [UserId] FROM [Categories] WHERE ([UserId] = #UserId) ORDER BY [Name]">
<SelectParameters>
<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters>
</asp:SqlDataSource>
Pick a category:
<asp:DropDownList ID="categories" runat="server" AutoPostBack="True"
DataSourceID="categoriesDataSource" DataTextField="Name"
DataValueField="CategoryID" AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value="">-- All Categories --</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="picturesDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT PictureID, Title, UploadedOn
FROM Pictures
WHERE UserId = #UserId AND
(CategoryID = #CategoryID OR #CategoryID IS NULL)
ORDER BY UploadedOn DESC" CancelSelectOnNullParameter="False">
<SelectParameters>
<asp:QueryStringParameter Name="UserId" QueryStringField="ID" />
<asp:ControlParameter ControlID="categories" Name="CategoryID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="PictureID" DataSourceID="picturesDataSource" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="PictureID"
DataNavigateUrlFormatString="~/Photodetail.aspx?ID={0}" Text="View Comments" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="UploadedOn" HeaderText="Date Added"
SortExpression="UploadedOn" />
<asp:ImageField DataImageUrlField="PictureID"
DataImageUrlFormatString="~/UploadedImages/{0}.jpg" HeaderText="Image">
<ControlStyle Width="100px" />
</asp:ImageField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
When i take away the
<SelectParameters>
<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters>
and ([UserId] = #UserId), the dropdown list control will populate values from the database but when I leave those 2, it won't populate any value from the database.
When I remove the
<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters>,
and leave ([UserId] = #UserId), I get the error:
Must declare the scalar variable "#UserId".
Can somebody please help me out?
Thanks in advance
The one I posted here in your previous thread is definitely working.
I think you might have missed out the "ID" querystring in your url.
Are you browsing to "http://localhost:1234/WebAppName/PageName.aspx?=12"? (assume the auto generated port number is 1234 and the ID is 12)
It will throw error if you don't specify "?ID=12".
public void bind()
{
sqldataadapter da=new sqldataadapter("select A,B from table",cn);
datatable dt=new datatable();
da.fill(dt);
dropdownlist1.datatextfield="A";
dropdownlist1.datavaluefield="B";
dropdownlist1.datasource=dt;
dropdownlist1.databind();
}

Resources