views:

43

answers:

2

hi,

yesterday i posted a question regarding the Exception Handling technique, but i did'nt quite get a precise answer, partly because my question must not have been precise. So i will ask it more precisely.

There is a method in my BLL for authenticating user. If a user is authenticated it returns me the instance of the User class which i store in the session object for further references. the method looks something like this...

public static UsersEnt LoadUserInfo(string email)
{
    SqlDataReader reader = null;
    UsersEnt user = null;

    using (ConnectionManager cm = new ConnectionManager())
    {
        SqlParameter[] parameters = new SqlParameter[1];

        parameters[0] = new SqlParameter("@Email", email);

        try
        {
            reader = SQLHelper.ExecuteReader(cm.Connection,
                    "sp_LoadUserInfo", parameters);
        }
        catch (SqlException ex)
        {
            //this gives me a error object
        }

        if (reader.Read())
            user = new UsersDF(reader);
    }

    return user;
}

now my problem is suppose if the SP does not exist, then it will throw me an error or any other SQLException for that matter. Since this method is being called from my aspx.cs page i want to return some meaning full message as to what could have gone wrong so that the user understands that there was some problem and that he/she should retry logging-in again.

but i can't because the method returns an instance of the User class, so how can i return a message instead ??

i hope i made it clear !

thank you.

+1  A: 

There are a lot of approaches you could take here, an easy one would be to return null if you can't find an appropriate user object (because of the exception). then in the calling code you just test for null and if it is null display an error message.

for example

User u = LoadUserInfo(email);
if(u == null)
{
    ErrorLabel.Text = "Could not log in.";
    ErrorLabel.Visible = true;
    //.... or some other notification
}
else
{
    //... normal load
}

that would be a basic way to go about it.

luke
A: 

Hi shrewd,

There are lot of approaches you have but in your method you declared "SqlDataReader reader = null;" some times you will get error at if condition "if (reader.Read())" because you declared as null.

public static UsersEnt LoadUserInfo(string email)

{

SqlDataReader reader = new  SqlDataReader();

UsersEnt user = null; 

using (ConnectionManager cm = new ConnectionManager()) 
{ 
    SqlParameter[] parameters = new SqlParameter[1]; 

    parameters[0] = new SqlParameter("@Email", email); 

    try 
    { 
        reader = SQLHelper.ExecuteReader(cm.Connection, 
                "sp_LoadUserInfo", parameters); 
        if (reader.Read()) 
             user = new UsersDF(reader); 

    } 
    catch (SqlException ex) 
    { 
        user.Exception=ex.Message;        
    } 

} 

return user; 

}

and then

User us = LoadUserInfo(email);
if(us.Exception != null)
{
ErrorLabel.Text = "Could not log in.";
ErrorLabel.Visible = true;
//.... or some other notification
}
else
{
//... normal load
}

i think it will work.

Chandu