I recently had a DropDownList being pre-populated from a database. I also wanted to insert "Select" at the top of the list and make it a required field. Googling this yielded much misinformation for what turned out to be pretty simple to solve in the end.

Say you have a DDL like this:

<asp:DropDownList id="testDDL" runat="server">
    <asp:ListItem Text="Select"></asp:ListItem>


If we try to populate this DropDownList from a database with the values 1,2 and 3, and then try and add a RequiredFieldValidator, that validator will not fire.

Some people resort to creating a CustomValidator and/or JavaScript here. This is not necessary. We need to take a step back here and re-phrase the problem: We already have data in our list in the shape of the "Select" item. We now want to append data from the database. A quick search of the MSDN documentation leads to the AppendDataBoundItems property for the DropDownList.

From the documentation: "The AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs. After data binding, the items collection contains both the items from the data source and the previously added items." The italics are mine...

We can use the InitialValue of the RequiredFieldValidator in conjunction with the AppendDataBoundItems property of the DropDownList to arrive at a working solution:

<asp:DropDownList id="testDDL" AppendDataBoundItems="true" runat="server">
    <asp:ListItem Text="Select"></asp:ListItem>
<asp:RequiredFieldValidator ID="salaryReqVal" ControlToValidate="salaryDDL"
    ErrorMessage="*" Display="dynamic" InitialValue="Select"

Incorrect Format


Here's the scenario: I'm building a basic 3-layer Web application for a client with business and data layers. Queries are performed in stored procedures, just basic ADO.NET. The table I am attempting to insert a record into has a range a field types such as strings, ints, decimals, etc.

If the user is entering integer or decimal values via texboxes, which is usually the case, then you have to expect either no data or a string of possibly mal-formed data. You must filter out the mal-formed data with validation and/or convert the resulting input data to the expected database type.

If you have a database field called SquareFeet of type int and you attempt to insert the following in your code-behind insert method, you will probably see this error:

// Results in error message
AddSomething(int squareFeet)


You must be careful if you are trying to fetch, say, an int or decimal as input:

// Correct way if SquareFeet field is an int
int squareFeet
if (int.TryParse(squareFeetTxt.Text.Trim(), out squareFeet))
    squareFeet = Convert.ToInt32(squareFeetTxt.Text.Trim());
     squareFeet = 0;
// Now insert the value as part of the record


// Correct way if SquareFeet field is a decimal
int squareFeet
if (decimal.TryParse(squareFeetTxt.Text.Trim(), out squareFeet))
    squareFeet = Convert.ToDecimal(squareFeetTxt.Text.Trim());
     squareFeet = 0.0m;
// Now insert the value as part of the record



Refer to these links for the differences between Convert.ToInt, int.Parse and Int.TryParse:

Refer to this link for an explanation of the 0.0m decimal syntax:

Moral of the Story
If you are learning any language, your first priority must be to understand the types thoroughly. Resist the temptation to gloss over the grammar just to get to the fancy stuff! :-)

Tags: , ,


Tech Support from Hell

by agrace 8. August 2009 16:48

Follow me on Twitter Presenting the modern-day equivalent of "Who's on First"....

I just got my son a new 64-bit PC with Vista installed. Unfortunately, we didn't order a built-in wireless adapter and attempted to use an existing adapter which we had been using for our older 32-bit machines. Needless to say, we encountered problems. I went online to the website of the adapter manufacturer and had to fill about about half a dozen forms before being offered the opportunity to talk to an online support "engineer". Later on, they emailed me the text of the two online support sessions I initiated. I have changed the name of the company and the female support imposter. Nothing else has been changed. Read on for a dose of the twighlight zone... I promise that no blondes have been injured in the lead up to this blog post :-|

