Solved

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

Posted on 2014-10-07
6
150 Views
Last Modified: 2014-10-13
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
Comment
Question by:BlakeMcKenna
  • 3
  • 2
6 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 40367335
try :

clsDE.capacity = If(IsDBNull(row2("Capacity")), "", row2("Capacity"))
0
 

Author Comment

by:BlakeMcKenna
ID: 40368243
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
 

Author Comment

by:BlakeMcKenna
ID: 40368995
Paul,

I did try your suggestion and it worked but not in all instances.
0
 

Author Comment

by:BlakeMcKenna
ID: 40369680
I've requested that this question be deleted for the following reason:

I figured it out...
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 40369681
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AutoHotkey ActiveX Component Error 8 73
Get String split 5 48
Passing a form as a parameter to an external class 1 28
Help with Query not working in client's PC 1 35
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now