Skip to main content

Posts

Showing posts from November, 2009

Count Active Sessions in ASP.Net

In order to monitor load on a site I've built and support, I wanted to add a simple counter to show how many active sessions were currently being handled by the web application. The first technique was to use a counter variable held in the Application context. This was pretty straightforward - in global.asax increment the counter in the Session_Start event and decrement in the Session_End:   1    Sub Session_Start( ByVal sender As Object , ByVal e As EventArgs)   2     3        'Increment session count   4        Application.Lock()   5        Application( "SessionCount" ) += 1   6        Application.UnLock()   7     8    End Sub   9      10    Sub Session_End( ByVal sender As Object , ByVal e As EventArgs)    11      12        'Decrement session count    13        Application.Lock()    14        Application( "SessionCount" ) = Application( "SessionCount" ) - 1    15        Application.UnLock()    16      17  

Paging with SQL Server and MySQL

Paging sets of database records in a web application efficiently has long been a relatively tricky undertaking. Whilst web form controls such as the GridView offer control based paging very easily, they will still hit the database for all the records before processing the paging within the web application, and hence unnecessary amounts of data are passed between database and web server. Database based techniques - only retrieving the particular rows you require in each request - are therefore generally to be preferred. But some of those provide their own headaches - for example as well as retrieving the particular set of rows for a page, you will often want to know what the full count of records to be able to display a message along the lines of: 200 records found. Page 1 of 20 . SQL Server Paging Prior to the 2005 version, I would do this using SQL Server with two queries - one to get the page of data and another, using the same WHERE clause, to get the count. With SQL S