Conversion from type ‘DBNull’ to type ‘String’ is not valid

Published under VB.net,web design,web development. Tags: , , .

In a VB.net application, retrieving variables from a database, if the value was null, it would cause the error, “Conversion from type ‘DBNull’ to type ‘String’ is not valid”.  A work-around to stop this error was to create a function that would evaluate the imported database variable for a null value as an object then convert it to a string for use without error in your code.

Here is the VB.net code:


Function checkNull(Byval variable as object) as String
If IsDBNull(varialbe) Then
Return("")
Else
Return (variable)
End If
End Function

VN:F [1.9.3_1094]
Rating: 6.8/10 (5 votes cast)
VN:F [1.9.3_1094]
Rating: -2 (from 2 votes)
Conversion from type 'DBNull' to type 'String' is not valid, 6.8 out of 10 based on 5 ratings
 
Take a piece of the Web from WebPaws.com: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • MisterWong
  • Y!GG
  • Webnews
  • Digg
  • del.icio.us
  • StumbleUpon
  • De.lirio.us
  • eKudos
  • email
  • Facebook
  • Fleck
  • Google Bookmarks
  • Hype
  • LinkedIn
  • MySpace
  • Print
  • Technorati
  • YahooMyWeb

3 Comments to Conversion from type ‘DBNull’ to type ‘String’ is not valid

  1. AndrewBoldman,

    Hi, cool post. I have been wondering about this topic,so thanks for writing.

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  2. feliciafet,

    In it something is. Now all became clear, many thanks for the help in this question.

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  3. shanwaj,

    Hi there,
    Very nice example,i am struggling to convert dbnull to string.Every time i am getting error.I solved my problem with your example.

    Thanks a lot

    VA:F [1.9.3_1094]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)

Leave a comment to this post


WebPaws.com is Digg proof thanks to caching by WP Super Cache