views:

522

answers:

6

I have made something like the following code:

protected void Page_Load(object sender, EventArgs e)
{
   Label1.Text = Session["loginid"].ToString();
}

protected void delete_click(object sender, EventArgs e)
{
    delete("mail1",Session["loginid"]);
}

private int delete(string mailid, string user)
{
 System.IO.Directory.Delete(Server.MapPath(@"~\files\" + user + @"\" + mailid), true);
}

When i press the delete button, everything works fine and the folder gets deleted. but after that when page postbacks again then a NullRefrenceException is raised at Label1.Text = Session["loginid"].ToString();

why is it happening...??

When I am not using this Directory.Delete() method everything is working fine and session variables are not set to null.

When I traced my application I found that After Directory.Delete() method Session variables were intact and I was able to use those session variables in the processing after Directory.Delete().

But as soon as the page postbacks all session variables are set to null. And this problem doesn't appear when i m not using this delete() method.

The folder I m deleting is in my project's folder. I m running this website using Visual Studio.

Please help.

A: 

Since the page loads correctly before you press the delete button, the problem is presumably with the Session["loginid"].ToString() reference. Do you have any other code that references Session["loginid"]? The code you have shown here won't do anything that removes loginid from the Session.

However, if this application is running on a server cluster and you're using the default session mode of in-process, you may be losing access to your session between HTTP requests because they're handled by different servers. See here for more information.

ElectricDialect
If I remove the directory.delete() function from the code then the whole application is running so fine without any exception.
Akshay
A: 

If I remove the directory.delete() function from the code then the whole application is running so fine without any exception

Ok, seems that we found your problem. Your project does not have the necessary privileges to delete the direcotry (even if the directory is deleted.nevertheless: there are privilege problems)

I guess that you're application is throwing an exception while performing this file operation and a new session begins. I have a similiar situation on one of my projects, but I still haven't figured out how to solve it.

I'm pretty sure you will concur with description if you create the Global.asax and set breakpoints on Application_OnError and Session_OnStart (or however these methods are spelled correctly). You will see that the an error is raised and afterwards a new Session is started.

citronas
I think you are right.Because when I traced my appliation then i found that when page is postbacked after using the directory.delete() method then all the session variables are set to null.
Akshay
If there was an exception, though, wouldn't he see the Yellow Screen of Death on the postback that executed the Delete method? Why would the page come back all fine and dandy only to have it fail due to an expired session on the NEXT postback?
Scott Mitchell
No you would not and thats a really strange behaviour.
citronas
I traced my application again.I found that after directory.delete() method session variables were intact ie in all the processing after Directory.Delete() i was able to use those variables.But as soon as the page postbacks all session variables are null.Its so strange.Let me provide some more info:the folder I am deleting is in my project folder.I have not yet hosted this website on iis, I am running it using Visual Studio.Please help.
Akshay
@citronas: an exception shouldn't affect session variables. @AKshay: if you still have the problem, then why did you accept an answer?
John Saunders
@John Saunders:Yes, I know. But if you do not believe me, try to build sample project based on my descriptions above.
citronas
@citronas: I've been working with ASP.NET for nine years. I know very well that an exception does not cause session variables to go away. However, an AppDomain restart _does_, and deleting files within the web site may cause an AppDomain restart.
John Saunders
Thank you Mr. Saunders.I got the problem and now i m placing the files which are to be deleted outside my website.
Akshay
+1  A: 

Is your 'files' folder in your web application folder? Maybe application restarting itself when you deleting the files. Try to use sessionStateServer. Its keep sessions alive.

Web.config:

<configuration>
    <system.web>
        <sessionState mode="StateServer"></sessionState>
    </system.web>
</configuration>
cem
While this very well get session back on track, it is treating the symptom and not the problem.
Scott Mitchell
@Scott: I upvoted it because of the insight about AppDomain restarts being caused by the delete call.
John Saunders
can you please explain this <sessionState /> a bit more.How to use it? Where to use it? What exactly is does?
Akshay
You'll write in web.config file. StateServer's a windows-service, Its seperating your sessions from your application. And i editing this post or you may look msdn page; http://msdn.microsoft.com/en-us/library/ms972429.aspx
cem
A: 

First, a couple of sanity checks:

  1. Does session work as expected on other pages?
  2. Is your Delete method deleting files in a special ASP.NET folder, like App_Data or App_Code, which may be causing the application to restart?

Here's what I would try to debug this issue, after checking the above:

  1. Set a breakpoint on the delete method and have the session variable in a watch window. See what the value of the session variable is before the call to Directory.Delete is and what it is afterward. Is it at that point when you're losing session, or is not until the next page visit?
  2. Use a tool like Fiddler to examine the cookies exchanged between the browser and web server on postbacks. Check that when the browser first visits a new session cookie is created and stored on the browser. Then, when deleting the folder, see if the web server is sending a new session cookie on the response of that postback. This would indicate that a new session has been created.

Thanks

Scott Mitchell
A: 

Deleting a folder in your virtual directory may cause your application to re-start, thus loosing all session data. To prevent this, either delete individual files (not folders) or use the StateServer to maintain your sessions.

J.Hendrix
A: 

Just another guess here but maybe it's because your modifying something in your applications directory (a hunch since your using Server.MapPath with the ~). IIS maybe thinks the app has changed and recycles the application and as a result wipes out all sessions.

This is similar to if you modify your web.config file while someone is using the app and it drops all sessions and recycles the app. Are you deleting a directory that may contain information that IIS is using for the application?

You said it only happens when you include that line of code and a session will really only get wiped out consistently (unless you are doing it yourself manually) when the application is recycled by IIS or times out. It is obviously not timing out so the recycle must be what is happening.

Kelsey