Search code examples
c#asp.netsession-variableswebmethod

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


Solution

  •   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);
    }