I am trying to retrieve SessionItemShory column for given sessionid.The data stored is serialized so we need to deserialize the data to use it.I am trying to do that but its not working.I have attached the code i used please let me know what is going wrong.I think that the casting is going wrong soomewhere.Thanks in advance.Please help.
[Serializable]
public class sessiondata
{
public string username { get; set; }
public string country { get; set; }
public string city { get; set; }
public sessiondata(string u,string c,string cty)
{
username = u;
country = c;
city = cty;
}
}
object objbyte;
//string sessioninfo=string.Empty;
SqlConnection connection=new SqlConnection("server=(localdb)\\userdata;database=ASPState;");
connection.Open();
SqlCommand command = new SqlCommand("Select SessionItemShort from dbo.ASPStateTempSessions where SessionID='" + Label9.Text + "'", connection);
SqlDataReader drd = command.ExecuteReader();
if(drd.HasRows)
{
while(drd.Read())
{
objbyte=drd["SessionItemShort"];
byte[] bytData = (byte[])objbyte;
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytData);
var liz2=new List<sessiondata>();
using (Stream stream =ms )
{
BinaryFormatter bin = new BinaryFormatter();
liz2 = (List<sessiondata>)bin.Deserialize(stream);
foreach (sessiondata sdata in liz2)
{
Console.WriteLine("{0}, {1}, {2}",
sdata.username,
sdata.country,
sdata.city);
}
}