Click here to Skip to main content
15,845,744 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 everytime I'm gettin 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?

Thank you
Suresh
Posted
Comments
Raja Soosai 3-Jul-13 7:21am    
Show your code pls

Without analysing your code it is almost impossible to solve your problem as we can't read your mind
but to bind a drop down you need
C#
DataSource,DataTextField,DataValueField
something like 
        ddlABC.Items.Add(new ListItem("--Please Select--", "0"));
        ddlABC.DataSource = <a list="" or="" datasource="">;
        ddlABC.DataTextField = "SomeName";
        ddlABC.DataValueField = "SomeID";
        ddlABC.DataBind();
 
Share this answer
 
v2
ddl.Datasource=yourtablename(datatable);
ddl.DataTextField="tablecolumn name";
ddl.DataBind();
 
Share this answer
 
This means that on each postback your dropdown is getting rebinded. Bind your DropDownList in Not IsPostBack block of your Page_Load event. Try like this:
C#
if(!IsPostBack){
    DropDownList1.DataSource = YourDataSource;
    DropDownList1.DataTextField = "EmpName";
    DropDownList1.DataValueField = "Dept";
    DropDownList1.DataBind();
}


--Amit
 
Share this answer
 
Comments
Suri Rayal 3-Jul-13 8:50am    
I did bind drop down list within if(!IsPostBack){} only,
_Amy 3-Jul-13 23:05pm    
Then somewhere you are rebinding the dropdown.. Can I see your code? Is it a user control?
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..
SQL
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();
            }
        }

 
Share this answer
 
Comments
The Doer 18-Jul-13 2:08am    
show your code for Selected Change event, your binding has no error

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