asp.netsessiondirectory.delete

session variable object gets deleted on postback - ASP.NET


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.


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.