Showing posts with label global. Show all posts
Showing posts with label global. Show all posts

Monday, March 26, 2012

Session Variable in Application_Error

Hi,

I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?

Thanks in advancePost the code which is failing.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
===================================
<bmukulu@.gmail.comwrote in message news:1174846749.292871.264970@.y66g2000hsf.googlegr oups.com...

Quote:

Originally Posted by

Hi,
>
I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?
>
Thanks in advance
>


On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@.nowhere.com>
wrote:

Quote:

Originally Posted by

Post the code which is failing.
>
Juan T. Llibre, asp.net MVP
asp.net faq :http://asp.net.do/faq/
foros de asp.net, en espaol :http://asp.net.do/foros/
===================================
>
>
>
<bmuk...@.gmail.comwrote in messagenews:1174846749.292871.264970@.y66g2000hsf.g ooglegroups.com...

Quote:

Originally Posted by

Hi,


>

Quote:

Originally Posted by

I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?


>

Quote:

Originally Posted by

Thanks in advance- Hide quoted text -


>
- Show quoted text -


Here is the code:
Sub Application_Error(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "This is an
example"

End If

End Sub
bmukulu@.gmail.com wrote:

Quote:

Originally Posted by

On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@.nowhere.com>
wrote:

Quote:

Originally Posted by

>Post the code which is failing.
>>
>Juan T. Llibre, asp.net MVP
>asp.net faq :http://asp.net.do/faq/
>foros de asp.net, en espaol :http://asp.net.do/foros/
>===================================
>>
>>
>>
><bmuk...@.gmail.comwrote in messagenews:1174846749.292871.264970@.y66g2000hsf.g ooglegroups.com...

Quote:

Originally Posted by

>>Hi,
>>I am trying to add some error handling in a Global.asax file. I am
>>declaring a session variable within the Application_Error procedure.
>>However, everytime i try to pass something into the sessionstate i get
>>a error message of 'Object reference not set to an instance of an
>>object.' Is there any workaround for this? And why am I getting this
>>error?
>>Thanks in advance- Hide quoted text -


>- Show quoted text -


>
Here is the code:
Sub Application_Error(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "This is an
example"
>
End If
>
End Sub
>


You have to check if you have an HttpContext object before you try to
use it. It's not at all certain that the code where the error occurs has
a http context at all.

--
Gran Andersson
_____
http://www.guffa.com
Can you try this and see if it makes a difference ?

Protected Sub Application_Error(sender As [Object], e As EventArgs)

' Get the information about the error
Dim ctxt As HttpContext = HttpContext.Current

Dim except As Exception = ctxt.Server.GetLastError()

Dim errorInfo As String = "<br>Offending URL: " + ctxt.Request.Url.ToString() + "<br>Source: " _
+ except.Source + "<br>Message: " + except.Message + "<br>Stack trace: " + except.StackTrace

ctxt.Response.Write(errorInfo)

' ----------------
' To let the page finish executing we clear the error
' ----------------
ctxt.Server.ClearError()
End Sub

---

Next suggestion :

Without a valid session, you won't be able to store the exception information in it.

Try wrapping the statement in a conditional :

If Not HttpContext.Current.Session Is Nothing Then
Dim ctxt As HttpContext = HttpContext.Current
Dim except As Exception = ctxt.Server.GetLastError()
HttpContext.Current.Session.Add("sExample", except)
End If

That will tell you if your Session is valid.

If it is valid, you'll see the Session sExample set with the Exception info.
If it's not valid, Session sExample will be empty.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
===================================
<bmukulu@.gmail.comwrote in message news:1174849512.334478.233980@.n59g2000hsh.googlegr oups.com...
On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@.nowhere.com>
wrote:

Quote:

Originally Posted by

Post the code which is failing.
>
Juan T. Llibre, asp.net MVP
asp.net faq :http://asp.net.do/faq/
foros de asp.net, en espaol :http://asp.net.do/foros/
===================================
>
>
>
<bmuk...@.gmail.comwrote in messagenews:1174846749.292871.264970@.y66g2000hsf.g ooglegroups.com...

Quote:

Originally Posted by

Hi,


>

Quote:

Originally Posted by

I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?


>

Quote:

Originally Posted by

Thanks in advance- Hide quoted text -


>
- Show quoted text -


Here is the code:
Sub Application_Error(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "This is an
example"

End If

End Sub
Howdy,

You have to call Server.ClearError method after handling Application.OnError
event in order to prevent thread from being aborted (unhandled exception).
Without doing so, ASP.NET runtime assumes exception has not been handled and
stops the execution before session state is updated with chages.

Sub Application_Error(ByVal sender As Object, _
ByVal e As EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso _
TypeOf ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "This is an example"
Server.ClearError()
End If

End Sub

hope this helps

--
Milosz

"bmukulu@.gmail.com" wrote:

Quote:

Originally Posted by

On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@.nowhere.com>
wrote:

Quote:

Originally Posted by

Post the code which is failing.

Juan T. Llibre, asp.net MVP
asp.net faq :http://asp.net.do/faq/
foros de asp.net, en espa?ol :http://asp.net.do/foros/
===================================

<bmuk...@.gmail.comwrote in messagenews:1174846749.292871.264970@.y66g2000hsf.g ooglegroups.com...

Quote:

Originally Posted by

Hi,


Quote:

Originally Posted by

I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?


Quote:

Originally Posted by

Thanks in advance- Hide quoted text -


- Show quoted text -


>
Here is the code:
Sub Application_Error(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "This is an
example"
>
End If
>
End Sub
>
>


Hi Goran,

If HttpContext.Current was null he would get null reference exception. The
problem he described is definitely related to unhandeld exception, that
prevents session state to updated (i tesetd it), therefore he must call
Server.ClearError afterwards.

Best regards
--
Milosz

"G?ran Andersson" wrote:

Quote:

Originally Posted by

bmukulu@.gmail.com wrote:

Quote:

Originally Posted by

On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@.nowhere.com>
wrote:

Quote:

Originally Posted by

Post the code which is failing.
>
Juan T. Llibre, asp.net MVP
asp.net faq :http://asp.net.do/faq/
foros de asp.net, en espa?ol :http://asp.net.do/foros/
===================================
>
>
>
<bmuk...@.gmail.comwrote in messagenews:1174846749.292871.264970@.y66g2000hsf.g ooglegroups.com...
>Hi,
>I am trying to add some error handling in a Global.asax file. I am
>declaring a session variable within the Application_Error procedure.
>However, everytime i try to pass something into the sessionstate i get
>a error message of 'Object reference not set to an instance of an
>object.' Is there any workaround for this? And why am I getting this
>error?
>Thanks in advance- Hide quoted text -
- Show quoted text -


Here is the code:
Sub Application_Error(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "This is an
example"

End If

End Sub


>
You have to check if you have an HttpContext object before you try to
use it. It's not at all certain that the code where the error occurs has
a http context at all.
>
--
G?ran Andersson
_____
http://www.guffa.com
>


Milosz Skalecki [MCAD] wrote:

Quote:

Originally Posted by

Hi Goran,
>
If HttpContext.Current was null he would get null reference exception.


If you read the original post again, you'll see that this is exactly
what he is getting.

Quote:

Originally Posted by

The
problem he described is definitely related to unhandeld exception, that
prevents session state to updated (i tesetd it), therefore he must call
Server.ClearError afterwards.
>
Best regards


--
G?ran Andersson
_____
http://www.guffa.com
Good call Goran, your're right. I thought he could not read the information
already stored- I must have been very sleepy at the time i replied :)

Regards
--
Milosz

"G?ran Andersson" wrote:

Quote:

Originally Posted by

Milosz Skalecki [MCAD] wrote:

Quote:

Originally Posted by

Hi Goran,

If HttpContext.Current was null he would get null reference exception.


>
If you read the original post again, you'll see that this is exactly
what he is getting.
>

Quote:

Originally Posted by

The
problem he described is definitely related to unhandeld exception, that
prevents session state to updated (i tesetd it), therefore he must call
Server.ClearError afterwards.

Best regards


>
--
G?ran Andersson
_____
http://www.guffa.com
>

Session Variable in Application_Error

Hi,
I am trying to add some error handling in a Global.asax file. I am
declaring a session variable within the Application_Error procedure.
However, everytime i try to pass something into the sessionstate i get
a error message of 'Object reference not set to an instance of an
object.' Is there any workaround for this? And why am I getting this
error?
Thanks in advancePost the code which is failing.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
===================================
<bmukulu@.gmail.com> wrote in message news:1174846749.292871.264970@.y66g2000hsf.googlegroups
.com...
> Hi,
> I am trying to add some error handling in a Global.asax file. I am
> declaring a session variable within the Application_Error procedure.
> However, everytime i try to pass something into the sessionstate i get
> a error message of 'Object reference not set to an instance of an
> object.' Is there any workaround for this? And why am I getting this
> error?
> Thanks in advance
>
On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@.nowhere.com>
wrote:
> Post the code which is failing.
> Juan T. Llibre, asp.net MVP
> asp.net faq :http://asp.net.do/faq/
> foros de asp.net, en espa=F1ol :http://asp.net.do/foros/
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3Ddarkred">
>
> <bmuk...@.gmail.com> wrote in messagenews:1174846749.292871.264970@.y66g200=
0hsf.googlegroups.com...
>
>
> - Show quoted text -
Here is the code:
Sub Application_Error(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception =3D Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") =3D "This is an
example"
End If
End Sub
bmukulu@.gmail.com wrote:
> On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@.nowhere.com>
> wrote:
> Here is the code:
> Sub Application_Error(ByVal sender As Object, ByVal e As
> EventArgs)
> ' Fires when an error occurs
> Dim ex As Exception = Server.GetLastError()
> If Not ex.InnerException Is Nothing AndAlso TypeOf
> ex.InnerException Is System.IO.FileNotFoundException Then
> HttpContext.Current.Session("sExample") = "This is an
> example"
> End If
> End Sub
>
You have to check if you have an HttpContext object before you try to
use it. It's not at all certain that the code where the error occurs has
a http context at all.
Gran Andersson
_____
http://www.guffa.com
Can you try this and see if it makes a difference ?
Protected Sub Application_Error(sender As [Object], e As EventArgs)
' Get the information about the error
Dim ctxt As HttpContext = HttpContext.Current
Dim except As Exception = ctxt.Server.GetLastError()
Dim errorInfo As String = "<br>Offending URL: " + ctxt.Request.Url.ToString(
) + "<br>Source: " _
+ except.Source + "<br>Message: " + except.Message + "<br>Stack trace: " + e
xcept.StackTrace
ctxt.Response.Write(errorInfo)
' ----
' To let the page finish executing we clear the error
' ----
ctxt.Server.ClearError()
End Sub
Next suggestion :
Without a valid session, you won't be able to store the exception informatio
n in it.
Try wrapping the statement in a conditional :
If Not HttpContext.Current.Session Is Nothing Then
Dim ctxt As HttpContext = HttpContext.Current
Dim except As Exception = ctxt.Server.GetLastError()
HttpContext.Current.Session.Add("sExample", except)
End If
That will tell you if your Session is valid.
If it is valid, you'll see the Session sExample set with the Exception info.
If it's not valid, Session sExample will be empty.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
===================================
<bmukulu@.gmail.com> wrote in message news:1174849512.334478.233980@.n59g2000h
sh.googlegroups.com...
On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@.nowhere.com>
wrote:
> Post the code which is failing.
> Juan T. Llibre, asp.net MVP
> asp.net faq :http://asp.net.do/faq/
> foros de asp.net, en espaol :http://asp.net.do/foros/
> ===================================
>
> <bmuk...@.gmail.com> wrote in messagenews:1174846749.292871.264970@.y66g2000
hsf.googlegroups.com...
>
>
> - Show quoted text -
Here is the code:
Sub Application_Error(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso TypeOf
ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "This is an
example"
End If
End Sub
Howdy,
You have to call Server.ClearError method after handling Application.OnError
event in order to prevent thread from being aborted (unhandled exception).
Without doing so, ASP.NET runtime assumes exception has not been handled and
stops the execution before session state is updated with chages.
Sub Application_Error(ByVal sender As Object, _
ByVal e As EventArgs)
' Fires when an error occurs
Dim ex As Exception = Server.GetLastError()
If Not ex.InnerException Is Nothing AndAlso _
TypeOf ex.InnerException Is System.IO.FileNotFoundException Then
HttpContext.Current.Session("sExample") = "This is an example"
Server.ClearError()
End If
End Sub
hope this helps
Milosz
"bmukulu@.gmail.com" wrote:

> On Mar 25, 1:24 pm, "Juan T. Llibre" <nomailrepl...@.nowhere.com>
> wrote:
> Here is the code:
> Sub Application_Error(ByVal sender As Object, ByVal e As
> EventArgs)
> ' Fires when an error occurs
> Dim ex As Exception = Server.GetLastError()
> If Not ex.InnerException Is Nothing AndAlso TypeOf
> ex.InnerException Is System.IO.FileNotFoundException Then
> HttpContext.Current.Session("sExample") = "This is an
> example"
> End If
> End Sub
>
Hi Goran,
If HttpContext.Current was null he would get null reference exception. The
problem he described is definitely related to unhandeld exception, that
prevents session state to updated (i tesetd it), therefore he must call
Server.ClearError afterwards.
Best regards
--
Milosz
"G?ran Andersson" wrote:

> bmukulu@.gmail.com wrote:
> You have to check if you have an HttpContext object before you try to
> use it. It's not at all certain that the code where the error occurs has
> a http context at all.
> --
> G?ran Andersson
> _____
> http://www.guffa.com
>
Good call Goran, your're right. I thought he could not read the information
already stored- I must have been very sleepy at the time i replied :)
Regards
--
Milosz
"G?ran Andersson" wrote:

> Milosz Skalecki [MCAD] wrote:
> If you read the original post again, you'll see that this is exactly
> what he is getting.
>
> --
> G?ran Andersson
> _____
> http://www.guffa.com
>
Milosz Skalecki [MCAD] wrote:
> Hi Goran,
> If HttpContext.Current was null he would get null reference exception.
If you read the original post again, you'll see that this is exactly
what he is getting.

> The
> problem he described is definitely related to unhandeld exception, that
> prevents session state to updated (i tesetd it), therefore he must call
> Server.ClearError afterwards.
> Best regards
G?ran Andersson
_____
http://www.guffa.com

Thursday, March 22, 2012

Session Variables

Hey question for anyone who can answer...

Should I define all me Session vars in the global.asax page as I would in asp? and what is the syntax in .net.

thanks...

rich

Im sure there is probably other, more complicated, ways to declare session variables...but i just do it the same way in .net that I did in asp...just do session("myvar") to create a session variable...global.asax is a fine place to do that i think.

MajorCats


rperreta wrote:

Hey question for anyone who can answer...

Should I define all me Session vars in the global.asax page as I would in asp? and what is the syntax in .net.

thanks...

rich


Hi rich,
Please check this link...Session State Asp.NET
Hope this helps

Hello,

BTW, you can find a really good FAQ about the use of session variables here:ASP.Net Session Variables FAQ

HTH,


Thank you everyone for your help... I think I have my answers...

-rjp

Session Variables

I have a website I wrote where I store information that I need from page to page in session variables. I set the variable in the global.aspx to "". Then in my code I simply reference it like: Session("CheckType") = "DIRECT"

Now would anyone know why this would sometimes not keep? Some of my session variables work fine and keep their values from page to page. There is one however where I can assign a value on one page for it and it never keeps, so when I reference the value on another page it doesn't work. Ideas?

Thanks!It won't stay in memory if the session times out. Is that what's happening?

Tuesday, March 13, 2012

Session Variables ~ Counting People On a Web site

Hi All,

I have two questeions:

I would like to try and count how many people are on a web site at one time.....

Can this be done in the global asax file with session variables ?

I have written an short statement below , will this work......?

Also when using session variables,, do I ned to destroy them on session finish? if so how would i do that....

rgs

Tony

<script runat="server"
Sub Application_Start(Sender As Object, E As EventArgs)

Application("HowMany") = Application("HowMany") +1

Application("AllowAdd") = True
Application("MaxTries") = 3

End Sub

Sub Application_End(Sender As Object, E As EventArgs)
' Code that runs on application shutdown
End Sub

Sub Application_Error(Sender As Object, E As EventArgs)
' Code that runs when an unhandled error occurs
End Sub

Sub Session_Start(Sender As Object, E As EventArgs)
Session("NumTries")=0
End Sub

Sub Session_End(Sender As Object, E As EventArgs)
' Code that runs when a session ends
End Sub

</scriptYou will need an application Variable..

Application("Hits") = 0;

tehn on Session Start you increase that variable:

Application("Hits") += 1;

Note: you might have to do some casting, I'm not sure how that is in VB.

HTH,
Covo
BTW, you can't use the session variables these variables expire with the session, the application variables only expire when the application is re-started.

If you want something more persistent, you will have to implement a method to store your hits in a database/file.

Covo
Hi,

I guess something like this:


if(Application["Count"] != null)
{
Application["Count"] = ConvertTo.Int32(Application["Count"]) + 1;
}

else
{ Application["Count"] = 1; }


Try something like this...


' when the application starts set the variable to 0
Sub Application_Start(Sender As Object, E As EventArgs)

Application("HowMany") = 0

End Sub

' when a new user visits the site, a new session opens... so, increment the variable
Sub Session_Start(Sender As Object, E As EventArgs)

Application("HowMany") = Application("HowMany") + 1

End Sub

' when their session expires, decrement the variable
Sub Session_End(Sender As Object, E As EventArgs)

Application("HowMany") = Application("HowMany") - 1

End Sub

session variables and global.aspx.cs - where do i put them?

if i put an array in

public class Global : System.Web.HttpApplication
{

//e.g.

public static int [] count=new int[5]

}

and then refer to it in my page .cs files as Global.count[0]... [4] will
every session in my application be accessing the same count array or will
they each have their own?

I guess i am really saying is, is there a difference between the above and
:

protected void Session_Start(Object sender, EventArgs e)
{

Sesson["count"]=new int[5]

}

It strikes me that if they are the same then the first option is better
because i don't have to keep casting from object to int everytime i access
the array in web pages.Hi Tony,

You should think of the Application scope the same way you would think of Environment variables.
They are not only global, but they are also readable and writable to all users of the system at the same
time.

Session is better to use if the values are not final or need to be changed per user.
A session is created for each user making a request.

____________________________________
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Business | Personal