Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid
Go Back  Xtreme Visual Basic Talk > > > Conversion from type 'DBNull' to type 'String' is not valid


Reply
 
Thread Tools Display Modes
  #1  
Old 02-16-2008, 04:48 PM
Hollertrek Hollertrek is offline
Regular
 
Join Date: Jul 2007
Location: Chanhassen, Minnesota
Posts: 98
Default Conversion from type 'DBNull' to type 'String' is not valid


Populating a datatable from Access gives me this exception where a record has an empty field. How do I handle it?

System.InvalidCastException was unhandled
Message="Conversion from type 'DBNull' to type 'String' is not valid."

Code:
    Private Sub ShowData()
        lblCustNo.Text = CStr(dtOrderEntry.Rows(intCurrRow)("CustNo"))
        lblCustName.Text = CStr(dtOrderEntry.Rows(intCurrRow)("CustName"))
        lblAddr1.Text = CStr(dtOrderEntry.Rows(intCurrRow)("Addr1"))
        lblAddr2.Text = CStr(dtOrderEntry.Rows(intCurrRow)("Addr2"))
        lblCity.Text = CStr(dtOrderEntry.Rows(intCurrRow)("City"))
        lblState.Text = CStr(dtOrderEntry.Rows(intCurrRow)("State"))
        lblZip.Text = CStr(dtOrderEntry.Rows(intCurrRow)("ZipCode"))
        lblDayPhone.Text = CStr(dtOrderEntry.Rows(intCurrRow)("DayPhone"))
        lblEvePhone.Text = CStr(dtOrderEntry.Rows(intCurrRow)("EvePhone"))

    End Sub
Reply With Quote
  #2  
Old 02-16-2008, 06:07 PM
Machaira's Avatar
MachairaConversion from type 'DBNull' to type 'String' is not valid Machaira is offline
Jedi Coder

* Expert *
 
Join Date: Aug 2002
Location: Abingdon, MD
Posts: 3,438
Default

Use the ToString method instead

lblCustNo.Text = dtOrderEntry.Rows(intCurrRow)("CustNo").ToString()
Reply With Quote
  #3  
Old 02-16-2008, 10:24 PM
shaul_ahuvaConversion from type 'DBNull' to type 'String' is not valid shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

I'll throw in my voice as well

What I've done in the past was to create a generic "GetValue" function:
Code:
Public Shared Function GetValue(Of T)(value As Object) As T
    If value is Nothing OrElse value Is DBNull.Value
        Return Nothing
    End If

    Return DirectCast(value, T)
End Function
.
.
.
lblCustNo.Text = GetValue(Of String)(dtOrderEntry.Rows(intCurrRow)("CustNo"))
It's a little bit longer than Machaira's suggestion, but can be used for any type.
Reply With Quote
  #4  
Old 02-17-2008, 07:48 AM
Hollertrek Hollertrek is offline
Regular
 
Join Date: Jul 2007
Location: Chanhassen, Minnesota
Posts: 98
Default

Thanks to both of you. I tried .ToSring and it worked like a charm, though I'm not sure why, over the original. The second approach I will keep in mind for other types. This forum never disappoints!
Reply With Quote
  #5  
Old 02-17-2008, 01:22 PM
shaul_ahuvaConversion from type 'DBNull' to type 'String' is not valid shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

DBNull.ToString returns an empty string, but DBNull can't be directly converted (CStr, CType, DirectCast) to a string - that's why it wasn't working
Reply With Quote
  #6  
Old 09-24-2009, 07:03 AM
learner520 learner520 is offline
Newcomer
 
Join Date: May 2009
Posts: 5
Default

I never find a perfect answer anywhere but on Xtreme VB TALK
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid
 
Conversion from type 'DBNull' to type 'String' is not valid
Conversion from type 'DBNull' to type 'String' is not valid
 
-->