(866) 837-0540
 
 

Internet Marketing and Design Services for Turnkey Web Solutions 

Facebook Twitter Gplus Flickr LinkedIn YouTube Google Maps E-mail RSS
 
 
Home » web development » web design » VB.net » Conversion from type ‘DBNull’ to type ‘String’ is not valid
formats

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

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.13_1145]
Rating: 6.2/10 (6 votes cast)
VN:F [1.9.13_1145]
Rating: -2 (from 2 votes)
Conversion from type 'DBNull' to type 'String' is not valid, 6.2 out of 10 based on 6 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.
  • Facebook
  • LinkedIn
  • TwitThis
  • MySpace
  • Google Bookmarks
  • Webnews
  • Digg
  • del.icio.us
  • StumbleUpon
  • eKudos
  • email
  • Fleck
  • Hype
  • MisterWong
  • Print
  • Technorati
  • YahooMyWeb
 
Tags: , ,

4 Responses

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

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  2. In it something is. Now all became clear, many thanks for the help in this question.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    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.13_1145]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  4. Suresh

    Hi,
    We can also check below
    VB.NET
    String nullString = If(IsDBNull(nullString)), String.Empty, nullString)

    C#
    String nullString = IsDBNull(nullString) ? String.Empty: nullString

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: +1 (from 1 vote)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© Copyright © 1998-2011, WebPaws.com All rights reserved.
credit