Puseletso Michelle
Puseletso Michelle

Reputation: 141

Need help on session variable

How do i return a Userid in web method and use that returned result in website ASP.NET,make it session variable in order to use it on different webpages in ASP.NET?

[WebMethod] public bool UserLogin(string EmailAdd, string Password) {

        SqlConnection myConnection = new SqlConnection(@"user id=BankUser;password=Computer1;server=(local)\sql2008;database=BillionBank;");
        myConnection.Open();
        SqlCommand myCommand = new SqlCommand("SELECT * FROM [BillionBank].[dbo].[tblCustomerProfile] WHERE EmailAdd='" + EmailAdd + "' AND Password ='" + Password + "'", myConnection);
        SqlDataReader myreader;
        myreader = myCommand.ExecuteReader();
        if (myreader.Read())
        {
            return true;
        }
        else
            return false;
    }

this is my code i want to return a Userid as a result and use it in website as a session variables,when am calling the web method in my website

Upvotes: 1

Views: 989

Answers (3)

Moiz
Moiz

Reputation: 2439

  public string GetUserID(string EmailAdd, string Password)
    {
        SqlConnection myConnection = new SqlConnection(@"user id=BankUser;password=Computer1;server=(local)\sql2008;database=BillionBank;");
        myConnection.Open();
        SqlCommand myCommand = new SqlCommand("SELECT * FROM [BillionBank].[dbo].[tblCustomerProfile] WHERE EmailAdd='" + EmailAdd + "' AND Password ='" + Password + "'", myConnection);
        SqlDataReader myreader;
        string result = "";

        myreader = myCommand.ExecuteReader();
        if (myreader.Read())
        {
            result = Convert.ToString(myreader["UserID"]);
        }

        return result;

    }

protected void btnLogin_Click(object sender, EventArgs e)
{
    Session["UserID"] = GetUserID(YOUR_EMAILID, YOUR_PASSWORD);
}

Upvotes: 0

Reddy
Reddy

Reputation: 66

On A webmethod keep a [WebMethod(EnableSession = true)] then you can return session variable.

Upvotes: 1

middelpat
middelpat

Reputation: 2585

In the web method set the session variable like:

Session["userId"] = 1234;

Then within your application you can just call the session again. Note you'll need to parse the session to int again

int userId
if(int.TryParse(Session["userId"].toString(), out userId)
{
     //do something
}

Upvotes: 1

Related Questions