Checking for null

A

Andy G

I'm getting this error...Operator is not valid for type 'DBNull' and string
"". What is happening is that I'm calling a stored procedure to use the
email address to recover a forgotten username. If the email does not exist
then I get the above error. After I execute the stored procedure I try and
apparenty this is not catching the error. Should I use a Try...Catch? What
is the correct way to check for nulls in ASP.NET?

If Not myCommand.Parameters("@LOGIN_NAME").Value = "" Then

returnLogin = myCommand.Parameters("@LOGIN_NAME").Value

Else

lblMessage.Text = "Email does not exist in system. Re-enter email or contact
BRM Support"

End If
 
D

D. Shane Fowlkes

Just a guess but have you tried IsNothing?

If IsNothing(yadda) = True Then
Do Something
End If
 
A

Andy G

I tried this and I still get the same error. Checking for null should not
be this difficult. Any other ideas?

If IsNothing(myCommand.Parameters("@LOGIN_NAME").Value) = True Then

returnLogin = myCommand.Parameters("@LOGIN_NAME").Value

Else

lblMessage.Text = "Email does not exist in system. Re-enter email or contact
BRM Support"

End If
 
D

D. Shane Fowlkes

Hmmm...another guess. Have you tried IsDBNull?

If IsDBNull(database field here) = False Then
do something
End If
 
G

Guest

IsDBNull will and should work!
GDLUCK
PAtrick


Andy G said:
I tried this and I still get the same error. Checking for null should not
be this difficult. Any other ideas?

If IsNothing(myCommand.Parameters("@LOGIN_NAME").Value) = True Then

returnLogin = myCommand.Parameters("@LOGIN_NAME").Value

Else

lblMessage.Text = "Email does not exist in system. Re-enter email or contact
BRM Support"

End If
 
A

Andy G

Thanks! IsDBNull worked in addition to a little thing I forgot. I added
Exit Sub inside the condition, otherwise it would redirect which I didn't
want it to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top