why is gridview not showing data on slecting a dropdown value RRS feed

  • Question

  • User186897 posted


    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
            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()
                            GridView1.DataSource = dt
                        End Using
                    End Using
                End Using
            End Using
        End Sub
            <asp:DropDownList ID="DropDownList1" runat="server" autopostback="true" CssClass="auto-style2" DataSourceID="SqlDataSource1" DataTextField="Country" DataValueField="Country">
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>" SelectCommand="SELECT [Country] FROM [Sites]"></asp:SqlDataSource>
                <asp:Button ID="Button1" autopostback="true" runat="server" CssClass="auto-style1" style="z-index: 1" Text="Button" />
                <asp:Button ID="Button2" runat="server" CssClass="auto-style4" Text="send email"  />
            <asp:GridView ID="GridView1" runat="server" CssClass="auto-style3" AutoGenerateColumns="False">
                    <asp:CheckBox ID="chkSelect" runat="server" />
            <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
            <asp:BoundField DataField="SiteName" HeaderText="Name" ItemStyle-Width="150" />
            <asp:TemplateField HeaderText="Email">
                    <asp:HyperLink ID="lnkEmail" runat="server" Text='<%# Eval("Email") %>'  />

    Friday, May 20, 2016 10:51 AM


All replies

  • User3690988 posted

    I think that there are two reasons:

    1. I think that your PostBack logic is backwards.  I would Bind if it is a PostBack.  Your logic will only load the GridView once.
    2. 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)


    Working With DropDownList SelectedIndexChanged Event

    Gridview DropDownList Selected Index Changed Event & Get GridView Row Index

    Best Regards,


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 21, 2016 2:45 AM