Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a drop-down list and uses the SQL data source for it where I am retrieving the product names from SQL server. Now, I want to add "Please Select a product" Option to the drop-down list.
As far as my knowledge, I add options to drop-down list by using

C#
<asp:ListItem Selected ="true" Value = "1">1</asp:ListItem>

Since, I am not adding the values but retrieving the values from DB, how to achieve this Option additionally and add to it as first position to my drop-down list?

I tried the below code, but not able to do get at first position.Also, each time I am getting extra "please select" option whenever I am selecting other values.

C#
protected void NameDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
     NameDropDownList.Items.Insert(0, new ListItem("Please Select a product", "Please Select a product");
     SqlCommand cmd = new SqlCommand("SELECT ProductID, Price, Description, Rating FROM Product_Info Where Name = '" + NameDropDownList.Text + "'", conn);
     SqlDataReader myReader;
     conn.Open();
     myReader = cmd.ExecuteReader();
     while (myReader.Read()) {
     //Logic
    }
      conn.Close();
      myReader.Close();


Here is my code behind where I bind the data:

C#
<tr>
        <td class="style2">Name</td>
         <td>
               <asp:DropDownList ID="NameDropDownList" runat="server" Height="16px"
                   Width="130px" AutoPostBack="True" DataSourceID="NameSqlDataSource"
                   DataTextField="Name" DataValueField="Name"
                   onselectedindexchanged="NameDropDownList_SelectedIndexChanged">
               </asp:DropDownList>

               <asp:SqlDataSource ID="NameSqlDataSource" runat="server"
                   ConnectionString="<%$ ConnectionStrings:ProductsConnectionString %>"
                   SelectCommand="SELECT [Name] FROM [Product_Info]"></asp:SqlDataSource>
           </td>
   </tr>

I also enables the Auto post back to true.Thanks in advance
Posted
Updated 26-May-15 9:01am
v4

C#
Thanks for your responses. I figured out the actual problem and able to done it in simple step. First, I make the AppendDataBoundItems behavior of my drop-down list To TRUE and kept the following code and it works perfectly.

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           NameDropDownList.Items.Insert(0, new ListItem("Please Select a Product", "Please Select a Product"));
        }

   }
 
Share this answer
 
Try
C#
NameDropDownList.Items.Add("Please Select a product");


If you clear the list first then the "Please Select a Product" will be the first item in the list and then populate the rest of the list.

Personally I would not put help information in the list but put it in on a tooltip or separate label.

This assumes you do not have a bound list..

If data bound use
SQL
NameDropDownList.Items.Insert(0, new ListItem("Please Select a product", "Please Select a product");
 
Share this answer
 
v2
Comments
Member 10392328 26-May-15 12:35pm    
I have a drop-down list where I am retrieving the product names from the data base. So when I select a name, I am successfully able to retrieve other details related to name. Now, I want to add an Option "Please Select" for improvements.
I tried this, but not able to do get at first position. Also, each time I am getting extra "Please select " Option when I select a product.
DropDownList1.Items.Insert(0,"Please Select Options");
 
Share this answer
 
v2
C#
DropDownList1.DataSource = dt;
     DropDownList1.DataTextField = "Name";
     DropDownList1.DataValueField = "ID";
     DropDownList1.DataBind(); 
DropDownList1.Items.Insert(0,"Please Select Options");`
 
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