Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friend


i have a problem using dropdownlist. however code is working but there is a problem when i select an item from the drop down list and the gridview show the result accordingly but dropdownlist repeats its existing data again and again.

Here is a code plz please rectify it
protected void Page_Load(object sender, EventArgs e)
        {
            string cs = ConfigurationManager.ConnectionStrings["LibraryDatabase"].ToString();
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("select * from tblBooks  where Title=@TITLE", con);
                cmd.Parameters.AddWithValue("@TITLE", DropDownList1.SelectedValue);
                SqlCommand cmd1 = new SqlCommand("select Title from tblBooks",con);
                
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                GridView1.DataSource = rdr;
                GridView1.DataBind();
                rdr.Close();
                SqlDataReader rdr1 = cmd1.ExecuteReader();

                while (rdr1.Read() == true)
                {
                    DropDownList1.Items.Add(new ListItem( rdr1["Title"].ToString()));
               }

                rdr1.Close();
            }
        }
Posted
Updated 6-Jul-13 13:02pm
v2
Comments
[no name] 6-Jul-13 17:42pm    
I don't see where you are calling DropDownList1.Items.Clear() anywhere. Have you tried that?

try this code
put this property of gridview

1. EnableViewState="false"
2. AutoPostBack="True"
3. AppendDataBoundItems="True"

then

following code
C#
protected void Page_Load(object sender, EventArgs e)
{ 
    if(!ispostback)
    {
     
        //put your code here  
    
    }
}
 
Share this answer
 
v2
C#
protected void Page_Load(object sender, EventArgs e)
{
    if(!ispostback)
    { 
        //put your code here   
    }
}
 
Share this answer
 
v2

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