Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session.OnEnd Problems (heh heh)

Status
Not open for further replies.

jfrost10

Programmer
Jun 3, 2001
2,004
CA
Hey guys,

Keeping with the global.asa theme of the last post, I'm having probs firing the code within my session_onend code. I access a record (which in teh db locks it using a custom locking scheme), and then close my browser which should end the session. However, my code to perform db cleanup that I have in my session on end never fires! I changed the timeout property to 1 minute, and got rid of the http KeepAlive property. Any thoughts on how to get it to go?

Thanks,
Jack
 
Please post the contents of your global.asa... This is not a bug - it's an undocumented feature...
;-)
 
Hey Jonax,

here's my session end. Now this is using ASP.NET (yes yes, I know I know, there is an asp.net forum. But there's not a whole lot of people developing with this yet, and from what I've seen there shouldn't be too much different with how Session_End works between old school and new way), so the code may look a bit different. I basically just execute a stored procedure, and use a Session variable that I filled previously.

Any help is appreciated! :)
Jack

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
Dim cnCon As New CustomSQLConnectionFunction()
Dim cmCom As New SqlClient.SqlCommand()
With cmCom
.Connection = cnCon.OpenConn
.CommandType = CommandType.StoredProcedure
.CommandText = "up_gen_Unlock"
.Parameters.Add("@strUserName", SqlDbType.VarChar, 50).Value = Session("User").UserID
.ExecuteNonQuery()
End With
cmCom.Connection.Close()
cmCom = Nothing
cnCon = Nothing
End Sub
 
Why do you call it with the sender- and e- parametres???
You don't seem to use them...
...
As I haven't tried the .net stuph yet, I'm not too sure about this, but shouldn't it be called session_ONend ? This is not a bug - it's an undocumented feature...
;-)
 
The stuff in the parameter list is just what Visual Studio.NET puts in there for the global.asa template. I don't use them, but it also doesn't hurt (they use those parameters in alot of template functions).

And no, with asp.NET its just _End, not _OnEnd.

Jack
 
:-(

And have you tried response.writing Session("User").UserID

It does contain what it is supposed to? This is not a bug - it's an undocumented feature...
;-)
 
heh, well here's where the new features with .NET come in real handy. You don't need to do response.write code anymore to see what the value of a variable is. Because ASP.NET apps are basically VB or C# apps with webpages instead of forms, you can actually put a stop in the code and step through it as it executes. So yes, the variable value is right. My problem is that the session_end doesn't fire at all. Even if I put a session.abandon, it doesn't trigger it. I know that there is a timeout function that you can set, but I would think that if you close a browser or abandon the session that the session_end would fire immediately and not wait for the timeout.

Jack
 
Yup, so would I... This is not a bug - it's an undocumented feature...
;-)
 
...and your global.asa is called global.asax???

excerpt from msdn:

One caveat: Although ASP and ASP.NET applications can run side-by-side, even within the same directory, they share no application information. For example, settings made within global.asa will not apply to ASP.NET applications; settings made in global.asax will not apply to ASP applications. Similarly, Application and Session state information is not shared. Session data added for a user in an ASP.NET application is not available for that same user in an ASP application unless it is re-created. This is not a bug - it's an undocumented feature...
;-)
 
Nosing along through msdn I also found this example:

Code:
[Visual Basic]
<Script language=&quot;VB&quot; runat=&quot;server&quot;>
     Sub Session_OnStart()
         'Session start-up code goes here.
     End Sub
     Sub Session_OnEnd()
         'Session clean-up code goes here.
     End Sub
     Sub Security_OnAuthentication(Source As Object, Details as AuthenticationEventArgs)
         'Authentication code goes here.
     End Sub
</script>
[C#]
<Script language=&quot;C#&quot; runat=&quot;server&quot;>
     void Session_OnStart() {
         // Session start-up code goes here.
     }
     void Session_OnEnd() {
         // Session clean-up code goes here.
     }
     void Security_OnAuthentication(Object Source, AuthenticationEventArgs Details) {
         // Authentication code goes here.
     }
</script>

Try including the language parameter, and ONend... This is not a bug - it's an undocumented feature...
;-)
 
