Session Question (SQL Server)

M

Mick Walker

I have the following simple class:

using System;

[Serializable]
public class Person
{
public Person()
{

}
/// <summary>
/// Creates a new person object with pre assigned values
/// </summary>
/// <param name="Fname">Persons Firstname</param>
/// <param name="Sname">Persons Surname</param>
/// <param name="Age">Persons Age</param>
/// <param name="Postcode">Persons PostCode</param>
public Person(string Fname, string Sname, int Age, string Postcode)
{
strFName = Fname;
strSName = Sname;
intAge = Age;
strPostcode = Postcode;
}
# region "Properties"
private string _strFname;
private string _strSname;
private int _intAge;
private string _strPostcode;
public string strFName
{
get {return _strFname;}
set {_strFname = value; }
}

public string strSName
{
get {return _strSname;}
set {_strSname = value;}
}

public int intAge
{
get {return _intAge; }
set {_intAge = value;}
}

public string strPostcode
{
get { return _strPostcode;}
set {_strPostcode = value; }
}
#endregion
}

And I use this class as follows:

-- Default.aspx.cs
using System;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person myPerson = new Person("Mick", "Walker", 27, "xxxxR");
Session["SomePerson"] = myPerson;
Response.Redirect("Default2.aspx");
}
}

And in Default2.aspx
using System;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person myPerson = new Person();
myPerson = (Person)Session["SomePerson"];
Response.Write(myPerson.strFName + "<br/>");
Response.Write(myPerson.strSName + "<br/>");
Response.Write(myPerson.intAge.ToString() + "<br/>");
Response.Write(myPerson.strPostcode + "<br/>");

}
}

This gives me the Error:

Error 1 Cannot implicitly convert type 'object' to 'Person'. An explicit
conversion exists (are you missing a cast?)
C:\Projects\StateTest\Default2.aspx.cs 17 20 C:\Projects\StateTest\

I am using SQL Server as my session provider if that makes a difference
(I did however mark the Person Object as Serializable).
Does anyone know what I am doing wrong?
 
M

Mick Walker

Mick said:
I have the following simple class:

using System;

[Serializable]
public class Person
{
public Person()
{

}
/// <summary>
/// Creates a new person object with pre assigned values
/// </summary>
/// <param name="Fname">Persons Firstname</param>
/// <param name="Sname">Persons Surname</param>
/// <param name="Age">Persons Age</param>
/// <param name="Postcode">Persons PostCode</param>
public Person(string Fname, string Sname, int Age, string Postcode)
{
strFName = Fname;
strSName = Sname;
intAge = Age;
strPostcode = Postcode;
}
# region "Properties"
private string _strFname;
private string _strSname;
private int _intAge;
private string _strPostcode;
public string strFName
{
get {return _strFname;}
set {_strFname = value; }
}

public string strSName
{
get {return _strSname;}
set {_strSname = value;}
}

public int intAge
{
get {return _intAge; }
set {_intAge = value;}
}

public string strPostcode
{
get { return _strPostcode;}
set {_strPostcode = value; }
}
#endregion
}

And I use this class as follows:

-- Default.aspx.cs
using System;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person myPerson = new Person("Mick", "Walker", 27, "xxxxR");
Session["SomePerson"] = myPerson;
Response.Redirect("Default2.aspx");
}
}

And in Default2.aspx
using System;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person myPerson = new Person();
myPerson = (Person)Session["SomePerson"];
Response.Write(myPerson.strFName + "<br/>");
Response.Write(myPerson.strSName + "<br/>");
Response.Write(myPerson.intAge.ToString() + "<br/>");
Response.Write(myPerson.strPostcode + "<br/>");

}
}

This gives me the Error:

Error 1 Cannot implicitly convert type 'object' to 'Person'. An
explicit conversion exists (are you missing a cast?)
C:\Projects\StateTest\Default2.aspx.cs 17 20
C:\Projects\StateTest\

I am using SQL Server as my session provider if that makes a difference
(I did however mark the Person Object as Serializable).
Does anyone know what I am doing wrong?
My bad it actually works fine. Before I cast the session item I
requested I was getting the error. Which as VS does, still showed after
I made the modifications.
 
H

Hans Kesting

Mick said:
Person myPerson = new Person();
myPerson = (Person)Session["SomePerson"];


As a note: first you create a new Person, then you overwrite it with
the value from the Session. You don't need that first "new Person()".

Either:
Person myPerson;
myPerson = (Person)Session["SomePerson"];

or even shorter:
Person myPerson = (Person)Session["SomePerson"];

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top