Hi, my name is Retarded Support Ditz. How may I help you? [03:52:34 PM]
Anthony Grace: I need Vista 64 bit driver for my network adapter [03:53:21 PM]
Retarded Support Ditz: Are you looking for the drivers of WUSB54G ? [03:57:57 PM]
Anthony Grace: yes :-) [03:54:35 PM]
Anthony Grace: Windows gives an error message when we use our existing disk... [03:55:11 PM]
Retarded Support Ditz: I want to inform you that your adapter is incompatible with the vista 64 bit drivers ,there are two adapters from Linkcrisco that would work with the vista 64 bit operating system is WMP11(In desktop computer) and WUSB600N (On laptop and desktop) [04:00:01 PM]
Anthony Grace: Can you give me download links to these? [03:57:02 PM] (finishing gulp of coffee and just missed what she said)
Retarded Support Ditz: Its WMP110 [04:00:57 PM]
Anthony Grace: link? [03:57:21 PM]
Retarded Support Ditz: You can visit www.linkcrisco.com/downloads and type the model number of the device . [04:01:48 PM]
Anthony Grace: Thanks Retarded :-) [03:58:12 PM]
Retarded Support Ditz: Thank you for giving us an opportunity to serve you through Live Chat Support. For your records, a transcript of this chat session will be e-mailed to you. Feel free to contact us if you require further assistance. Thank you for choosing Linkcrisco and have a great day! [04:02:23 PM]
Anthony Grace: Are u still there? [03:58:46 PM]
Retarded Support Ditz: Yes. [04:03:04 PM]
Anthony Grace: Getting "Enter Valid Model Number" error message... [03:59:19 PM]
Retarded Support Ditz: Did you type WM110 ? [04:03:42 PM]
Anthony Grace: yes, please try it yourself... [04:00:18 PM]
Anthony Grace: you said wmp110 initially? [04:00:44 PM]
Anthony Grace: Same error message for WM110 [04:01:09 PM]
Retarded Support Ditz: WMP110 . [04:05:05 PM]
Retarded Support Ditz: Make sure you are choosing WMP110 . [04:05:25 PM]
Anthony Grace: ok that worked... [04:01:48 PM]
Retarded Support Ditz: Okay . [04:05:51 PM]
Retarded Support Ditz: Are we all set? [04:05:58 PM]
Anthony Grace: it's asking for version number 1.0 or 2.0 [04:02:06 PM]
Retarded Support Ditz: Select 2.0 [04:06:09 PM]
Anthony Grace: nothing happens... staring at page? [04:02:49 PM]
Retarded Support Ditz: Re-select the version . [04:07:14 PM]
Anthony Grace: I did... no postback on form [04:03:53 PM]
Anthony Grace: Cna you email me the driver? [04:04:06 PM]
Retarded Support Ditz: Click on the following link . [04:08:27 PM]
Retarded Support Ditz:
http://downloads.somecrisco.com/downloads/wtf.zip [04:08:40 PM]
Anthony Grace: Thanks Retarded, we'll give that a try, bye :-) [04:05:26 PM]
'Anthony Grace' disconnected ('Concluded by End-user'). [04:05:29 PM]

Aargh! Now, in fairness to Retarded Support Ditz, she made an attempt to tell me something at 4:00 p.m. but she failed to notice that I'm still looking for these drivers for our existing adapter - I didn't quite get what she was saying. Here's round two...

Retarded Support Ditz: Hi, my name is Retarded Support Ditz. How may I help you?
Anthony Grace: Hi again Retarded, the driver you provided failed to install
Retarded Support Ditz: It is because you are using WUSB54G adapter and you are trying to install the WMP110 drivers .
Retarded Support Ditz: Which is not possible .
Anthony Grace: Then why did you provide me with this driver???
Retarded Support Ditz: Because you asked me the link .
Anthony Grace: I asked you for a 64 bit vista driver for this particular network card. Do I have to copy and paste the converstion or can we escalate this? Network hardware was supplied by Comcast; can we escalate this? Thank you.
Anthony Grace: Retarded Support Ditz: Are you looking for the drivers of WUSB54G ? [03:57:57 PM] (copied and pasted from earlier session into this one)
Retarded Support Ditz: Anthony ,I did inform you that your adapter is incompatible with the 64 bit operating system and I also mentioned that only two adapters from Linksys that supports Vista 64 bit operating system ,1.WMP110 2 WUB600N
Anthony Grace: obviously the first one does not supprt Vista 64 bit, so how about the second one?
Retarded Support Ditz: You will have to purchase a new card .
Retarded Support Ditz: As the adapter which you are using will not work .
Anthony Grace: thanks :-S
Retarded Support Ditz: Thank you for giving us an opportunity to serve you through Live Chat Support. For your records, a transcript of this chat session will be e-mailed to you. Feel free to contact us if you require further assistance. Thank you for choosing LinkCrisco and have a great day!

So I get my kid to call around and we locate a network adapter from the same company as the older one. He asks the store guy a pile of questions as he converses with me on his cell phone. When he gets home I confirm from the documentation that the adapter is indeed "Vista 64-bit Ready". Fat chance. One hour of cursing later and I'm back online at the same site looking for a driver. I finally got one that worked and managed to avoid their online support "engineers". I guess she tried to tell me something but it's hard to have a communication going with someone reading their lines from a manual. And to think that I always wished for a human on the other end... I wonder should I have performed the Turing Test before beginning? :-|