Click here to Skip to main content
15,886,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
C#
SqlCommand cmd = new SqlCommand("select ID from login order by ID",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.Items.Insert(0, "--Select One--");
DropDownList1.DataTextField = "id";                
DropDownList1.DataBind();

In this, I am not able set the DropDown value to 0 i.e.,"--Select One--"

Please check following snapshot...

http://s7.postimage.org/fxks94fmj/drop.jpg[^]
Posted
v2

Please check the answer Answer to - “DropDownList.SelectedIndex = -1” problem
[^]
Where it says to insert that value after the dropdown is bound...
In your codes do like below...
C#
SqlCommand cmd = new SqlCommand("select ID from login order by ID",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "id";                
DropDownList1.DataBind();

DropDownList1.Items.Insert(0, new ListItem("--Select One--", ""));

Try using this and let me know...

Thanks...
 
Share this answer
 
Comments
PRAKASH_N 22-Nov-12 6:24am    
Hey...itz working....Thank U Very much
Great news....
Anytime, my pleasure to help you.
Thanks for accepting the answer...
You should use AppendDataBoundItems property. Note the correct Code snippets.
C#
SqlCommand cmd = new SqlCommand("select ID from login order by ID", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.AppendDataBoundItems = true;
DropDownList1.Items.Insert(0, new ListItem("--Select One--", "0"));        
DropDownList1.DataSource = ds;        
DropDownList1.DataTextField = "id";
DropDownList1.DataBind();
 
Share this answer
 
v2
Comments
PRAKASH_N 22-Nov-12 6:53am    
working but after updating the table showing some kind of 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