Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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>

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.

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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top