Hey Jonax,

Yeah, my global has an asax suffix. Now here's the lowdown on that code you found:

The code that MS gives there is for the Beta release (I've got candidate copy), and for developers not using vs.net to create their aspx pages. This might get weird, but bear with me:

When you create an aspx page in VisualStudio.net, you have the interface part (with the aspx extension) but you also have a .vb/.cs counterpart (which holds all the code for the page, like a class file). The structure of the .vb/.cs files is similar to a cross between VB and Java, so when you code a page, its as if you were coding a form in vb, not writing script in script tags within a text editor.

When Visual Studio creates teh global.asax file, it only has Session_start, Session_end, etc., not the OnEnd as ASP did. Also, I can test and see that the Session_start does get fired, so the file/code is working and accessible...well, except for the session_end
;)

Jack

 
Hi,

just a question : where is database, on some other computer or ?

If it is on other computer then this is know problem and you can search on microsoft site for solution.


Oliver
 
Hey Oliver,

Yes, the db is on another server. What are you thinking the problem is?

Jack
 
Ok, here is explanation :


PRB: Connection to SQL Server from Session_OnEnd() Event May Fail (Q247968)

--------------------------------------------------------------------------------
The information in this article applies to:


Microsoft Active Server Pages


--------------------------------------------------------------------------------


SYMPTOMS
When connecting to a Microsoft SQL Server database from within the Active Server Pages (ASP) event, Session_OnEnd(), the connection to the SQL Server computer may fail with one of the following errors:

Error Number: -2147467259 Error Description: [Microsoft][ODBC SQL Server Driver]Client unable to establish connection
-or-
Error Number: -2147467259 Error Description: [Microsoft][ODBC SQL Server Driver][Named Pipes]ConnectionOpen (CreateFile())

This problem can occur if all of the following conditions are true:
You are trying to establish the connection to SQL Server using the Named Pipes network library.


The SQL Server is on a separate computer from the Internet Information Server (IIS) server.


You are trying to establish the connection to the database from within the Session_OnEnd() event of ASP.





CAUSE
The cause of this problem is twofold. First, when running in the Session_OnEnd() event, the execution takes place under a different user context than in the Session_OnStart() and regular ASP pages. This is a known issue with ASP and is described in more detail in the following Microsoft Knowledge Base article:

Q243828 BUG: Session_OnEnd Changes Security Context of InProcess Component
Second, the Named Pipes network library is being used to connect to the SQL Server computer. When you connect to SQL Server from the ASP page via Named Pipes, the client computer must be validated by SQL Server to use the Named Pipe. If the SQL Server computer is unable to validate (authenticate) the incoming user, then the connection request will fail. This applies even if the connection to SQL Server is being attempting using standard authentication. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
Q159976 Connect to the Microsoft SQL Server via Named Pipes





RESOLUTION
To resolve this problem, follow these steps:

Switch the network library that the client code is using to connect to SQL Server to Transmission Control Protocol/Internet Protocol (TCP/IP). If you use TCP/IP to connect to SQL Server, then the authentication noted earlier does not have to take place. You can do this by configuring the &quot;client configuration&quot; option of the data source name (DSN) that you are using to connect to SQL Server (if you are using a DSN) or by adding a parameter to the connection string telling it to connect using the TCP/IP library. The following Knowledge Base article describes how to add this parameter to the connection string:
Q238949 Set the SQL Server Network Library in an ADO Connection
In addition, you must make sure that SQL Server is configured to listen on the TCP/IP protocol. You can do this by using the Server Network Utility with SQL Server 7.0 or through Setup for SQL Server 6.5. For more information on this, please see the SQL Server Books Online.


If your Web application is running out of process, then the Session_OnEnd() event will fire in the security context of the local IWAM_[ IIS computer name ]. In this scenario, you can also work around the problem by adding a local user account on the SQL Server computer that corresponds to the IWAM_[ IIS computer name ] account. This local user account on the SQL Server computer must have an identical user name and password.





REFERENCES
SQL Server Books Online



(link : )



Bye,
Oliver
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top