• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

Getting a "Conversion from type 'DBNull' to type 'String' is not valid" error.

Isn't this the correct way to check for NULLS if the value is coming from a Database? I'm getting the  "Conversion from type 'DBNull' to type 'String' is not valid" error on the indicated line. The variable clsDE.capacity is defined as a "String".

    Public Function GetDetailsEquipmentRecord(ByVal iEquipment_ID As Integer, ByVal clsDE As CalTableClasses.CalibrationClasses.DETAILS_Equipment, ByVal EH As ErrorHandling.ErrorHandler)
        Try
            DL.GetDetailsEquipmentRecord(iEquipment_ID, EH)

            If EH.Success Then
                If EH.DataSet.Tables(0).Rows.Count = 1 Then
                    Dim row2 As DataRow = EH.DataSet.Tables(0).Rows(0)
                    clsDE.capacity = If(DBNull.Value.ToString.Equals(row2("Capacity")), "", row2("Capacity"))  '----------> Throwing exception
                End If
            End If

        Catch ex As Exception
            EH.ErrorMessage = "CalBusiness/GetDetailsEquipmentRecord() - " & ex.Message & "~E"
        End Try

        Return EH
    End Function

Open in new window

0
BlakeMcKenna
Asked:
BlakeMcKenna
  • 3
  • 2
1 Solution
 
Paul JacksonSoftware EngineerCommented:
try :

clsDE.capacity = If(IsDBNull(row2("Capacity")), "", row2("Capacity"))
0
 
BlakeMcKennaAuthor Commented:
I actually went a different route that I've always used in the past. It's more primitive but works like a charm.

clsDE.capacity = "" & row2("Capacity")

Open in new window

0
 
BlakeMcKennaAuthor Commented:
Paul,

I did try your suggestion and it worked but not in all instances.
0
 
BlakeMcKennaAuthor Commented:
I've requested that this question be deleted for the following reason:

I figured it out...
0
 
Paul JacksonSoftware EngineerCommented:
Shouldn't be deleted, QA's answer or both QA's and Expert's answer should be selected as the solution and added to the knowledgebase.
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now