Clearing Session Variables

A

Amy

I'm having a problem with clearing my session variables. I'm using
session variables to pass data from a web form to a pdf generator.
The problem comes in when a user goes back to the original page and
takes data out of the field - the session variable is persisting.

i tried the following, and it didn't work - I'm not sure where to go
from here. Any advice would be appreciated. The string being passed
is both the variable name and the name of the session variable.


public bool CheckIfEmpty (string test)
{
if (test == "")
{

Session.Remove(test);
return true;
}
else
{
return false;
}

}
 
N

Nicholas Paldino [.NET/C# MVP]

Amy,

You are only removing a variable if the string is empty. I would think
you want to move the call to Remove into the "false" part of the check.

Hope this helps.
 
A

Amy

I think I only *want* to remove the variable if the string is empty.
If I remove it
and it's not empty, then, the data I want to keep isn't getting
passed, right? Here's how the method is being used:

if (CheckIfEmpty(locAdd2) == false)
{
Session.Add("locAdd2", locAdd2);
}

so, the first time the user fills out the form, if they filled out
that form field, it gets added as a Session variable. If they come
back to the form and change the data, the data changes. But if they
come back and clear all the information out, the original data
persists.

(the method again, for reference):

public bool CheckIfEmpty (string test)
{
if (test == "")
{

Session.Remove(test);
return true;
}
else
{
return false;
}
 
A

Amy

I never did get the Session.Remove to work in this method. The
solution I found is to use Session.Clear on the second page, after I
was done with all the Session variables. So when I go back to the
first page, I'm starting with a clean slate.

If anyone can tell me why the Session.Remove in the method below
didn't work, I'd love to know.

--Amy Jones
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top