Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DBNull

Status
Not open for further replies.

tmar

MIS
Mar 24, 2007
62
US
I've got the script below which I execute against my sql database and get an error stating "incorrect syntax near =" and the following line is highlighted:

If IsDBNull(command.ExecuteScalar) Then
email = "None"

Code:
Dim conn As New SqlConnection(sqlconn)
Dim command As New SqlCommand(ssql, conn)
conn.Open()
If IsDBNull(command.ExecuteScalar) Then
email = "None"
Else
email = command.ExecuteScalar
End If

conn.Close()
conn.Dispose()

Code for function:

Public Function IsDBNull(ByVal dbvalue) As Boolean
Return dbvalue Is DBNull.Value
End Function

I know that the results are an empty record set but I thought the isdbnull function would catch it. Where am I off?
 
First question:
Why do you have your own IsDBNull function? It already exists in VB.Net.

Second question:
Instead of executing your command up to two times, why don't you just execute it once. Declare a variable and set it to ExecuteScalar, then compare the value to set it to "None" if it's null.
 
I didn't realize the isdbnull was a part of vb.net. I found the code on the web when I searching for a different flavor of the same null problem.

I could be wrong but I believe when I've assigned variable's value directly to the executescalar and the resulting dataset was null I'd get an error, which is why I've done the "testing" step.

As you can see by my code I'm pretty much a novice, I apologize for my inefficiencies.
 
Try something like this, comment out or remove your IsDBNull function.

Code:
        Dim Email As String = ""
        Dim o As Object = cmd.ExecuteScalar
        If IsDBNull(o) Then
            Email = "None"
        Else
            Email = o.ToString
        End If
        MessageBox.Show(Email)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top