patterncsharpMinor
Getting the unique identifier from a given login username
Viewed 0 times
uniquetheidentifierlogingettingusernamefromgiven
Problem
Are there any issues in this code?
///
/// This function gets the unique identifier of a member by the given
/// login user name of that member on our web site.
///
/// The username
public static string GetMemberNumberByUserId(string userName, string connectionString)
{
try
{
SqlConnection database = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("GetMemberInfoByUserID", database);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@userID", userName);
database.Open();
SqlDataReader reader = command.ExecuteReader();
reader.Read();
string memberNumber =
reader.GetString(reader.GetOrdinal("memberNumber"));
// Make sure the member number is a 11 or 12 digit number
if (Regex.IsMatch(memberNumber, @"^(000|100)\d{6,7}(?!00)\d{2}$"))
{
return memberNumber;
}
else
{
return "";
}
}
catch(Exception)
{
return null;
}
}Solution
catch(Exception)
{
return null;
}You should never do this. Exception means that there is some sort of problem. And you shouldn't ignore problems, you should learn about them and do something about them as soon as possible.
What you should do is that at first, don't catch any exceptions. Then, if you find out that some exceptions actually do happen under normal conditions, then handle those, but only those.
Code Snippets
catch(Exception)
{
return null;
}Context
StackExchange Code Review Q#30460, answer score: 4
Revisions (0)
No revisions yet.