Showing posts with label project. Show all posts
Showing posts with label project. 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

Hi

In my ASP.NET project, I have a session variable that is assigned on a login page. In another .cs file (in another directory), I'm trying to use
if((string)Session["variable"] == "something"

When I compile, it tells me that Session does not exist in the calss or namespace

My hypothesis is that it is because this directory is compiled before the directory that actually has the Session variable's declaration. Any ideas on how to fix this

thanks

George"Session" is not a global variable, it is a property of the
System.Web.UI.Page object.
When you use Session["variable"] inside an aspx page, it is actually
translating to this.Session["variable"].
When you are in another class which is not an aspx page derived off of the
Page class, it has no access to the Session object.

You either need to pass a reference to the Session object into your other
class (yuck), or pass the value itself as a parameter/property/etc.

--
Michael J. Mooney
MCP+SB, MCAD, MCSD
"George" <anonymous@.discussions.microsoft.com> wrote in message
news:CE2AB820-0B7C-43E0-BCA6-E3A654E6E18A@.microsoft.com...
> Hi,
> In my ASP.NET project, I have a session variable that is assigned on a
login page. In another .cs file (in another directory), I'm trying to use
> if((string)Session["variable"] == "something")
> When I compile, it tells me that Session does not exist in the calss or
namespace.
> My hypothesis is that it is because this directory is compiled before the
directory that actually has the Session variable's declaration. Any ideas
on how to fix this?
> thanks.
> George
Or you can grab the application object and then access the session:

using System.Web; //Need this at top

HttpApplication ha = HttpContext.Current.ApplicationInstance;

string mySessionValue = ha.Session["MySessionValue"];

Very simple:

VB.NET

Dim ha As HttpApplication = HttpContext.Current.ApplicationInstance
Dim mySessionValue As String = ha.Session("MySessionValue")

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"Michael J. Mooney" <mike_mooney@._yahoo.com.NOSPAM> wrote in message
news:%23tyjDyLTEHA.2336@.TK2MSFTNGP10.phx.gbl...
> "Session" is not a global variable, it is a property of the
> System.Web.UI.Page object.
> When you use Session["variable"] inside an aspx page, it is actually
> translating to this.Session["variable"].
> When you are in another class which is not an aspx page derived off of the
> Page class, it has no access to the Session object.
> You either need to pass a reference to the Session object into your other
> class (yuck), or pass the value itself as a parameter/property/etc.
> --
> Michael J. Mooney
> MCP+SB, MCAD, MCSD
> "George" <anonymous@.discussions.microsoft.com> wrote in message
> news:CE2AB820-0B7C-43E0-BCA6-E3A654E6E18A@.microsoft.com...
> > Hi,
> > In my ASP.NET project, I have a session variable that is assigned on a
> login page. In another .cs file (in another directory), I'm trying to use
> > if((string)Session["variable"] == "something")
> > When I compile, it tells me that Session does not exist in the calss or
> namespace.
> > My hypothesis is that it is because this directory is compiled before
the
> directory that actually has the Session variable's declaration. Any ideas
> on how to fix this?
> > thanks.
> > George

Session Variable

Hi,
In my ASP.NET project, I have a session variable that is assigned on a login
page. In another .cs file (in another directory), I'm trying to use
if((string)Session["variable"] == "something")
When I compile, it tells me that Session does not exist in the calss or name
space.
My hypothesis is that it is because this directory is compiled before the di
rectory that actually has the Session variable's declaration. Any ideas on
how to fix this?
thanks.
George"Session" is not a global variable, it is a property of the
System.Web.UI.Page object.
When you use Session["variable"] inside an aspx page, it is actually
translating to this.Session["variable"].
When you are in another class which is not an aspx page derived off of the
Page class, it has no access to the Session object.
You either need to pass a reference to the Session object into your other
class (yuck), or pass the value itself as a parameter/property/etc.
Michael J. Mooney
MCP+SB, MCAD, MCSD
"George" <anonymous@.discussions.microsoft.com> wrote in message
news:CE2AB820-0B7C-43E0-BCA6-E3A654E6E18A@.microsoft.com...
> Hi,
> In my ASP.NET project, I have a session variable that is assigned on a
login page. In another .cs file (in another directory), I'm trying to use
> if((string)Session["variable"] == "something")
> When I compile, it tells me that Session does not exist in the calss or
namespace.
> My hypothesis is that it is because this directory is compiled before the
directory that actually has the Session variable's declaration. Any ideas
on how to fix this?
> thanks.
> George
Or you can grab the application object and then access the session:
using System.Web; //Need this at top
HttpApplication ha = HttpContext.Current.ApplicationInstance;
string mySessionValue = ha.Session["MySessionValue"];
Very simple:
VB.NET
Dim ha As HttpApplication = HttpContext.Current.ApplicationInstance
Dim mySessionValue As String = ha.Session("MySessionValue")
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
****************************************
********
Think Outside the Box!
****************************************
********
"Michael J. Mooney" <mike_mooney@._yahoo.com.NOSPAM> wrote in message
news:%23tyjDyLTEHA.2336@.TK2MSFTNGP10.phx.gbl...
> "Session" is not a global variable, it is a property of the
> System.Web.UI.Page object.
> When you use Session["variable"] inside an aspx page, it is actually
> translating to this.Session["variable"].
> When you are in another class which is not an aspx page derived off of the
> Page class, it has no access to the Session object.
> You either need to pass a reference to the Session object into your other
> class (yuck), or pass the value itself as a parameter/property/etc.
> --
> Michael J. Mooney
> MCP+SB, MCAD, MCSD
> "George" <anonymous@.discussions.microsoft.com> wrote in message
> news:CE2AB820-0B7C-43E0-BCA6-E3A654E6E18A@.microsoft.com...
> login page. In another .cs file (in another directory), I'm trying to use
> namespace.
the
> directory that actually has the Session variable's declaration. Any ideas
> on how to fix this?
>

Monday, March 26, 2012

Session Variable Disappeared

Hello,

I'm using MasterPages to create the layout for all of the pages in my project. Before I converted the Default.aspx page over to the masterpage format this worked.

I have two pages, login.aspx and default.aspx, of course I'm using Forms Authentication. All that works fine, you try to get to default it prompts for login.

Well I used a custom function for the login, it compares data entered to that in a database, if it passed it also returns a variable containing there Trust Level. (String)

At the end of the Button Submit Click but before I redirect the authenticated user to the page they requested I use this code to store trust level in the Session object.

Session("Trust")=TRUST

When the default page loads it shows/hides elements base on thier trust. Now after I've switched over to master pages the code no longer works, it's returning an empty string for Session("Trust")

Example:

<% If Session("Trust") = "Admin" Then %>
Your Access Level is Administrator
<% Else %>
Your Access Level is <% Response.Write(Session("Trust")) %>
<% End If %
I'm loggin in with the Admin Account with a trust of "Admin" and it's just returning an empty string.

Any ideas?What are you using to redirect? If you are not currently, you should be using the RedirectFromLoginPage method.

HTH...
CHris
Thx for the reply,

I figured it out, it was because I deleted the Global.* file. D'OH

Session variable expirement.

Hello, all.
Is there any case that session vaiables expire before the time-out?
Now, I'm developing the UI project that use the webservice project.
Ocationally I met the situation that I mentioned before.
Thanks.bluewind44 wrote:
> Hello, all.
> Is there any case that session vaiables expire before the time-out?
> Now, I'm developing the UI project that use the webservice project.
> Ocationally I met the situation that I mentioned before.
> Thanks.
Session variables don't expire before the end of the session.
They can be *removed* before the end, or you have a different
session than you think.
You mention WebService. Webservices by default do *not* use
Session (for speed). So it you try to store something in Session in one
call and then use that in a next call, the value is gone (or even more
drastic: "Session" is null, so you get an error when you try to store anythi
ng)
To enable sessions for webservices:
1) use [WebMethod(EnableSession=true)] on the webservice method
2) create a CookieContainer (that's the name, I think) and add it to
the webservice proxy at the calling side (there is a property with a similar
name).
Then use the same proxy or at least the same container for both calls.
Hans Kesting
Thanks for the reply.
Actually I don't use session varaiable on the webservice project.
I just use in a UI Projects and save it as a string variable.
Then I call my webservie project by using the string variable.
You mentioned about removement of session variable.
What cause removing the session variable ?
Kevin Lee
If, for some reason, the ASP.NET worker process crashes and gets recycles,
or the IIS worker process crashes and is recycled, you will lose all session
variables.
-Altaf[MVP]
----
--
All that glitters has a high refractive index.
www.mendhak.com
"bluewind44" <bluewind44@.gmail.com> wrote in message
news:1129714096.221184.53580@.g44g2000cwa.googlegroups.com...
> Thanks for the reply.
> Actually I don't use session varaiable on the webservice project.
> I just use in a UI Projects and save it as a string variable.
> Then I call my webservie project by using the string variable.
> You mentioned about removement of session variable.
> What cause removing the session variable ?
> --
> Kevin Lee
>
Unless you are using State Server or SQL Server
to maintain state for your applications, of course.
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/
======================================
"S.M. Altaf [MVP]" <smaltaf@.PLEASEDONTSPAMMEmsn.com> wrote in message
news:eMpGPcJ1FHA.916@.TK2MSFTNGP10.phx.gbl...
> If, for some reason, the ASP.NET worker process crashes and gets recycles,
or the IIS
> worker process crashes and is recycled, you will lose all session variable
s.
> -Altaf[MVP]
> ----
--
> All that glitters has a high refractive index.
> www.mendhak.com

> "bluewind44" <bluewind44@.gmail.com> wrote in message
> news:1129714096.221184.53580@.g44g2000cwa.googlegroups.com...
>
Thanks really for all replies. ^_^

Session variable expirement.

Hello, all.

Is there any case that session vaiables expire before the time-out?

Now, I'm developing the UI project that use the webservice project.
Ocationally I met the situation that I mentioned before.

Thanks.bluewind44 wrote:
> Hello, all.
> Is there any case that session vaiables expire before the time-out?
> Now, I'm developing the UI project that use the webservice project.
> Ocationally I met the situation that I mentioned before.
> Thanks.

Session variables don't expire before the end of the session.
They can be *removed* before the end, or you have a different
session than you think.

You mention WebService. Webservices by default do *not* use
Session (for speed). So it you try to store something in Session in one
call and then use that in a next call, the value is gone (or even more
drastic: "Session" is null, so you get an error when you try to store anything)

To enable sessions for webservices:
1) use [WebMethod(EnableSession=true)] on the webservice method
2) create a CookieContainer (that's the name, I think) and add it to
the webservice proxy at the calling side (there is a property with a similar name).
Then use the same proxy or at least the same container for both calls.

Hans Kesting
Thanks for the reply.

Actually I don't use session varaiable on the webservice project.
I just use in a UI Projects and save it as a string variable.
Then I call my webservie project by using the string variable.

You mentioned about removement of session variable.
What cause removing the session variable ?

--------
Kevin Lee
If, for some reason, the ASP.NET worker process crashes and gets recycles,
or the IIS worker process crashes and is recycled, you will lose all session
variables.

-Altaf[MVP]
------------------------
All that glitters has a high refractive index.
www.mendhak.com

"bluewind44" <bluewind44@.gmail.com> wrote in message
news:1129714096.221184.53580@.g44g2000cwa.googlegro ups.com...
> Thanks for the reply.
> Actually I don't use session varaiable on the webservice project.
> I just use in a UI Projects and save it as a string variable.
> Then I call my webservie project by using the string variable.
> You mentioned about removement of session variable.
> What cause removing the session variable ?
> --------
> Kevin Lee
Unless you are using State Server or SQL Server
to maintain state for your applications, of course.

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/
======================================
"S.M. Altaf [MVP]" <smaltaf@.PLEASEDONTSPAMMEmsn.com> wrote in message
news:eMpGPcJ1FHA.916@.TK2MSFTNGP10.phx.gbl...
> If, for some reason, the ASP.NET worker process crashes and gets recycles, or the IIS
> worker process crashes and is recycled, you will lose all session variables.
> -Altaf[MVP]
> ------------------------
> All that glitters has a high refractive index.
> www.mendhak.com

> "bluewind44" <bluewind44@.gmail.com> wrote in message
> news:1129714096.221184.53580@.g44g2000cwa.googlegro ups.com...
>> Thanks for the reply.
>>
>> Actually I don't use session varaiable on the webservice project.
>> I just use in a UI Projects and save it as a string variable.
>> Then I call my webservie project by using the string variable.
>>
>> You mentioned about removement of session variable.
>> What cause removing the session variable ?
>>
>> --------
>> Kevin Lee
>>
Thanks really for all replies. ^_^

Session variable is not transferred to an asp page?

I have a asp.net project with all major pages written in aspx, but I want to keep an existing asp page in the same directory as the aspx files. But when the asp page gets loaded, the session variable is not recognized. Is this the case and why?To be specific, when I load the asp.net app using the address of http://localhost/myproj/ it works fine, and the asp page could read the session variable assigned by an aspx page. But if I use my computer name, say http://keystrokes_win2k/myproj/ then it doesn't work.

Any ideas?
No, the ASP and ASP.NET sessions are two separate objects that do not share their data. There are some third party session objects that you can use to communicate between the two environments, though.
What is the third party session object, please provide some lead ?

Saturday, March 24, 2012

session variable only accessable through page behind class instance?

Are session variables available to any class instance within a project namespace or only for use for the form codebehind class?

I have tried to set a session variable on a code behind class and then retrieve its value in a class instance used by the codebehind class and I get errors. Maybe I'm just not meant to access that way and only codebehind classes (classes that inherit System.Web.UI.Page) can use them?As far as I know session variables are only available in aspx pages.

That does not prevent you from passing value of your session variables to any of your business tier objects.
It's more a question of context.
Within the context of the ASP.NET process, any class can get to the session using HttpContext.Current.Session.
Thanks very much for the help!

Session variable VS HREF parameters...

I already use session variable in my project to set a session timeout when
the user doesn't do anything for 10 minutes.

When I call other pages, I often use parameters in HREF link.

I was wondering if it was better to pass parameter from page to page as
session variable instead of doing it in HREF link?

Thx for the hint !The first part of your message is very confusing to me, as Sessions time out
all by themselves. However, I can help you with your other question.

> When I call other pages, I often use parameters in HREF link.
> I was wondering if it was better to pass parameter from page to page as
> session variable instead of doing it in HREF link?

You have a couple of issues here. When you pass data via URL, you are
exposing it to the user, which can be a security risk, so one consideration
is how sensitive the data is. If it's not sensitive, you're fine, as long as
you make sure that the user can't create a parameterized URL that would
cause some problem. One of the advantages of using QueryString parameters is
that the user can bookmark a dynamic page, as the bookmark will have the
parameters in it.

As for Sessions, they can be problematic as well, since they time out after
a certain interval of inactivity. As long as you make sure to handle this
eventuality, Session is fine.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"+The_Taco+" <dominic.feron@.dessausoprin.com> wrote in message
news:eZ2jciO7DHA.488@.TK2MSFTNGP12.phx.gbl...
> I already use session variable in my project to set a session timeout when
> the user doesn't do anything for 10 minutes.
> When I call other pages, I often use parameters in HREF link.
> I was wondering if it was better to pass parameter from page to page as
> session variable instead of doing it in HREF link?
> Thx for the hint !
I think he means that he is using session as a crewd way to timeout a user
by setting session.timeout = 10 and checking for it on each postback.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Kevin Spencer" <kevin@.takempis.com> wrote in message
news:%23%23MS0$O7DHA.1852@.TK2MSFTNGP10.phx.gbl...
> The first part of your message is very confusing to me, as Sessions time
out
> all by themselves. However, I can help you with your other question.
> > When I call other pages, I often use parameters in HREF link.
> > I was wondering if it was better to pass parameter from page to page as
> > session variable instead of doing it in HREF link?
> You have a couple of issues here. When you pass data via URL, you are
> exposing it to the user, which can be a security risk, so one
consideration
> is how sensitive the data is. If it's not sensitive, you're fine, as long
as
> you make sure that the user can't create a parameterized URL that would
> cause some problem. One of the advantages of using QueryString parameters
is
> that the user can bookmark a dynamic page, as the bookmark will have the
> parameters in it.
> As for Sessions, they can be problematic as well, since they time out
after
> a certain interval of inactivity. As long as you make sure to handle this
> eventuality, Session is fine.
> --
> HTH,
> Kevin Spencer
> .Net Developer
> Microsoft MVP
> Big things are made up
> of lots of little things.
>
> "+The_Taco+" <dominic.feron@.dessausoprin.com> wrote in message
> news:eZ2jciO7DHA.488@.TK2MSFTNGP12.phx.gbl...
> > I already use session variable in my project to set a session timeout
when
> > the user doesn't do anything for 10 minutes.
> > When I call other pages, I often use parameters in HREF link.
> > I was wondering if it was better to pass parameter from page to page as
> > session variable instead of doing it in HREF link?
> > Thx for the hint !

Tuesday, March 13, 2012

Session variables disappearing...

Hi everyone,

I have a very strange problem, since this works fine on my other project that I have completed.

I store user information in session variables, one important one being "AllowAccess" which is checked on every page to see if the user can actually go there (so they cannot just type in url's etc). This is simple and works a treat on my last project.

Anyway, in this project the user logs in and the session variables get initialised etc etc

Session("userName") = Me.tb1.text

After I check the password I Server.Transfer("Home.aspx") where (like all screens) it does a basic check to see if Session("AllowedAccess") <> True. The first time you go to the page it is true (have quad checked with debug!). BUT, even with all the code commented out, as soon as you post back anything for some reason Session("AllowedAccess") is equal to "nothing". There is absolutely no code anywhere on the page (it's all commented out) except:


If Session("AllowedAccess") <> True Then
Server.Transfer("index.aspx")
End If

First time on the page it works, second time the value disappears!!

Does anyone know why this is happening? It must be something simple!!

Cheers
AndrewHave you not disabled session for the application. I don't remember the details but you can do this at page and application level. I'm not sure with .NET if the IIS session setting overrides/works with asp.net.
Is there an advantage to using session variables for this purpose rather than using the web.config?
Can we see the code where you set the Session variables and then the Page_Load portion up to where the check is performed?
Sure,

The session variable is set in the previous page:

Session("AllowAccess") = True

And there is no other code in Home.aspx, just:

If Session("AllowAccess") <> True Then
Server.Transfer("index.aspx")
End If

Hope you can work out an answer, this has stumped other .NET people in my office :/

Andrew
Here's my hypothesis:
The session management relies on a session cookie that has to be sent to the client in the header (like all cookies).
When you're Server.Transferring, though, you're actually aborting the current thread, resulting in the headers not having the time to be sent to the client.
Try it without the transfer, just to test this hypothesis.
The problem is that this works with my other project, running off the same PC.

You can imagine my frustration after clicking "New Project", starting work and not being able to get around this problem when it had worked flawlessly in the other project!

I use Server.Transfers in my other project but that's not really the problem since it seems the session variables disappear on post back!!

Still needing help...

Andrew
Would that be considered a bug? That had crossed my mind, but I figured that you guys had undoubtedly thought of that already.

It does make perfect sense though since the current Context is lost, so would the unsent Session data...
What do you mean it works with your other project? You mean that if you copy the same page to another project, it works??
Did you try removing the Server.Transfer, just to check?
In my other project I set up the session variables the same way and I use Server.Transfer's to navigate to other pages.

The login page is very similar, with the only differences coming from the fact that it's a different project (i.e. different layout etc etc).

Both applications are database applications, basically created to add, update and report on data.

I'm thinking that it must be a bug :/

How could you have session variables on the page, then post back ANYTHING and they disappear? I have buttons on the screen with NO code behind. I swear the only three lines I have in my code are the If Then conditional...

Still lost...
We need a simple repro.
I simply would not use Session variables to hold state. They are a problem and their values predicatably disappear due to many issues:
1. User does not allow cookies,
2. Server starts a new thread.
3. Server farm serves the next request in another server,
4. etc. etc.

1. Use hidden variables to pass this information in the form and/or querystring.

or
2. I am not sure that the Context object can be used to pass information, but it seems to work in all cases that I have tried.

Hope this clarifies this issue.
You cannot use hidden variables/querystring variables for secure information. It's just not practical. There are ways around those issues.