Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET session
Dear all
 
I need to use session variables in my program but I can not find the Session class.
I need to know in which namespace of the .Net framework the Session class can be found.
 
Thank you guys in advance
 
protected void Read_Emails(object sender, EventArgs e)
        {   
            Pop3Client pop3Client;
            if (  
                Session["Pop3Client"] == null)
            {
                pop3Client = new Pop3Client();
                pop3Client.Connect(txtMailServer.Text, int.Parse(txtPort.Text), chkSSL.Checked);
                pop3Client.Authenticate(txtUserName.Text, txtPassword.Text);
                Session["Pop3Client"] = pop3Client;
            }
            else
            {
                pop3Client = (Pop3Client)Session["Pop3Client"];
            }
            int count = pop3Client.GetMessageCount();
            DataTable dtMessages = new DataTable();
            dtMessages.Columns.Add("MessageNumber");
            dtMessages.Columns.Add("From");
            dtMessages.Columns.Add("Subject");
            dtMessages.Columns.Add("DateSent");
            int counter = 0;
            for (int i = count; i >= 1; i--)
            {
                Message message = pop3Client.GetMessage(i);
                dtMessages.Rows.Add();
                dtMessages.Rows[dtMessages.Rows.Count - 1]["MessageNumber"] = i;
                dtMessages.Rows[dtMessages.Rows.Count - 1]["Subject"] = message.Headers.Subject;
                dtMessages.Rows[dtMessages.Rows.Count - 1]["DateSent"] = message.Headers.DateSent;
                counter++;
                if (counter > 5)
                {
                    break;
                }
            }
            gvEmails.DataSource = dtMessages;
            gvEmails.DataBind();
        }
 
Here Session is giving error as not present in current context.
Posted 7-Feb-13 19:09pm
Edited 8-Feb-13 0:59am
digimanus30.8K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

The Session property provides programmatic access to the properties and methods of the System.Web.SessionState.HttpSessionState class.
 
Because ASP.NET pages contain a default reference to the System.Web namespace (which contains the HttpContext class),
 
you can reference the members of HttpContext on an .aspx page without using the fully qualified class reference to HttpContext.
 
For example, you can use Session["SessionVariable1"] to get or set the value of the session state variable
 
so, you just use session as below,...
 
// to set session variable write statement as below,
Session["UserId"] = 1;
 
Here, "UserId" is name of your session variable which is set to value 1 in above line. no declaration like integer,string etc...
 
To fetch value of session variable...
int UserId = Session["UserId"] ;
Happy Coding!
Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Read this blog
 
http://techpint.com/programming/how-create-session-variable-aspnet[^]
 
Or else add your code where you are facing this issue
 

Accept and vote if helps otherwise revert back with queries
--RDB
  Permalink  
v3
Comments
vikrant vaibhav at 8-Feb-13 0:25am
   
i need namespace dude
Rahul Dhoble at 8-Feb-13 0:28am
   
Post your code
vikrant vaibhav at 8-Feb-13 0:33am
   
protected void Read_Emails(object sender, EventArgs e)
{
Pop3Client pop3Client;
if (
Session["Pop3Client"] == null)
{
pop3Client = new Pop3Client();
pop3Client.Connect(txtMailServer.Text, int.Parse(txtPort.Text), chkSSL.Checked);
pop3Client.Authenticate(txtUserName.Text, txtPassword.Text);
Session["Pop3Client"] = pop3Client;
}
else
{
pop3Client = (Pop3Client)Session["Pop3Client"];
}
int count = pop3Client.GetMessageCount();
DataTable dtMessages = new DataTable();
dtMessages.Columns.Add("MessageNumber");
dtMessages.Columns.Add("From");
dtMessages.Columns.Add("Subject");
dtMessages.Columns.Add("DateSent");
int counter = 0;
for (int i = count; i >= 1; i--)
{
Message message = pop3Client.GetMessage(i);
dtMessages.Rows.Add();
dtMessages.Rows[dtMessages.Rows.Count - 1]["MessageNumber"] = i;
dtMessages.Rows[dtMessages.Rows.Count - 1]["Subject"] = message.Headers.Subject;
dtMessages.Rows[dtMessages.Rows.Count - 1]["DateSent"] = message.Headers.DateSent;
counter++;
if (counter > 5)
{
break;
}
}
gvEmails.DataSource = dtMessages;
gvEmails.DataBind();
}
 
Here Session is giving error as not present in current context.
Rahul Dhoble at 8-Feb-13 0:35am
   
Post your error which dot net gave you
vikrant vaibhav at 8-Feb-13 1:49am
   
session not present in current context
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

The above suggestions are great, but also tr out the dynamic object ViewBag. In your controller declare anything an array, primitive type, string type etc like this:
 
ViewBarg.user = "someone";
 
And in the razorview page access the item like this:
 
@ViewBag.user
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 DamithSL 165
4 Tomas Takac 144
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100