Question

in my web site, mobile number is userId, then some people has email,and some others no, how can i check one user has email or not?

MembershipUser mu = Membership.GetUser(username);
string userEmail = mu.Email.ToString();
if (!string.IsNullOrEmpty(userEmail))
{
  //do something
}

in above code i get error in 2nd line,any help?

Was it helpful?

Solution

If your Email string is null, then ToSting()ing it will throw an exception. You should remove that method call. It's probably you're main problem.

What is the error?

Try debugging like this

    if (String.IsNullOrWhiteSpace(username))
            throw new ArgumentException("username is null or whitespace");

    MembershipUser mu = Membership.GetUser(username);
    if (mu == null)
        throw new ArgumentNullException("Couldn't get a membership user for that username");
    string userEmail = mu.Email; // email is already a string, do not invoke Tostring() on it

     if (!string.IsNullOrEmpty(userEmail))
     {
         //do something
     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top