Saleh Refaai
Saleh Refaai

Reputation: 709

ASP.net Dropdownlist don't return the correct index

I am trying to insert data to SQL server database. But I depend on the selected item in drop down list to insert the corresponding id. when I run the code it works properly but I always get the same index 0. How can I get the correct index of the selected item in drop down list. here is the code of the insert button:

protected void btnInsert_Click(object sender, EventArgs e)
    {
        int x= DropDownList2.SelectedIndex;
        cmd.Connection = con;
        con.Open();
        cmd.CommandText = @"INSERT INTO RatingsAndComments (PID,RaterEmail,RaterName,Rating,Comment) VALUES (@PID,@email,@name,@rating,@comment) ";
        cmd.Parameters.AddWithValue("@PID", ids[x]);
        cmd.Parameters.AddWithValue("@email", txtmail.Text);
        cmd.Parameters.AddWithValue("@name", name.Text);
        cmd.Parameters.AddWithValue("@rating", txtrating.Text);
        cmd.Parameters.AddWithValue("@comment", comment.Text);
        cmd.ExecuteNonQuery();

        con.Close();

    }

and here is the asp code of the drop down list

<asp:DropDownList ID="DropDownList2" runat="server"  Width="130" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
            </asp:DropDownList>

here is the data displayed in the drop down list:

protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();
            string FetchData = "Select * from Physicians";
            cmd = new SqlCommand(FetchData, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();
            sda.Fill(dt);
            ids = new string[dt.Rows.Count];
            names = new string[dt.Rows.Count];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ids[i] = dt.Rows[i][0].ToString();
                names[i] = dt.Rows[i][1].ToString();
            }
            DropDownList2.DataSource = names;
            DropDownList2.DataBind();
            con.Close();
        }

Upvotes: 0

Views: 251

Answers (2)

Coskun Ozogul
Coskun Ozogul

Reputation: 2469

The origin of the problem is your page_load event. You don't check if there is a postback or not. In this case a control like button who postbacks the page on click naturaly will re-fill your combobox and the index will allways be 0.

Try this :

protected void Page_Load(object sender, EventArgs e)
    {

       if (!IsPostBack)
      {
        con.Open();
        string FetchData = "Select * from Physicians";
        cmd = new SqlCommand(FetchData, con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);

        DataTable dt = new DataTable();
        sda.Fill(dt);
        ids = new string[dt.Rows.Count];
        names = new string[dt.Rows.Count];

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ids[i] = dt.Rows[i][0].ToString();
            names[i] = dt.Rows[i][1].ToString();
        }
        DropDownList2.DataSource = names;
        DropDownList2.DataBind();
        con.Close();
     }
}

Another thing, you can configure your dropdownlist like thi :

 <asp:DropDownList ID="DropDownList2" runat="server"  Width="130" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" DataTextField="PhysicianID_Field_In_Your_Physicians_Table" DataValueField="Physician_Name_Field_In_Your_Physicians_Table">
        </asp:DropDownList>

and you can get the selected value of your dropdown list like :

 cmd.Parameters.AddWithValue("@PID", DropDownList2.SelectedValue);

Upvotes: 3

Faraz Babakhel
Faraz Babakhel

Reputation: 664

Try this

DropDownList2.DataSource = names;
DropDownListMonth.DataTextField = i.ToString();
DropDownListMonth.DataValueField = i.ToString();
DropDownList2.DataBind();

Upvotes: 0

Related Questions