473,320 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Session Question (SQL Server)

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?
Sep 26 '07 #1
2 1106
Mick Walker wrote:
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.
Sep 26 '07 #2
Mick Walker wrote:
>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
Sep 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nhi Lam | last post by:
Hi, I understand that there are 3 modes in which I can configure the SessionStateModule. What I need is an out of process Session State store with fail over support. The "SQL Server Mode" seems...
5
by: Abhilash.k.m | last post by:
This is regarding the session management using Out of proc session management(SQL SERVER). Among the samples below which one is better to set the session? 1. There are 20 session...
1
by: quique | last post by:
Hi all, I want to know the advantages and disadvantages of the methods of maintain session state, obviously if i choose sqlserver is the most robust but the less performace.. If wan't to...
9
by: Greg Linwood | last post by:
I'm having difficulty understanding Session state in ASP.Net. It's almost embarrassing asking this as I've been using ASP since it was first released & it really shouldn't be this hard to use -...
4
by: John Q. Smith | last post by:
I'm trying to find out some of the details behind OOP state management with SQL Server. For instance - how long does the session object live on any server? Is it created and destoyed with each...
2
by: John A Grandy | last post by:
for high traffic public websites , what are the proven options for session-state storage & management ? is an out-of-process state-server generally preferred over a sql-server ? what are the...
9
by: Patrick | last post by:
Hello I'm running two Webservers Using ASP.NET. both are running the same ASP.NET Application, with <sessionState mode="SQLServer" stateConnectionString="tcpip=127.0.0.1:42424"...
5
by: Tim W. | last post by:
Folks. In a B2B Procurement system we've created, we got following Session-Issue: Configuration: We are using IIS 6.0 and added SQL-Based-Sessions in web.config with a timeout of 240 minutes...
6
by: DNB | last post by:
I would like to know what you guys think is the best way to access data: Asp.Net session vs. Database Queries. In our application we are using asp.net tree view to display hierarchical data and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.