Add a blank entry to a dropdownlist filled by a dataset

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();
ddlCategories.DataBind();

Open in new window

tristamaneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
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.
0
tristamaneAuthor Commented:
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

0
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
Can you give it a negative value , say -1?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

tristamaneAuthor Commented:
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.
0
Saqib KhanSenior DeveloperCommented:

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tristamaneAuthor Commented:
Eggsalad, the following did the trick.
ddlCategories.Items.Insert(0, new ListItem("Select Category", ""));

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.