riskyricky1972
asked on
asp.net c# login class
I have the following code behind asp.net c#. It works fine, and now I want to put them into class and save in Bin folder.
Can you show me what I need to edit inside of the following codes, and save in class in bin folder?
and I will have login.aspx and use code behind to call the class back. How can I do that?
I will give out 500 pts for this question
Note: You can ignore the dbReader object, and simply return value (LoginChkCommand.Parameter s["@retval "].Value)
protected void cmdlogin_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection();
try
{
//create db connection and open it
string ConnectionString = ConfigurationSettings.AppS ettings["D Bconnectio n"];
string CommandText = "up_IsValidLogon";
myConnection.ConnectionStr ing = ConnectionString;
SqlCommand LoginChkCommand = new SqlCommand(CommandText, myConnection);
LoginChkCommand.CommandTyp e = CommandType.StoredProcedur e;
//need to add parameter
//**********************
SqlParameter var1 = new SqlParameter();
var1.ParameterName = "@username";
var1.SqlDbType = SqlDbType.VarChar;
var1.Value = UserName.Text;
LoginChkCommand.Parameters .Add(var1) ;
SqlParameter var2 = new SqlParameter();
var2.ParameterName = "@password";
var2.SqlDbType = SqlDbType.VarChar;
var2.Value = UserPass.Text;
LoginChkCommand.Parameters .Add(var2) ;
SqlParameter var3 = new SqlParameter();
var3.ParameterName = "@retval";
var3.SqlDbType = SqlDbType.Int;
var3.Direction = ParameterDirection.Output;
LoginChkCommand.Parameters .Add(var3) ;
//**********************
myConnection.Open();
SqlDataReader myReader = LoginChkCommand.ExecuteRea der();
GridView1.DataSource = myReader;
GridView1.DataBind();
myReader.Close();
//**********************
//now get the output parameter:
lblmsg.Text = LoginChkCommand.Parameters ["@retval" ].Value
//**********************
}
catch (Exception ex)
{
throw (ex);
}
finally
{
myConnection.Close();
}
}
Can you show me what I need to edit inside of the following codes, and save in class in bin folder?
and I will have login.aspx and use code behind to call the class back. How can I do that?
I will give out 500 pts for this question
Note: You can ignore the dbReader object, and simply return value (LoginChkCommand.Parameter
protected void cmdlogin_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection();
try
{
//create db connection and open it
string ConnectionString = ConfigurationSettings.AppS
string CommandText = "up_IsValidLogon";
myConnection.ConnectionStr
SqlCommand LoginChkCommand = new SqlCommand(CommandText, myConnection);
LoginChkCommand.CommandTyp
//need to add parameter
//**********************
SqlParameter var1 = new SqlParameter();
var1.ParameterName = "@username";
var1.SqlDbType = SqlDbType.VarChar;
var1.Value = UserName.Text;
LoginChkCommand.Parameters
SqlParameter var2 = new SqlParameter();
var2.ParameterName = "@password";
var2.SqlDbType = SqlDbType.VarChar;
var2.Value = UserPass.Text;
LoginChkCommand.Parameters
SqlParameter var3 = new SqlParameter();
var3.ParameterName = "@retval";
var3.SqlDbType = SqlDbType.Int;
var3.Direction = ParameterDirection.Output;
LoginChkCommand.Parameters
//**********************
myConnection.Open();
SqlDataReader myReader = LoginChkCommand.ExecuteRea
GridView1.DataSource = myReader;
GridView1.DataBind();
myReader.Close();
//**********************
//now get the output parameter:
lblmsg.Text = LoginChkCommand.Parameters
//**********************
}
catch (Exception ex)
{
throw (ex);
}
finally
{
myConnection.Close();
}
}
ASKER
why take out 'catch' ?
If you throw the exception as you have done, there is no need to handle the exception in a 'catch' block in the first place. I think steveberzins has taken out the catch block it does not perform any functionality at all in the code given by you.
exactly, just catching something to throw it, is a waste of letters :)
ASKER
Error message: Unable to cast object of type 'System.Int32' to type 'System.String'
point to return (string)LoginChkCommand.Pa rameters[" @retval"]. Value; One line before finally
point to return (string)LoginChkCommand.Pa
Replace the following line:
return (string)LoginChkCommand.Pa rameters[" @retval"]. Value;
with this:
return Convert.ToString(LoginChkC ommand.Par ameters["@ retval"].V alue);
return (string)LoginChkCommand.Pa
with this:
return Convert.ToString(LoginChkC
ASKER
I try to call the class by using:
and it created error saying that I can not convert the variable...
string hello= LoginClass(UserName.Text, UserPass.Text);
if (hello) = "sss"
{
}
and it created error saying that I can not convert the variable...
string hello= LoginClass(UserName.Text, UserPass.Text);
if (hello) = "sss"
{
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
nice...
{
public static string LoginChk(string userName, string password)
{
SqlConnection myConnection = new SqlConnection();
try
{
//create db connection and open it
myConnection.ConnectionStr
SqlCommand LoginChkCommand = new SqlCommand("up_IsValidLogo
LoginChkCommand.CommandTyp
//need to add parameters
//**********************
SqlParameter var1 = new SqlParameter();
var1.ParameterName = "@username";
var1.SqlDbType = SqlDbType.VarChar;
var1.Value = userName;
LoginChkCommand.Parameters
SqlParameter var2 = new SqlParameter();
var2.ParameterName = "@password";
var2.SqlDbType = SqlDbType.VarChar;
var2.Value = password;
LoginChkCommand.Parameters
SqlParameter var3 = new SqlParameter();
var3.ParameterName = "@retval";
var3.SqlDbType = SqlDbType.Int;
var3.Direction = ParameterDirection.Output;
LoginChkCommand.Parameters
myConnection.Open();
LoginChkCommand.ExecuteRea
return (string)LoginChkCommand.Pa
}
finally
{
myConnection.Close();
myConnection.Dispose();
}
}
}
replace all the code you have in the try {} with:
lblmsg.Text = LoginClass.LoginChk(UserNa