Link to home
Start Free TrialLog in
Avatar of riskyricky1972
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.Parameters["@retval"].Value)

protected void cmdlogin_Click(object sender, EventArgs e)
    {
        SqlConnection myConnection = new SqlConnection();
        try
        {
            //create db connection and open it
            string ConnectionString = ConfigurationSettings.AppSettings["DBconnection"];
            string CommandText = "up_IsValidLogon";
            myConnection.ConnectionString = ConnectionString;

            SqlCommand LoginChkCommand = new SqlCommand(CommandText, myConnection);
            LoginChkCommand.CommandType = CommandType.StoredProcedure;
            //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.ExecuteReader();
              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();
        }

    }
Avatar of steveberzins
steveberzins

   public class LoginClass
    {
        public static string LoginChk(string userName, string password)
        {

            SqlConnection myConnection = new SqlConnection();

            try
            {

                //create db connection and open it
                myConnection.ConnectionString = ConfigurationSettings.AppSettings["DBconnection"];

                SqlCommand LoginChkCommand = new SqlCommand("up_IsValidLogon", myConnection);
                LoginChkCommand.CommandType = CommandType.StoredProcedure;

                //need to add parameters
                //**********************
                SqlParameter var1 = new SqlParameter();
                var1.ParameterName = "@username";
                var1.SqlDbType = SqlDbType.VarChar;
                var1.Value = userName;
                LoginChkCommand.Parameters.Add(var1);

                SqlParameter var2 = new SqlParameter();
                var2.ParameterName = "@password";
                var2.SqlDbType = SqlDbType.VarChar;
                var2.Value = password;
                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();
                LoginChkCommand.ExecuteReader();
                return (string)LoginChkCommand.Parameters["@retval"].Value;

            }
            finally
            {
                myConnection.Close();
                myConnection.Dispose();
            }
        }
    }

replace all the code you have in the try {} with:
lblmsg.Text = LoginClass.LoginChk(UserName.Text,  UserPass.Text);
Avatar of riskyricky1972

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 :)
Error message: Unable to cast object of type 'System.Int32' to type 'System.String'
point to return (string)LoginChkCommand.Parameters["@retval"].Value; One line before finally
Replace the following line:
return (string)LoginChkCommand.Parameters["@retval"].Value;

with this:
return Convert.ToString(LoginChkCommand.Parameters["@retval"].Value);
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"
{
}
ASKER CERTIFIED SOLUTION
Avatar of Member_2_3737922
Member_2_3737922

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
nice...