Reputation: 151
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.
Upvotes: 3
Views: 14039
Reputation: 47726
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.
Upvotes: 2
Reputation: 2199
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.
Upvotes: 1
Reputation: 1911
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>
Upvotes: 1
Reputation: 8759
First, a couple of sanity checks:
Here's what I would try to debug this issue, after checking the above:
Thanks
Upvotes: 0
Reputation: 19365
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.
Upvotes: 0
Reputation: 3802
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.
Upvotes: 0