C# .NET - how to pass memberID to global.asax?

Asked By Judy Lim on 13-Nov-11 02:29 AM
Hi. I had retreive the memberID from the database as well as save the session. I need to pass the memberID session to global.asax to capture the logout time corresponding with the user and the logged in time. The question is how should I pass the session from this page to global.asax page? I'm not sure where I should put the memberID in global.asax. Can someone tell me how I should do?

 try
            {
                string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                connection = new SqlConnection(connectionString);
                connection.Open();
                //prepare sql statements
                string sql = "SELECT * from Member where emailaddress='" + emailaddress + "' And Password='" + password + "'";
                Response.Write(sql);
                command = new SqlCommand(sql, connection);
                SqlDataAdapter da = new SqlDataAdapter(command);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    /* add value to Session */

                    Session["MemberId"] = dt.Rows[0]["MemberID"].ToString();    [save the session]
                    validUser = true;


                    //save emailaddress in session
                    Response.Write("valid user:" + emailaddress);
                    String SQL = "SELECT MemberID FROM Member where emailaddress = @EmailAddress";
                    command.Parameters.AddWithValue("@EmailAddress", emailaddress);

                    [retrieve memberID from database]

                    dataReader = command.ExecuteReader();


Global.asax

try
            {


                string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                connection = new SqlConnection(connectionString);
                connection.Open();


                //retrieve the logid 
                SqlCommand cmd = new SqlCommand("SELECT TOP 1 LogId FROM LoggedInUsers where MemberID = @MemberID ORDER BY LogId DESC");
                //pass the session Session
                int logid = (int)cmd.ExecuteScalar();


                //update record based on logid
                SqlCommand cmd1 = new SqlCommand("UPDATE LoggedInUsers Where LogId = @LogId");


                //prepare sql statements
                string sql = "UPDATE  LoggedInUsers set LogoutTime=@LogoutTime where emailaddress=@EmailAddress";
                //Response.Write(sql + "<br />");


                command = new SqlCommand(sql, connection);
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@LogoutTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                command.Parameters.AddWithValue("@DisplayName", displayname);


                int rowCount = command.ExecuteNonQuery();
                if (rowCount != 0)
Neha Garg replied to Judy Lim on 13-Nov-11 03:14 AM
Hello Judy,

Here is the code which you need to use to pass memberId to Global.asax

protected void Session_Start(Object sender, EventArgs e)
{
WindowsIdentity identity = null;
identity = (WindowsIdentity)(HttpContext.Current.User.Identity);
Kirtan Patel replied to Judy Lim on 13-Nov-11 03:15 AM
in Global.asax you can only use ApplicationState session state will not work at all.ApplicationState is same as SessionState but information you store in ApplicationState is globally available unlike Session.

Application["MemberID"] = 12345

you can access that into Global.asax like below

int MemberID = Convert.ToInt32(Application["MemberID"]);
dipa ahuja replied to Judy Lim on 13-Nov-11 05:38 AM
Untitled document
public static int count = 1;
void Application_Start(object sender, EventArgs e)
{
  
Application["MemberID"] = count;
}

void Session_Start(object sender, EventArgs e)
{
   count =
Convert.ToInt32(Application["myCount"]);
   
Application["MemberID"] = count + 1;
}
 
Now every time when visitor open your site the variable count will be increased by one.
 

► To Get the total at you home page you can write it as:
 
int a = Convert.ToInt32(Application["MemberID"]);
 
Judy Lim replied to dipa ahuja on 13-Nov-11 07:37 AM
Hi.
Thank you, but may I know how to capture the logout time?
Judy Lim replied to Kirtan Patel on 13-Nov-11 07:41 AM
Thank you.
But how do I capture the logout time using my code in global.asax?
dipa ahuja replied to Judy Lim on 13-Nov-11 08:22 AM
You can write the code inside the Session_End:

  void Session_End(object sender, EventArgs e)
  {
    DateTime logoutTime = DateTime.Now;
 
    //store this logoutTime to table
  }
Judy Lim replied to dipa ahuja on 13-Nov-11 08:51 AM
Thank you. But I need to do this way but I cannot figure out where I had gone wrong. There is no error message.

protected Boolean UpdateLoggedInUsersLogoutTime(string emailaddress)
        {
            SqlConnection connection = null;
            SqlCommand command = null;
            Boolean successFlag = false;


            try
            {


                string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                connection = new SqlConnection(connectionString);
                connection.Open();


                //retrieve the logid 
                SqlCommand cmd = new SqlCommand("SELECT TOP 1 LogId FROM LoggedInUsers where MemberID = @MemberID ORDER BY LogId DESC");
                //pass the session Session
             
                int logid = (int)Session["MemberId"];


                //update record based on logid
                SqlCommand cmd1 = new SqlCommand("UPDATE LoggedInUsers set LogoutTime = @LogoutTime Where LogId = @LogId");


                //prepare sql statements
                string sql = "UPDATE  LoggedInUsers set LogoutTime=@LogoutTime where emailaddress=@EmailAddress";
                //Response.Write(sql + "<br />");


                command = new SqlCommand(sql, connection);
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@LogoutTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                command.Parameters.AddWithValue("@EmailAddress", emailaddress);


                int rowCount = command.ExecuteNonQuery();
                if (rowCount != 0)