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)
{
    Convert.ToInt32(SquareFeetTxtBox.Text.Trim());   
}

 

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());
else
     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());
else
     squareFeet = 0.0m;
// Now insert the value as part of the record

 

References

Refer to these links for the differences between Convert.ToInt, int.Parse and Int.TryParse:
http://dotnetperls.com/int-parse-conversion
http://dotnetperls.com/int-tryparse

Refer to this link for an explanation of the 0.0m decimal syntax:
http://msdn.microsoft.com/en-us/library/364x0z75(VS.80).aspx

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! :-)

kick it on DotNetKicks.com

Tags: , ,

ASP.NET | C#



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? :-|




I was working on a site tonight after being away from it for over three months. I came across some CSS and it took me a while to remember what it was for. I gleaned this online somewhere ages ago, but kudos and credit to the author whoever they are!

Jumping Treeview

 

For some time I have been having problems using the ASP.NET TreeView control in Sitemaps; every time I would hover over one of the parent nodes it would jump up and down. Here's some sample markup and the CSS to fix it in Firefox:

        <div id="idTreeView">
            <asp:TreeView ID="TreeView1" runat="server"                     DataSourceID="SiteMapDataSource1"
                    HoverNodeStyle-Height="0" Font-Bold="true" ImageSet="BulletedList">
                <RootNodeStyle Font-Bold="True" />
                <ParentNodeStyle VerticalPadding="0px" Font-Bold="True"                     Font-Underline="false"  />
                <HoverNodeStyle Font-Underline="false" ForeColor="#5555DD" />
                <NodeStyle Font-Bold="False" Font-Size="8pt" CssClass="sitelink"
                    ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px"                     VerticalPadding="0px" />
            </asp:TreeView>
           
            <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
        </div>

 

        div#idTreeView img
        {
            display: block;
            float: left;
        }

        div#idTreeView div
        {
            display: inline-block;
        }

        div#idTreeView .sitelink a
        {
            text-decoration:none;
        }

 

kick it on DotNetKicks.com

Tags: ,

ASP.NET | CSS