?
Solved

Using ViewState in c# code

Posted on 2008-06-10
7
Medium Priority
?
5,772 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
7 Comments
 
LVL 7

Expert Comment

by:Chandan_Gowda
ID: 21750062
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
ID: 21750071
//it is not Sessions

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

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

Author Comment

by:lakshmidurga
ID: 21750203
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
ID: 21750318
please post u r code
0
 
LVL 2

Accepted Solution

by:
greigtech earned 1000 total points
ID: 21750622
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

771 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