Solved

Using ViewState in c# code

Posted on 2008-06-10
7
5,741 Views
Last Modified: 2013-12-17
Hi,

I want to access a variable through out my project.So,i am using a viewstate .
I am assigning value  for a viewstate variable  in this way
viewstate["username"]="xxx" in one page ,
i am not able to acess it it another page in the same project.
Is there anything i need to do to access it through out the project.
0
Comment
Question by:lakshmidurga
  • 3
7 Comments
 
LVL 7

Expert Comment

by:Chandan_Gowda
Comment Utility
Viewstates are accesiable only on the page that u have declared It, Viewstates cannot be accessed across pages.Use Sessions to achieve u r functionality
//First Page
Sessions["username"]="xxx"

//Second Page
string strtest=Sessions["username"].ToString();
0
 
LVL 7

Expert Comment

by:Chandan_Gowda
Comment Utility
//it is not Sessions

//First Page
Session["username"]="xxx"

//Second Page
string strtest=Session["username"].ToString();
0
 
LVL 7

Author Comment

by:lakshmidurga
Comment Utility
Hi
I have tried session.But,i am facing problems with session variables,when deployed in IIS.I am getting object reference not set to an object error at the statement
Session["xx"]="xxx"
if i comment out that statement it is functioning well.

This statement even working fine when i run my application using visual studio 2005.
Please help me in any other necessary code i need to code when using sessions in c#.
I have used session variables many times with vb.net code.They went well.This is the first time i am writing c# code.
0
 
LVL 7

Expert Comment

by:Chandan_Gowda
Comment Utility
please post u r code
0
 
LVL 2

Accepted Solution

by:
greigtech earned 250 total points
Comment Utility
There are many ways you can access variables throughout the application:

Session will live for the life of the users session:
Set it:
HttpContext.Current.Session.Add("VarName", "varValue");
Access it:
String x = (string)Session["VarName"];

** Or you can Cache it** which is great if you want to set dependencies and expiration.
HttpContext.Current.Cache.Add("VarName", "VarValue", null,null,null,null,null);
Access it by:
 String x = (string)Context.Cache["VarName"];

**or if you just want it alive during the duration of the call:
HttpContext.current.Items.Add("VarName", VarValue);
Access it by:
string s = (string)HttpContext.Current.Items["VarName"]

Hope it helps!



You can cache it

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now