Solved

Using ViewState in c# code

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FInd Image Control Gridview 3 38
Open a link in vb.net 2 27
PowerShell:  Column widths won't expand 3 50
reading excel file in .net 2 33
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

730 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