Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The session state Information is invalid and might be corrupted in ASP.Net

I am using ASP.Net 3.5 with C#,Development ID:Visual Studio 2008. When I am using

Session["FileName1"] = "text1.txt" 

it is working fine, but then I am using

number1=17;
string FileName1="FileName1" + number1.toString(); 

then setting with

Session[FileName1]="text1.txt";

gives me runtime error

The session state information is invalid and might be corrupted at System.Web.SessionState.SessionStateItemCollection.Deserializer(BinaryReader reader)

Can anybody solve my problem, when I am using string in the Session variable? Remember it works on my development machine (meaning local Visual Studio) but when deployed to the server it gives mentioned error.

enter image description here

like image 461
Vaibhav Parmar Avatar asked Nov 28 '13 12:11

Vaibhav Parmar


2 Answers

Make sure the FileName1 variable is not null before trying to access it via the Session[FileName1] syntax...

Here's a link to someone else that was having the same problem: http://forums.asp.net/t/1069600.aspx

Here's his answer:

In the code, I found the following line:

//some code
Session.Add(sessionVarName, sessionVarValue);
//some other code

Apparently, because of some dirty data, there is a time when sessionVarName is null.

Session.Add will not throw any exception in this case, and if your Session Mode is "InProc", there will be no problem. However, if your Session Mode is "SQLServer", during deserialization of the session store, you will got the exception that I got. So, to filter out dirty data, I modified the code to become:

if (sessionVarName != null)
{
  //somecode
  Session.Add(sessionVarName, sessionVarValue);
  //some other code
}  
like image 197
Nathan Prather Avatar answered Oct 17 '22 01:10

Nathan Prather


the reason of your error is

xyz = new Guid() is also xyz= Guid.Empty;

so when you try to convert to string it's throw error.

just modify you code something like that.

Guid guId = System.Guid.NewGuid(); 
string x = guId .ToString();
string FileName1="text1.txt" + x;
Session[FileName1]="text1.txt";
like image 42
Mohammad tanvirul islam Avatar answered Oct 17 '22 02:10

Mohammad tanvirul islam