Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I am using two dropdown list in my grid view and while the both dropdown list is in the "--Select-- " and "--Select--",The grid should show all the data,but for me it disappears while selecting a value in ddl before moving to the next ddl for filtering

What I have tried:

C#
 private void BindGrid()
        {
            DataTable dt = new DataTable();
            
            string query = " select * from gvdetails17 WHERE 1=1 ";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            if (ddlAddSalary1.SelectedValue != "")
            {
                gvDetails.Visible = false;
                string[] sal = ddlAddSalary1.SelectedValue.Split('-');
                string from = sal[0];
                string to = sal[1];


                query += " and Sal between  @fromsal and @tosal ";
                cmd.Parameters.AddWithValue("@fromsal", from);
                cmd.Parameters.AddWithValue("@tosal", to);
                
            }

            if (ddlAge.SelectedValue != "")
            {
                gvDetails.Visible = false;
                string[] age = ddlAge.SelectedValue.Split('-');
                string from1 = age[0];
                string to1 = age[1];

                query += " and Age between  @fromage and @toage ";
                cmd.Parameters.AddWithValue("@fromage", from1);
                cmd.Parameters.AddWithValue("@toage", to1);
                gvDetails.Visible = true;
                
            }
            cmd.CommandText = query;
            
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            gvDetails.DataSource = dt;
            gvDetails.DataBind();

        }
protected void ddlAddSalary1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            
            BindGrid();
        }

protected void ddlAge_SelectedIndexChanged(object sender, EventArgs e)
        {
           

            BindGrid();
        }
Posted
Updated 26-Feb-17 19:01pm
Comments
Karthik_Mahalingam 27-Feb-17 0:50am    
what is the value you are getting in "ddlAddSalary1.SelectedValue" during load?
Member 12605293 27-Feb-17 0:55am    
Initially it is empty it shows "" in debugging
Karthik_Mahalingam 27-Feb-17 0:59am    
then it should display all the data
run this query in sql server and check
select * from gvdetails17 WHERE 1=1
Member 12605293 27-Feb-17 1:12am    
Hi Karthik I have addressed this issue by adding one more condition
if (ddlAddSalary1.SelectedValue != "" && ddlAddSalary1.SelectedValue != "--Select--") .It works :)
Karthik_Mahalingam 27-Feb-17 1:17am    
ok fine.. good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900