Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand
                   ("SELECT CustomerId, Name FROM Customers"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                ddlCustomers.DataSource = cmd.ExecuteReader();
                ddlCustomers.DataTextField = "Name";
                ddlCustomers.DataValueField = "CustomerId";
                ddlCustomers.DataBind();
                con.Close();
            }
        }
        ddlCustomers.Items.Insert(0, new ListItem("", ""));
    }
}


What I have tried:

Dropdownlist items, if first item selected, then first item not display is Dropdownlist?
Posted
Updated 6-Oct-23 8:38am
v3
Comments
Dave Kreskowiak 29-May-23 10:15am    
You're going to have to do more explaining of what you're talking about. What is "customerid1" and how does that relate to the database code you posted?

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