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">Pe rsons Firstname</param>
/// <param name="Sname">Pe rsons Surname</param>
/// <param name="Age">Pers ons 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.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
Person myPerson = new Person("Mick", "Walker", 27, "xxxxR");
Session["SomePerson "] = myPerson;
Response.Redire ct("Default2.as px");
}
}
And in Default2.aspx
using System;
public partial class Default2 : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
Person myPerson = new Person();
myPerson = (Person)Session["SomePerson "];
Response.Write( myPerson.strFNa me + "<br/>");
Response.Write( myPerson.strSNa me + "<br/>");
Response.Write( myPerson.intAge .ToString() + "<br/>");
Response.Write( myPerson.strPos tcode + "<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\Sta teTest\Default2 .aspx.cs 17 20 C:\Projects\Sta teTest\
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? 2 1117
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">Pe rsons Firstname</param>
/// <param name="Sname">Pe rsons Surname</param>
/// <param name="Age">Pers ons 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.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
Person myPerson = new Person("Mick", "Walker", 27, "xxxxR");
Session["SomePerson "] = myPerson;
Response.Redire ct("Default2.as px");
}
}
And in Default2.aspx
using System;
public partial class Default2 : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
Person myPerson = new Person();
myPerson = (Person)Session["SomePerson "];
Response.Write( myPerson.strFNa me + "<br/>");
Response.Write( myPerson.strSNa me + "<br/>");
Response.Write( myPerson.intAge .ToString() + "<br/>");
Response.Write( myPerson.strPos tcode + "<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\Sta teTest\Default2 .aspx.cs 17 20
C:\Projects\Sta teTest\
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 to be it, but I heard there is
quite a bit of degradation in performance for using this
mode. My next option is the "State Server Mode". However,
this mode does not give me the fail over support. Is there
anything that I can do the enhance the...
|
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 variables and all of them
are being stored into session and accessed from session
and individual session object. Example: Session
= "XYZ", Session=100, Session="NAME", etc.
|
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 maintain session state in sqlserver, Does
anybody knows when it's saved to database?, on
beguinSession, on endSession...
<sessionState
|
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 - perhaps I'm just not very smart
or perhaps MS is making this too hard for us sql bunnies to understand - I
dunno, but I'd really appreciate someone explaining what I'm doing wrong
here & perhaps suggest a better approach..
I'm familiar with use of...
|
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 page request?
- will each reading of a session variable cause a round
trip to the DB server? or does the complete session live
within the HttpContext object?
- when asp.net session state is enabled (in any mode),
after a session has been created,...
| |
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 relevant criteria ? is the primary criteria max expected total
storage size (for all active sessions) versus max ram available on the
state-server machine ?
if ADO.NET objects (such as small DataTables) must be stored in
session-state , is any...
|
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"
sqlConnectionString="data source=myserver;user id=myuser;password=mypw"
cookieless="false" timeout="20" />
This seems to work. But we have cases, when users report that they loose the
|
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 and set them to cookieless mode.
As well we added Session.Timeout=240 in asp.net-application and saved data
into the session.
Session-Timeout in IIS is also set to 240.
|
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 when user clicks on particular node it brings up totally different
page with all the asp.net controls dynamically generated.
Example:
Tree view Control is as follow:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |