Solved

Using ViewState in c# code

Posted on 2008-06-10
7
5,749 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
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 250 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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 new code templates 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