Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dropdownlist select only first value not the selected value
This is my code
SqlConnection con = new SqlConnection(cn.ConnectionStrings);
con.Open();
string bind = "SELECT [PriId],[Priority] FROM [WebCrm].[dbo].[tbl_Priority]";
adp = new SqlDataAdapter(bind, con);
DataTable dt = new DataTable();
adp.Fill(dt);
ddlPriority1.DataSource = dt;
ddlPriority1.DataValueField = "PriId";
ddlPriority1.DataTextField = "Priority";
ddlPriority1.DataBind();
ddlPriority1.Items.Insert(0, "Select");
ddlPriority1.SelectedIndex = (0);
con.Close();
Posted
Comments
Krunal Rohit 12-Oct-15 5:39am    
Which value you want to get selected ?

-KR
Radhika20 12-Oct-15 5:53am    
Priority value will get that stored in tbl_Priority
Krunal Rohit 12-Oct-15 7:11am    
Still not clear. Please elaborate your question.

-KR

Make sure you put that code inside of

C#
if (!IsPostBack)
{

}


because you are probably databinding your dropdown everytime the page posts back. That will cause you to lose the selected value.
 
Share this answer
 
If your page getting postback,you have set ddlPriority1.SelectedIndex = (0); in your code.

Try this may be helpful for you.


if (!IsPostBack)
{
SqlConnection con = new SqlConnection(cn.ConnectionStrings);
con.Open();
string bind = "SELECT [PriId],[Priority] FROM [WebCrm].[dbo].[tbl_Priority]";
adp = new SqlDataAdapter(bind, con);
DataTable dt = new DataTable();
adp.Fill(dt);
ddlPriority1.DataSource = dt;
ddlPriority1.DataValueField = "PriId";
ddlPriority1.DataTextField = "Priority";
ddlPriority1.DataBind();
ddlPriority1.Items.Insert(0, "Select");
ddlPriority1.SelectedIndex = (0);
con.Close();
}
 
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