Showing posts with label error. Show all posts
Showing posts with label error. Show all posts

Thursday, March 29, 2012

Session varaibles are lost when an error occurs

In VS2003, when debugging (with breakpoints set) a ASP.net web project
(locally), if an error/exception occurs the browser navigates to the "Server
Error" page. After this point it looses all the values stored in the Session
objects, and leads to many other errors, before I must refresh the page and
restart all over again. What the problem can be?
Thanks in advance.Hi,
Before redirecting to the error page the current session is distroyed and a
new session is created. This is the ASP.NET behavior. What you can do is to
catch all unhandled exceptions in the Application_Error from Global file. In
this handler you should clean the error and manually redirect to your custom
error page using Server.Transfer method. This way you prevent the
application from distroying the session.
"So" <.> wrote in message news:O38eKVIUFHA.2712@.TK2MSFTNGP09.phx.gbl...
> In VS2003, when debugging (with breakpoints set) a ASP.net web project
> (locally), if an error/exception occurs the browser navigates to the
"Server
> Error" page. After this point it looses all the values stored in the
Session
> objects, and leads to many other errors, before I must refresh the page
and
> restart all over again. What the problem can be?
> Thanks in advance.
>
Thanks for your reply. Sorry for getting back late.
I tried to do something like this below in global.asax.vb, but I am still
loosing the Session variables. Note that I am not redirecting to any error
page at the moment. What more can be done, and where (on web/docu) can I
read in good details about the ASP.net session handling?
Protected Sub Application_Error(ByVal sender As [Object], ByVal e As
EventArgs)
Dim objErr As Exception =
Server.GetLastError().GetBaseException()
Server.ClearError()
Dim err As String = "<b>Error Caught in Application_Error
event</b><hr><br>" & _
"<br><b>Error in: </b>" &
Request.Url.ToString() & _
"<br><b>Error Message: </b>" &
objErr.Message.ToString() & _
"<br><b>Stack Trace:</b><br>" &
objErr.StackTrace.ToString().Replace("at ", "<br>at ") & _
"<br><a
href='java script:window.location.reload()'>Refresh</a>"
Response.Write(err.ToString())
End Sub
Thanks again for your time.
"Mircea Pleteriu" <mpleteriu@.newsgroup.nospam> schrieb im Newsbeitrag
news:eO46FqJUFHA.228@.TK2MSFTNGP12.phx.gbl...
> Hi,
> Before redirecting to the error page the current session is distroyed and
a
> new session is created. This is the ASP.NET behavior. What you can do is
to
> catch all unhandled exceptions in the Application_Error from Global file.
In
> this handler you should clean the error and manually redirect to your
custom
> error page using Server.Transfer method. This way you prevent the
> application from distroying the session.
> "So" <.> wrote in message news:O38eKVIUFHA.2712@.TK2MSFTNGP09.phx.gbl...
> "Server
> Session
> and
>

Session varaibles are lost when an error occurs

In VS2003, when debugging (with breakpoints set) a ASP.net web project
(locally), if an error/exception occurs the browser navigates to the "Server
Error" page. After this point it looses all the values stored in the Session
objects, and leads to many other errors, before I must refresh the page and
restart all over again. What the problem can be?

Thanks in advance.Hi,

Before redirecting to the error page the current session is distroyed and a
new session is created. This is the ASP.NET behavior. What you can do is to
catch all unhandled exceptions in the Application_Error from Global file. In
this handler you should clean the error and manually redirect to your custom
error page using Server.Transfer method. This way you prevent the
application from distroying the session.

"So" <.> wrote in message news:O38eKVIUFHA.2712@.TK2MSFTNGP09.phx.gbl...
> In VS2003, when debugging (with breakpoints set) a ASP.net web project
> (locally), if an error/exception occurs the browser navigates to the
"Server
> Error" page. After this point it looses all the values stored in the
Session
> objects, and leads to many other errors, before I must refresh the page
and
> restart all over again. What the problem can be?
> Thanks in advance.

Session variable

Hello,
How to declare a variable of session without value (empty) ?
Because If I use a page with a Null Session I have the error :
"Object reference not set to an instance of an object."
on this line :
Line 16: Dim SessionUser As String = Session("USER").ToString

