Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I've a dropdown list with datasource like
empname dept
-----------------
Rahul 02
martin 01
sachin 02

i've binded this table to my dropdown list and when i try to access dept in dropdown selected event change every time I'm getting Rahul only. even i selected sachin.

Whats the problem? What I assume is I've two common values in my datavalue field (02 and 02) is this a problem?

Please check my code shown below..

C#
if (!IsPostBack)
       {
 try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("toget_emp", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ddlReason.DataSource = ds.Tables[0];
                    ddlReason.DataTextField = "empname";
                    ddlReason.DataValueField = "dept";
                    ddlReason.DataBind();
                    ddlReason.Items.Insert(0, "--Select--");
                }
            }
            catch (SqlException ex)
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "alert", "javascript:alert('" + ex.Message + "')", true);
            }
            finally
            {
                con.Close();
            }
        }
Posted
Updated 3-Jul-13 3:08am
v2
Comments
[no name] 3-Jul-13 9:02am    
How is this any different than the exact same question that you already asked, http://www.codeproject.com/Questions/615222/Dropdown-problem-with-common-values?
jaideepsinh 3-Jul-13 9:05am    
Why you not use primary key set primary key to DataValueField .

Write code like this


ddlReason.DataBind();
ddlReason.Items.Insert(0, "--Select--";);
ddlReason.Items[0].value="0";
 
Share this answer
 
Yes ,It would be the problem but you can use DropDownList's SelectedIndex property and catch selected value and text in DropDownList onselectedindexchanged="ddlReason_SelectedIndexChanged" to get selected text field even you have same value in value field ,But it's suggested to use unique value in DataValueField.

C#
protected void ddlReason_SelectedIndexChanged(object sender, EventArgs e)
  {
      DropDownList ddl = (DropDownList)sender;
      int index = ddl.SelectedIndex;
      string Text = ddl.SelectedItem.ToString();
      string Value = ddl.SelectedValue;

  }
 
Share this answer
 
You can use Rahul id as 02 and sachin id as 002 or

Dont use deptid u should use empid which is unique for all records...
 
Share this answer
 

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