[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 956
  • Last Modified:

Session_Start in Global.asax

Hello everybody,
I have a counter on my site, which I handle myself. I put a code for incrementing a counter by 1 in my Session_Start method of Global.asax.cs file:

protected void Session_Start(Object sender, EventArgs e)
{
      SqlConnection conn = null;
      string connStr = ConfigurationSettings.AppSettings[ "ConnStr" ];
      
      try
      {
            conn = new SqlConnection( connStr );
            conn.Open();
            string sql = "UPDATE counter_table SET counter = counter + 1 WHERE id = 1";
            SqlCommand cmd = new SqlCommand( sql, conn );
            cmd.ExecuteNonQuery();
      }            
      finally
      {
            if( conn != null )
                  conn.Close();
      }
}

It works absolutelly fine on my local server, but when I upload it to the web-server (of my web-hosting company), it behaves a bit strange.

It does increase the counter when someone comes first to the web-site. Then, if this someone goes to another page and returnes to the main page, the counter gets incremented again... but only once - if this repeated (go to a different page and come back on the main one), the counter stays the same.

I got nothing incrementing in my page load - my Global.asax is empty beside the code you saw above + InitializeComponents in the constructor... Any idea what can cause it?

Thanks,
Yurich
0
Yurich
Asked:
Yurich
  • 4
  • 3
  • 2
2 Solutions
 
Sammy AgeilCommented:
added Application.Lock(); and Application.Unlock();

protected void Session_Start(Object sender, EventArgs e)
{
     SqlConnection conn = null;
     string connStr = ConfigurationSettings.AppSettings[ "ConnStr" ];
     
     try
     {
         Application.Lock(); //lock the session for concurrency reasons
          conn = new SqlConnection( connStr );
          conn.Open();
          string sql = "UPDATE counter_table SET counter = counter + 1 WHERE id = 1";
          SqlCommand cmd = new SqlCommand( sql, conn );
          cmd.ExecuteNonQuery();
     }          
     finally
     {
           Application.Unlock(); //Unlock the session
          if( conn != null )
               conn.Close();
     }
}
0
 
YurichAuthor Commented:
thanks,
tried it and no difference...

and if fact it should not be as it gets into finally before it gets to the next page and pack to the main page... I'd say probably lock in the session_start and unlock it in the Session_End but I'm not sure if it's going to effect anything else...

Regs,
Yurich
0
 
the_paabCommented:
I think that Application.Lock and Unlock is used to provide access synchronization of appllicationstate object:
  Application.Lock();
  Application["UsersOnline"] = (int)Application["UsersOnline"] + 1;
  Application.UnLock();
...so it has no influens to your problem.

What is your session timeout? Can you show us your web.config?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Sammy AgeilCommented:
Yurich,
In this case I would use a variable to flag the counter variable with a session variable for example
check if that variable doesnt exist increment your counter, and if it does exist dont do anything?

Just a thought to stop the incrementing when going back one page or two

0
 
YurichAuthor Commented:
Thank you guys,

that's a bit for the session timeout:

<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20" />

so, the timeout is 20 seconds...

2.sammy I thought of doing it as well, but that would be a work around, and I'll do it if I can't find a proper solution for this problem.

Regs,
Yurich
0
 
the_paabCommented:
It is 20 minutes.
0
 
the_paabCommented:
Try to protocol time of session ending, I hope it could help you to discover reason of session recreating. Sorry about the VB sample, I hope you can adopt it to C#:

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        FileOpen(1, "c:\esfile.log", OpenMode.Append)
        WriteLine(1, Now)
        FileClose(1)
    End Sub
0
 
YurichAuthor Commented:
yes, of course it's 20 minutes. I'll probably have to do this log to see weather it's really loggin off.

Thanks,
Yurich
0
 
YurichAuthor Commented:
i haven't got time to sort it out, but I'll just close the question.

thank you for your help guys.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now