Answered by:
why is gridview not showing data on slecting a dropdown value

Question
-
User186897 posted
Hello
I have this aspx page and vb page...on selecting a value in dropdownlist i am unable to see any values in the grid?
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Me.BindGrid() End If End Sub Private Sub BindGrid() Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString Using con As New SqlConnection(constr) Using cmd As New SqlCommand("SELECT Id,SiteName,Email FROM Sites where Country=' " & DropDownList1.SelectedValue & "'") Using sda As New SqlDataAdapter() cmd.Connection = con sda.SelectCommand = cmd Using dt As New DataTable() sda.Fill(dt) GridView1.DataSource = dt GridView1.DataBind() End Using End Using End Using End Using End Sub
<div> <asp:DropDownList ID="DropDownList1" runat="server" autopostback="true" CssClass="auto-style2" DataSourceID="SqlDataSource1" DataTextField="Country" DataValueField="Country"> </asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>" SelectCommand="SELECT [Country] FROM [Sites]"></asp:SqlDataSource> <p> <asp:Button ID="Button1" autopostback="true" runat="server" CssClass="auto-style1" style="z-index: 1" Text="Button" /> </p> <p> <asp:Button ID="Button2" runat="server" CssClass="auto-style4" Text="send email" /> </p> <asp:GridView ID="GridView1" runat="server" CssClass="auto-style3" AutoGenerateColumns="False"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chkSelect" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" /> <asp:BoundField DataField="SiteName" HeaderText="Name" ItemStyle-Width="150" /> <asp:TemplateField HeaderText="Email"> <ItemTemplate> <asp:HyperLink ID="lnkEmail" runat="server" Text='<%# Eval("Email") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div>
Friday, May 20, 2016 10:51 AM
Answers
-
User-2057865890 posted
Hi Lexi85,
on selecting a value in dropdownlist i am unable to see any values in the grid?You should use a SelectedIndexChanged event of a dropdownlist control by binding it with a database.
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged"> protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { ... }
reference:
Working With DropDownList SelectedIndexChanged Event
Gridview DropDownList Selected Index Changed Event & Get GridView Row Index
Best Regards,
Chris
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 21, 2016 2:45 AM
All replies
-
User3690988 posted
I think that there are two reasons:
- I think that your PostBack logic is backwards. I would Bind if it is a PostBack. Your logic will only load the GridView once.
- Your SQL is inserting a space in the Country. Change it to:
Using cmd As New SqlCommand("SELECT Id,SiteName,Email FROM Sites where Country='" & DropDownList1.SelectedValue & "'")
(Get rid of the space in ... where Country=' ")
^Friday, May 20, 2016 2:51 PM -
User-2057865890 posted
Hi Lexi85,
on selecting a value in dropdownlist i am unable to see any values in the grid?You should use a SelectedIndexChanged event of a dropdownlist control by binding it with a database.
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged"> protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { ... }
reference:
Working With DropDownList SelectedIndexChanged Event
Gridview DropDownList Selected Index Changed Event & Get GridView Row Index
Best Regards,
Chris
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 21, 2016 2:45 AM