2
Answers

Handle null on checking DB

Photo of mike Delvotti

mike Delvotti

13y
1.3k
1
I'm having some real issues with handling a null reference when checking my database for a username and password on the below code.

If the username and password match it works, but if it doesn't or the username + password textboxes are empty it throws a null execption?

How can I handle the null on the below?

if (strResult.Length == 0)
   {
               
      label1.Text = "INCORRECT USER/PASS!";
         }
   else
   {
              
    label1.Text = "YOU ARE LOGGED IN!";
       }
}

Answers (2)

1
Photo of Vulpes
NA 98.3k 1.5m 13y
You can check for a string being either null or empty with:

if (String.IsNullOrEmpty(strResult))
{                
   label1.Text = "INCORRECT USER/PASS!";
}
else
{              
   label1.Text = "YOU ARE LOGGED IN!";
}
Accepted
0
Photo of mike Delvotti
NA 287 0 13y
Thanks Vulpes, that was bang on, I've been going round in circles with that one.