Click to See Complete Forum and Search --> : Not all code paths return a value


watherton
May 16th, 2004, 05:16 AM
I have been rapping my head over this one for ages, and have finally given up and am asking for help.

I have a webform that references a custom namespace, inside that namespace is a class with a method that checks the users username and password. What i want the code to do is return the users firstname.

the code is as follows:

CALLIING CODE
private void Button1_Click(object sender, System.EventArgs e)
{
if (IsPostBack)
{
string sResult = Logon.Checkcredentials TextBox1.Text,TextBox2.Text);
}
}

CODE BEING CALLED
namespace WebnosysV2
{
namespace Connection
{
public class Logon
{
public static string Checkcredentials(string UserName, string Password)
{
int iUserID, iErrors;
string sErrorDesc, sFirstName;
SqlConnection strConn = new SqlConnectionSystem.Configuration.ConfigurationSettings.AppSettings["strConn"]);

SqlCommand objCMD = new SqlCommand("CheckLogon", strConn);
objCMD.CommandType = CommandType.StoredProcedure;
objCMD.Parameters.Add("@Username", SqlDbType.Char, 10);
objCMD.Parameters["@Username"].Value = UserName;
objCMD.Parameters.Add("@Password", SqlDbType.Char, 10);
objCMD.Parameters["@Password"].Value = Password;
objCMD.Parameters.Add("@UserID", SqlDbType.Int);
objCMD.Parameters["@UserID"].Direction = ParameterDirection.Output;
objCMD.Parameters.Add("@Errors", SqlDbType.Int);
objCMD.Parameters["@Errors"].Direction = ParameterDirection.Output;
objCMD.Parameters.Add("@Firstname", SqlDbType.Char,10);
objCMD.Parameters["@Firstname"].Direction = ParameterDirection.Output;

strConn.Open();
objCMD.ExecuteNonQuery();

iUserID = (int)objCMD.Parameters["@UserID"].Value;
iErrors = (int)objCMD.Parameters["@Errors"].Value;
sFirstName = Convert.ToString(objCMD.Parameters["@Firstname"].Value);

strConn.Close();
switch(iErrors)
{
case 1:
sErrorDesc += "You must supply a Username!";
break;
case 2:
sErrorDesc += "You must supply a Password!";
break;
case 3:
sErrorDesc += "No user found, please try again";
break;
default:
sErrorDesc += "Welcome";
return (sFirstName);

}
}
}
}
}


I would be most grateful for your assistance.

hspc
May 16th, 2004, 05:54 AM
the problem is with the switch
put the return outside it.
switch(iErrors)
{
case 1:
sErrorDesc += "You must supply a Username!";
break;
case 2:
sErrorDesc += "You must supply a Password!";
break;
case 3:
sErrorDesc += "No user found, please try again";
break;
default:
sErrorDesc += "Welcome";
}
return (sFirstName);