Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
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

C#
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
Updated 7-Feb-13 23:59pm
v3

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,
C#
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...
C#
int UserId = Session["UserId"] ;

Happy Coding!
:)
 
Share this answer
 
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
 
Share this answer
 
v3
Comments
vikrant vaibhav 8-Feb-13 0:25am    
i need namespace dude
RDBurmon 8-Feb-13 0:28am    
Post your code
vikrant vaibhav 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.
[no name] 8-Feb-13 1:00am    
You need to set the session value at some where before rendering to this page. You might be missing that.
RDBurmon 8-Feb-13 0:35am    
Post your error which dot net gave you
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:

C#
ViewBarg.user = "someone";


And in the razorview page access the item like this:

C#
@ViewBag.user
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900