moe
moe

Reputation: 5249

DropDownList showing duplicate values in asp.net using C#

I have only Yes and No as my item lists in my drop down but if i select "No" 3 times then it shows in my drop down list 3 times so it is duplicating each selection i make from the drop down. After the selection it calls stored procedure which updates the back-end to either Yes or No based on the selection. I am confused here so please help. thanks Here is my ASPX code for the DD

<asp:DropDownList ID="My_DD" runat="server" 
              AutoPostBack="true" DataTextField="ItemCompleted" AppendDataBoundItems="true" OnSelectedIndexChanged="DD_Changed" 
              DataValueField="ItemCompleted">
              <asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
                        <asp:ListItem Text="No" Value="No"></asp:ListItem>
          </asp:DropDownList>

Here is the code behind:

protected void DD_Changed(object sender, EventArgs e)
    {
        DropDownList My_DD = (DropDownList)sender;

        this.Bind_DD();

    }   

    protected void Bind_DD()
    {

        string myVar;
        myVar= My_DD.SelectedValue.ToString();
        string ID;
        ID = Request.QueryString["ID"];
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        SqlCommand cmd = new SqlCommand("sp_DD");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@myVAr", SqlDbType.VarChar).Value = myVar;
        cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID;
        cmd.Connection = con;
        sda.SelectCommand = cmd;

        sda.Fill(dt);
        My_DD.DataSource = dt;

        My_DD.DataBind();

    }

Page Load code here:

 protected void Page_Load(object sender, EventArgs e)
    {
        string Post_ID = Page.RouteData.Values["Post_ID"] as string;

        if (!IsPostBack)
        {
            BindData();
            BindData_ActionItems();

            //load DV_New
            LoadDetailView_New();       

        }

    }

Upvotes: 0

Views: 5050

Answers (1)

Ahmed Alaa El-Din
Ahmed Alaa El-Din

Reputation: 1833

what happens from code as i understand : you click Yes/No and then rebind data inside the same dropdown list with values that comes from db. i don't get the error but you can try to reclear dropdownlist before re-adding the items from the db

protected void Bind_DD()
{

    string myVar;
    myVar= My_DD.SelectedValue.ToString();
    string ID;
    ID = Request.QueryString["ID"];
    DataTable dt = new DataTable();
    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    SqlDataAdapter sda = new SqlDataAdapter();
    SqlCommand cmd = new SqlCommand("sp_DD");
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@My_DD", SqlDbType.VarChar).Value = My_DD;
    cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID;
    cmd.Connection = con;
    sda.SelectCommand = cmd;

    sda.Fill(dt);

    My_DD.Items.Clear();

    My_DD.DataSource = dt;
    My_DD.DataBind();

}

Upvotes: 1

Related Questions