Add a blank entry to a dropdownlist filled by a dataset

Posted on 2008-11-07
Last Modified: 2013-12-14
I'm not sure how to add a blank listitem to the top of a dropdownlist that is filled with a strongly typed dataset. Here is what I am using to fill the dropdownlist.

Tried adding the following to the top but was overwritten.

ddlCategories.Items.Add(new ListItem("", "Select Category"));
dsForumCategoryTableAdapters.ForumCategoriesTableAdapter categoryTA = new dsForumCategoryTableAdapters.ForumCategoriesTableAdapter();

dsForumCategory.ForumCategoriesDataTable categoryDT = categoryTA.GetData();

ddlCategories.DataSource = categoryDT;

ddlCategories.DataValueField = categoryDT.Columns["CategoryID"].ToString();

ddlCategories.DataTextField = categoryDT.Columns["Title"].ToString();


Open in new window

Question by:tristamane
    LVL 17

    Expert Comment

    have you tried adding the item after you have bound the data?

    Another option is to create the query in a stored procedure that adds the dummy record "Select a Category" to the resultset.

    Author Comment

    The snippet attached works but I am using the CategoryID as the value and it requires an int value otherwise throws error.

    System.ArgumentException: Input string was not in a correct format.Couldn't store <> in CategoryID Column.  Expected type is Int32.

    Any suggestions to get around it? I need a blank value for client side validation.

    DataRow row = categoryDT.NewRow();
    row["CategoryID"] = "0";
    row["ParentID"] = "0";
    row["Title"] = "";
    categoryDT.Rows.InsertAt(row, 0);

    Open in new window

    LVL 17

    Expert Comment

    Can you give it a negative value , say -1?

    Author Comment

    That didn't work. The RequiredFieldValidator is not firing. I'm guessing the only way around is to create a CustomValidator with client side validation.
    LVL 23

    Accepted Solution


    No need to add a dummy value via database commands.

    vendorsLst.Items.Insert(0, New ListItem("Choose Vendor"))
    where vendorsLst is name of control, 0 is the first position and replace "choose vendor" with your value.

    Author Comment

    Eggsalad, the following did the trick.
    ddlCategories.Items.Insert(0, new ListItem("Select Category", ""));

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now