Results 1 to 3 of 3

Thread: [RESOLVED] DBNull

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780

    Resolved [RESOLVED] DBNull

    I've been getting this error when pulling data from a recordset. It is coming from a null entry and as a newbie to VB2008 I'm not sure how to get around this.

    here is my code
    lblShipping.Text = reader("CARRIERCODE")

    the error:
    Conversion from type 'DBNull' to type 'String' is not valid.

    Basically what i'm attempting to accomplish is to see if there is anything in that field. If so put it into the text box if not leave it blank.
    Last edited by Dubya007; Dec 13th, 2010 at 03:24 PM.
    "...Men will still say THIS was our finest hour"
    If a tree falls in the woods and no one is there to see it, do all the other trees make fun of it?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: DBNull

    That looks like a DataReader, which gives you other options. One thing you could do is something like:
    Code:
    If IsNull(reader("CARRIERCODE")) Then
     'It is Null
    End If
    However, for a datareader, there is an alternative:
    Code:
    If reader.IsDBNull(reader.GetOrdinal("CARRIERCODE")) Then
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780

    Re: DBNull

    That worked great! Thank you so much for the fast answer.

    Here is my final Code.
    Code:
    If reader.IsDBNull(reader.GetOrdinal("CARRIERCODE")) Then
                                lblShipping.Text = ""
                            Else
                                lblShipping.Text = reader("CARRIERCODE")
                            End If
    "...Men will still say THIS was our finest hour"
    If a tree falls in the woods and no one is there to see it, do all the other trees make fun of it?

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