Thank you for your help.
PAB.You can use String.Empty for string values
Hello,
Thanks, I try that.
PAB.
C#

if ( Session["User"] == null)
{

// not exists

}

VB.Net

IF SESSION["USER"] is Nothing THEN

// Not Exists.

END IF

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 Class

Hi all,

I am trying to create a seesion variable in a class. When I try to compile it I get the following error.

CS0103: The name 'Session'does not exist in the class or namespace 'DatabaseConnection.DbConnect'

In the class file I try to create the session variable as follows

Session["CompanyName"]="Name";

Anyone have any ideas of my problem?HttpContext.Current.Session
When you use "Session" in a WebForm, you are accessing a property (Page.Session), which returns an HttpSessionState object, which you then index using ["CompanyName"]. If your class is not derived from Page (or several other classes which have a Session property), then your class doesn't have a Session property for you to use. Fortunately you can use the static method HttpContext.Current to get the current HttpContext object, which then gives you access to session state through its Session property.

AutoFed
Thanks for this.

How do I convert a session object to a boolean type or an integer?
You can use, Convert.ToInt32 and Convert.ToBoolean methods. These are the static methods of "Convert" class, which take object type as the parameter. So, you can pass your session variable to these methods to convert to the required type
It depends on what object you assigned to the session variable. If you assigned an integer to the session variable, e.g. Session["MyInt"] = 16, then you just need to cast it back to an int when you read it out, int myInt = (int)Session["myInt"]. If you assigned a string to the sesson variable, e.g. Session["MyInt"] = "16", and you want to convert that to an int, you can use Int32.Parse. You should only use the Convert functions if you don't know what the type of the object is, since there is some extra overhead in determining what the type is before converting it.

AutoFed

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

Session variable in static method creates error.

Hi. I have a static method that adds a number to a session variable. But when I code a line like this:

Session["CartSubTotal"] = Convert.ToDouble(Session["CartSubTotal"]) + Price;

I get an error like this:

c:\inetpub\wwwroot\aaagolfballs\ShoppingCart.aspx.cs(235): An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Session'

What is a guy to do?If Session["CartSubTotal"] has not been set, that is the error you get.

Check if Session["CartSubTotal"]==null, if so, just set it equal to price rather than trying to add in the existing value of the session variable...
It was null, but I put into the global.asax:


protected void Session_Start(Object sender, EventArgs e)
{
Session["CartSubTotal"] = 0;
}

I still get the same error.
I've got it. For those with the same error, here is how I fixed it. I used these variables in the static method:
System.Web.HttpContext.Current.Session
System.Web.HttpContext.Current.Server

Thanks to all.

Saturday, March 24, 2012

Session variable problem

Hi,

Im having trouble with the code below, I think it is because this code is executed in a class and not on an actual form, I am getting the error "Name 'Session' is not declared.

Is there away to fix this error? Is there an problems with setting the user session variable in a class and not on a form?

Private Function PasswordMatch(ByVal UserName, ByVal PassEntered)
Try
Dim intResult As Integer = 0
Dim intUser As Integer
SqlCmdLogon.CommandText = "sp_CheckPassword"
SqlCmdLogon.Parameters("@dotnet.itags.org.Username").Value = UserName
SqlCmdLogon.Parameters("@dotnet.itags.org.Password").Value = PassEntered
SqlCnnShippers.Open()
SqlCmdLogon.ExecuteNonQuery()
intResult = SqlCmdLogon.Parameters("@dotnet.itags.org.Rows").Value
intUser = SqlCmdLogon.Parameters("@dotnet.itags.org.UserID").Value
If intResult = -1 Then

session("UserID") = SqlCmdLogon.Parameters("@dotnet.itags.org.UserID").Value

Return True
Else
Return False
End IfApparently it is not possible, so passed it to a public readonly property instead then called that on the form to set the session variable instead.

Should be Ok.

I'll mark resolved but if anyone sees aproblem ps let me know.
Can i know how we check there is what stored inside the session?
Session("Name") ="Fish"
label1.text = Session("Name")
'Label should display "Fish"
Apparently it is not possible, so passed it to a public readonly property instead then called that on the form to set the session variable instead.

Should be Ok.

I'll mark resolved but if anyone sees aproblem ps let me know.
That's how it was supposed to be done. You can't let your HTTPContexts be mixed into your class logic.