Results 1 to 4 of 4

Thread: Type Casting Issues - best practices

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Location
    Southwest Ohio
    Posts
    6

    Type Casting Issues - best practices

    I am writing a program to read through an Access database and use the records selected to create orders in a separate ERP package... I have successfully selected the records for processing from Access into a dataset, but I am having problems due to InvalidCastException errors.

    I have two fields in Access (Price and Cost) that are typed as 'Currency'. The purpose of these two fields are to override the values in the ERP package, that is, if they have a value set in Access, use it, if not, use the default value in the ERP package.

    So, it is not unusual for there to be no value set for price and cost. Attempts to access this data from my data set in my code generated the cast exception errors mentioned above. I have created the following as a workaround for this issue, but my question is - Is this the best way to accomplish this? Suggestions are appreciated.

    #code

    Dim wQty, wPrice, wCost As Double

    wPrice = 0
    If Not (IsDBNull(ds2.Tables("Dtl").Rows(i).Item("Price"))) Then
    wPrice = ds2.Tables("Dtl").Rows(i).Item("Price")
    End If

    wCost = 0
    If ds2.Tables("Dtl").Rows(i).Item("Cost") <> vbNull Then
    wCost = ds2.Tables("Dtl").Rows(i).Item("Cost")
    End If
    #code

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Type Casting Issues - best practices

    First up, if the column is Currency in Access then the values are Decimal in VB, not Double. The correct way to go about this would be:
    Code:
    Dim price As Decimal = Decimal.Zero 'The variable will default to zero anyway but it doesn't hurt to be explicit.
    Dim row = ds2.Tables("Dtl").Rows(i)
    
    If Not row.IsNull("Price") Then
        price = CDec(row("Price"))
    End If
    Note that you might also use a nullable Decimal so that you don't have to use a fake zero:
    Code:
    Dim price As Decimal?
    Dim row = ds2.Tables("Dtl").Rows(i)
    
    price = If(row.IsNull("Price"), Nothing, CDec(row("Price")))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Type Casting Issues - best practices

    Well, best practice would probably be not to allow Null values in the database and force a default zero value.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Type Casting Issues - best practices

    not necessarily.... Null means the value is unknown ... different from a value of 0 ... just like with strings, a null value can and is usually different from an empty string. There's nothing wrong with null values in a database... now if it's a required field ... you can't leave it as NULL and then either have to default it to a value, or force the user to enter a value.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width