smr5
smr5

Reputation: 2793

Stored procedure in class and global variable

I have a class called UserInfo. Within this class I call a SQL stored procedure to validate the user. The stored procedure returns an output.

I get the output and store it in the local variable in the class which in this case is only accessible within UserInfo class.

Here's the class:

public class UserInfo
{
    public void validateUser(string username, string password)
    {
        string constring = "Data Source=db;Initial Catalog=Portal;User ID=u;Password=p";

        using (SqlConnection con = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("agentAuthentication", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Parameters.AddWithValue("@password", password);
                cmd.Parameters.Add("@loginresults", SqlDbType.BigInt, 8);
                cmd.Parameters["@loginresults"].Direction = ParameterDirection.Output;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

                int result =Convert.ToInt32(cmd.Parameters["@loginresults"].Value);

            }
        }
    }
}

On the button click of the login page I call this class to validate the user.

 protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            string username = text1.Value.Trim();
            string password = text2.Value.Trim();

            UserInfo u = new UserInfo();
            u.validateUser(username, password);

            int result = 0;

            if (result == 0)
            {
                Response.Write("<script>alert('Incorrect username or password');</script>");
            }

            if (result == 2)
            {
                Response.Write("<script>alert('Your account is inactive');</script>");
            }

            if (result != 0 && result != 2)
            {
                Session["isLoggedIn"] = 1;
                Response.Redirect("~/default.aspx");
            }

        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }

How can I store the returned value from the stored procedure so I can access it in the code behind of the button click?

As you can see the result is declared in two places. I want to declare the result in the class and check the value on the button click event.

The stored procedure return three types of output:

  1. Returns 0 if username or password is incorrect
  2. Returns 2 if account is inactive
  3. Else it return the ID of the user.

Upvotes: 0

Views: 880

Answers (1)

Habib
Habib

Reputation: 300

In the UserInfo class change public void validateUser(string username, string password) to public int validateUser(string username, string password) and return the result. declare this result variable outside of using clause and set its initial value to 0.

In the btnLogin_Click change u.validateUser(username, password); to int result = u.validateUser(username, password); and you are done.

Upvotes: 2

Related Questions