Results 1 to 7 of 7

Thread: InvalidCastException (RESOLVED)

  1. #1

    Thread Starter
    Addicted Member GSIV's Avatar
    Join Date
    Jun 2002
    Location
    Texas, USA
    Posts
    213

    Question InvalidCastException (RESOLVED)

    An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

    Additional information: Cast from type 'DBNull' to type 'String' is not valid.
    Is there an easy way to handle this exception? I am trying to use isNull right now, but it seems bulky.
    Last edited by GSIV; Nov 20th, 2003 at 09:22 PM.

  2. #2
    Hyperactive Member
    Join Date
    May 2002
    Location
    Wisconsin, USA
    Posts
    279
    Try using something like this:
    Code:
    If Not myDataReader.Item("Person_Name") Is DBNull.Value Then
        txtPersonName.Text = myDataReader.Item("Person_Name")
    Else : txtPersonName.Text = ""
    End If

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try using GetString and see if that works. I haven't tried it but it should convert DBNulls to String.Empty for you.
    VB Code:
    1. txtPersonName.Text=myDataReader.GetString("Person_Name")

  4. #4

    Thread Starter
    Addicted Member GSIV's Avatar
    Join Date
    Jun 2002
    Location
    Texas, USA
    Posts
    213
    Thanks guys!

  5. #5
    Hyperactive Member
    Join Date
    May 2002
    Location
    Wisconsin, USA
    Posts
    279
    No, it does not convert DBNull's to String.Empty. If you try to set a string equal to DBNull it will through an exception, and that is the problem he was having.

    I wouldn't want it to convert it to String.Empty. There are some cases where I would want to know if it were a Null value being returned from the database.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by crpietschmann
    No, it does not convert DBNull's to String.Empty. If you try to set a string equal to DBNull it will through an exception, and that is the problem he was having.

    I wouldn't want it to convert it to String.Empty. There are some cases where I would want to know if it were a Null value being returned from the database.
    Bummer it doesn't convert it for you.

    Although what are you talking about you 'wouldn't want it to convert it to String.Empty'? That is exactly what you did in your example: Else : txtPersonName.Text = ""

    I agree there are times you may want to know if it returned DBNull or not but it is common to substitute DBNull with String.Empty while in memory. If you want to maintain Null in the DB you can convert it back when you write to the DB.

  7. #7
    Hyperactive Member
    Join Date
    May 2002
    Location
    Wisconsin, USA
    Posts
    279
    Originally posted by Edneeis
    Although what are you talking about you 'wouldn't want it to convert it to String.Empty'? That is exactly what you did in your example: Else : txtPersonName.Text = ""
    In that specific case, Yes, I wanted it to be converted.

    After some thought, I do suppose that it would be nice for it to convert it to String.Empty when you are setting as string variable equal to the value that might be DBNull. It would eliminate the need for the code that I posted above.

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