shredder22
shredder22

Reputation: 13

Data won't bind to a gridview based on a dropdown list selection

I currently have two dropdown lists and they are populated with names from a database. What I want to do is have a player be selected, and then on a button click, the data from the table is populated into a gridview. Right now I am just getting a "No data returned" message and I cannot figure out why.

 <asp:DropDownList ID="ddl_QB1"  runat="server" Width="200px" AppendDataBoundItems="True"   
AutoPostBack="True"  Height="16px" DataTextField="Player" DataValueField="id"  ></asp:DropDownList>

<asp:Gridview ID="GridView1" runat="server" AutoGenerateColumns="false" Visible="true" 
                            BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px"   
                            CellPadding="4" GridLines="Horizontal" ShowHeaderWhenEmpty="True" EmptyDataText="No records Found"> 
                             <Columns>   
                                <asp:TemplateField HeaderText="Total Points">  
                                    <EditItemTemplate>  
                                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("[Pts]") %>'>  
                                        </asp:TextBox>  
                                    </EditItemTemplate>  
                                    <ItemTemplate>  
                                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Pts") %>'>  
                                        </asp:Label>  
                                    </ItemTemplate>  

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadQuarterbacks();

        if (!Page.IsPostBack)
        {
            SqlConnection con = new SqlConnection(connectionstring);

            SqlCommand cmd = new SqlCommand("select [id], [Player], [Pts], [Att], [Cmp], [Yds], [TD] from Quarterbacks", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            ddl_QB1.DataSource = dt;
            ddl_QB1.DataBind();
        }


    }
         private void LoadQuarterbacks()
    {
        ddl_QB1.Items.Clear();
        ddl_QB2.Items.Clear();


        SqlConnection con = new SqlConnection(connectionstring);
        SqlCommand cmd = new SqlCommand("SELECT id, Player FROM Quarterbacks", con);

        con.Open();

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet("Quarterbacks");
        da.Fill(ds);  // fill dataset
        ddl_QB1.DataTextField = ds.Tables[0].Columns["Player"].ToString(); // text field name of table dispalyed in dropdown
        ddl_QB2.DataTextField = ds.Tables[0].Columns["Player"].ToString();
        ddl_QB1.DataValueField = ds.Tables[0].Columns["id"].ToString();
        ddl_QB2.DataValueField = ds.Tables[0].Columns["id"].ToString();     // to retrive specific  textfield name 
        ddl_QB1.DataSource = ds.Tables[0];
        ddl_QB2.DataSource = ds.Tables[0];
        ddl_QB2.DataBind();//assigning datasource to the dropdownlist
        ddl_QB1.DataBind();  //binding dropdownlist

        con.Close();
        ddl_QB1.Items.Insert(0, new ListItem("--Select QuarterBack--", "0"));
        ddl_QB2.Items.Insert(0, new ListItem("--Select QuarterBack--", "0"));
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(connectionstring);
        string query = "SELECT [id], [Player], [Pts], [Att], [Cmp], [Yds], [TD] FROM Quarterbacks where id=" + ddl_QB1.SelectedValue;
        SqlDataAdapter sda = new SqlDataAdapter(query,con);
        con.Open();
        DataTable dt = new DataTable();
        sda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


}

}

Upvotes: 1

Views: 348

Answers (1)

mtv
mtv

Reputation: 151

Following 3 changes would give desired results.

First - AutoPostBack should be set to false on the dropdown element as that is causing postback even before you click on the button.

Second - remove the current code inside if(!Page.IsPostback). This code is not necessary. Also, this code is not setting DataTextField and DataValueField properties for ddl_QB1 and ddl_QB2.

Third - put the method call LoadQuarterbacks() inside if(!Page.IsPostback). We do not have to bind these values for ddl_QB1 and ddl_QB2 on each request.

If a refresh is required on the dropdown controls, then call LoadQuarterbacks() method after binding the gridview at the end of the button click. That way you are capturing the selected values from dropdown before rebinding the them. Rebinding the DropDowns would cause them loose the selected values.

Upvotes: 1

Related Questions