Click here to Skip to main content
15,039,359 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone,

this is my code where when user selects image_id and logo_id from two different dropdownlist...accordingly images and logos will be displayed on two gridviews...but when user is changing data in dropdownlist the gridview doesnot reflects the change

C#
public partial class USER_SEND_WATERMARK_REQUEST : System.Web.UI.Page
{
    String uName;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["uName"] != null)
        {
            Label2.Text = Request.QueryString["uName"];
            uName = Request.QueryString["uName"];
        }
        if (!IsPostBack)
        {
            dropdownlist1bind();
            dropdownlist2bind();
            BindGridView(sender,e);
            BindGridView2(sender,e);
        }
       
    }
    protected void LinkButton7_Click1(object sender, EventArgs e)
    {
        Response.Redirect("USER_HOME.aspx?uName="+uName);
    }
    protected void LinkButton6_Click1(object sender, EventArgs e)
    {
        Response.Redirect("LOGIN.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
     public void  dropdownlist1bind()
    {
        try
        {
           SqlConnection con= new SqlConnection(ConfigurationManager.ConnectionStrings["WATERMARKING"].ToString());
           SqlCommand cmd=new SqlCommand();

            cmd.CommandText = "Select IMAGE_ID from IMAGE where USERNAME = '"+ uName +"' ";
            cmd.Connection = con;
            con.Open();

            DropDownList1.DataSource = cmd.ExecuteReader();
            DropDownList1.DataTextField = "IMAGE_ID";
            DropDownList1.DataBind();
            
        }
        catch (Exception ex)       
        {
            Label11.Text="error";
        }   

    }
    public void dropdownlist2bind()
    {
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["WATERMARKING"].ToString());
            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "Select LOGO_ID from LOGO where USERNAME = '" + uName + "' ";
            cmd.Connection = con;
            con.Open();

            DropDownList2.DataSource = cmd.ExecuteReader();
            DropDownList2.DataTextField = "LOGO_ID";
            DropDownList2.DataBind();

        }
        catch (Exception ex)
        {
            Label12.Text = "error";
        }

    }

    public void BindGridView(object sender, EventArgs e)
    {
        DropDownList ss = DropDownList1;
        int i;
        if (int.TryParse(ss.SelectedValue, out i))
        {
            ConnectionStringSettings set = ConfigurationManager.ConnectionStrings["WATERMARKING"];
            String cns = set.ConnectionString;
            SqlConnection con = new SqlConnection(cns);
            con.Open();
            string sql = "Select IMAGE_CONTENT,IMAGE_ID from IMAGE where USERNAME='" + uName + "' and IMAGE_ID= '" + i + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, cns);
            DataTable dt = new DataTable();
            da.Fill(dt);
            gvCheckboxes.DataSource = dt;
            gvCheckboxes.DataBind();
           
 
            
        }
        else
        {
            Label13.Text = "no IMAGE selected";
        }
        
    }


    public void BindGridView2(object sender, EventArgs e)
    {
        DropDownList ss = DropDownList2;
       int i;
       if (int.TryParse(ss.SelectedValue, out i))
       {
           ConnectionStringSettings set = ConfigurationManager.ConnectionStrings["WATERMARKING"];
           String cns = set.ConnectionString;
           SqlConnection con = new SqlConnection(cns);
           con.Open();
           string sql = "Select LOGO_CONTENT,LOGO_ID from LOGO where USERNAME='" + uName + "' and LOGO_ID= '" + i + "'";
           SqlDataAdapter da = new SqlDataAdapter(sql, cns);
           DataTable dt = new DataTable();
           da.Fill(dt);
           GridView2.DataSource = dt;
           GridView2.DataBind();
       }
       else
       {
           Label14.Text = "no LOGO selected";
       }

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList1.SelectedIndexChanged += new EventHandler(BindGridView);
        
    }
    protected void gvCheckboxes_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList2.SelectedIndexChanged += new EventHandler(BindGridView2);
    }
}


[edition] Added pre tags
Posted
Updated 13-Feb-11 1:20am
v3

1 solution

I don't know why have you made things so complex, when a simple implementation would had worked.
Currently, out here:
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)    
{        
   DropDownList1.SelectedIndexChanged += new EventHandler(BindGridView);            
}

You are just attaching an event handler to the SelectedIndexChanged evevt and nothing else. What actually you wanted/inted to do was to call BindGridView event (and rebind data to grid) whenever there is a change in selected index. Thus, your code does not work as you expect it too.

Try something simpler like:
C#
   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dropdownlist1bind();
            dropdownlist2bind();
            BindGridView("");
            BindGridView2("");
        }       
    }

    // Similar implementation for BindGridView method 
    public void BindGridView2(string currentSelectedValue)
    {
       // Keeping 'i' intact as it looks like you are using some logic to get its value 
       // and then use it in query to fetch data
       int i;
       if (int.TryParse(currentSelectedValue, out i))
       {
           ConnectionStringSettings set = ConfigurationManager.ConnectionStrings["WATERMARKING"];
           String cns = set.ConnectionString;
           SqlConnection con = new SqlConnection(cns);
           con.Open();
           string sql = "Select LOGO_CONTENT,LOGO_ID from LOGO where USERNAME='" + uName + "' and LOGO_ID= '" + i + "'";
           SqlDataAdapter da = new SqlDataAdapter(sql, cns);
           DataTable dt = new DataTable();
           da.Fill(dt);
           GridView2.DataSource = dt;
           GridView2.DataBind();
       }
       else
       {
           Label14.Text = "no LOGO selected";
       }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridView(DropDownList1.SelectedValue);        
    }
    
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridView2(DropDownList2.SelectedValue);
    }
}
   
Comments
Espen Harlinn 14-Feb-11 10:15am
   
Good answer
Sandeep Mewara 14-Feb-11 13:45pm
   
Thanks again. :)

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