.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

static variables throughout application

Posted By:      Posted Date: October 06, 2010    Points: 0   Category :ASP.Net

hello frnds

i am developing a website in asp.net

i have created a class "Utility" in App_Code Folder

this class contains a static object of another class(LoginInfo) as described below:

public class Utility
public static LoginInfo CurrentUser = new LoginInfo();


This LoginInfo Class is my business object which only contains 2 properties UserID and UserName

Now what i am doing on my login page, after user authentication i am setting these properties with current logged in UserId and UserName

protected void Login_Click(object sender, EventArgs e)

        if (Authenticated User)
        Utility.CurrentUser.UserID = "001"; //ID of current logged in user
        Utility.CurrentUser.UserName = "Admin" //Username of current logged in user;


I am using this CurrentUser object on my webpage to get userid and username.

It works fine, but after few minutes my CurrentUser.UserID and CurrentUser.UserName is set to blank string. 

I am not able to figure out what is the problem. may be due to asp.net recycles.

Please help me how should i solve this problem? Is there any other way?


View Complete Post

More Related Resource Links

Session and Application Variables


I have a web application in which i need to logout the user if he clicks the logout button or even if he closes his browser window. On Logout button click, i am doing session.Abandon(), but how to go about his browser window close.

Can anybody help with a code sample.

Sharing of Non-Static member variables of a class using .NET Remoting

This kind of weird. I am trying to host an object of a class X (a class library class). This is a SAO and I have set the WellKnownMode as Singleton in the server configuration. <wellknown mode="Singleton" When multiple clients fetch the instance of the class X they can share the static member variables. Any changes made to the static variables are visible to each other. The problem is with the non-static member variables of the class. Each instance is having a local copy of the non-static variables. Why should that happen in case of a Singleton mode? Am I missing something for which this is happening? Any help will be appreciated.  A for apple, B for boy, C for c++

Default Application "does not contain a static 'Main' method suitable for an entry point"

Hi, When I create a brand new WPF application project in Visual Studio 2010 and immediately try to run it, I get the error WpfApplication1.exe does not contain a static 'Main' method suitable for an entry point. App.xaml's build action is set to ApplicationDefinition. I think it's not generating App.g.cs. Does anyone know what's wrong?

Behavior of static classes/variables

Hi, I have a static class, that contains some static DataTable-variables.. In some of them I fill the tables from my database and one of them even does some file-checking..I'm concerned about performance in these variables.. How to they behave? Is it called once in the applications lifetime or everytime I call for the variable?What I mean is, if I have a loop with 500 runs and I use one of the static variables in the loop, is the code that fills the datatable executes 500 times or just once?

static variables on asp.net pages


Hello guys. Believe it or now, i have just found that that static objects in classes (pages) hold the same value for that same page even with two different users logged in.

I have a page where the user fills the name and id of a customer (page a) for example and then puts it inside the user session, redirects to page b and on that page writes a report based on that customer and saves it to the database.

We are getting reports that when the first user saves the report, the values of the second user gets saved.

My question is, how to handle the user's data without using static objects inside classes? The click method of button just passes the sender and event arguments, so i cannot pass for example a DataTable through it, and i only use session variables to store information that has already being validated just before sending the user to the next page.

That's why i used static objects, so methods like click inside of buttons could access the data.

Is the only way to do this is to store objects (like a DataTable in my example) in the user's session and then retrieve, update and put it back in the session each time i need to use it? I always though the user's session should be as small as possible.

Static Variables in session


I am creating a static variable and adding to session.This variable has value that is user specific. Now I am getting a problem with this users are complaining that they are getting values that should be another users. It's like a mix up with session. Could this be because of static variable declaration?

Application variable or static class for global string?


I need to keep a global variable throughout the whole time the user is at my site, but do I use Application or a static class? The string variable should store a region name and my site makes a few changes depending on which region that has been set.

I read that Application was mainly for classic ASP, but I also read that a static class cannot be instantiated at runtime (e.g. when the user has logged in).

So, have I got something wrong here and which solution do I use?



Microsoft's Collaborative Application Markup Language (CAML) Loop

discovered an interesting error recently while working with Microsoft's Collaborative Application Markup Language (CAML) that, surprisingly, had received no ink. Partly what surprises me about this is that the error may require you to rewrite large sections of your code if you haven't previously considered this SharePoint limitation. I'll start with some context, but first of all the error is:

Integrating a Legacy Web Application in SharePoint

This month we will continue with the theme of deploying Microsoft's SharePoint as a company intranet by describing a method for integrating a legacy Web application inside a SharePoint site.

In many companies the intranet site is not just a home page with announcements. It houses Web applications used by many different departments. In last month's article, I built a page to search the employee table in Northwind (the sample database included in Microsoft's SQL Server). This month, I will move the Web pages used to add/remove records from the table into the SharePoint intranet.

MVC architecture in ASP.Net using C# and Microsoft Data Access Application block

The Model-View-Controller (MVC) pattern separates the modeling of the domain, the presentation, and the actions based on user input into three separate classes [Burbeck92]:

Model. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).

View. The view manages the display of information.

Controller. The controller interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.

High-Performance .NET Application Development & Architecture

It has always been a goal of project architects to plan an effective strategy from the ground up in regards to an new application. All relevant factors are taken into consideration with respect to the application, from its design and layout to a functional website infrastructure. Pre-.NET strategies and design guidelines still effective now were developed with Microsoft's DNA (Distributed interNet Application) platform. This model successfully served the purpose of architecting N(any number of)-Tier (levels) applications. In its basic sense, as in most robust, distributed applications, you'll architect 3 main layers or Tiers: presentation, business rules and data access.

Application architecture in asp .net

Application Architecture is the most important process of developing a good application. Some people tends to jump into coding without any architecture laid. Later we see those people changing the architecture and finally the application had to be developed from the scratch with the correct architecture.

In this article I will describe some details about how you can improve the architecture of you application. This article is targeted to the beginner's audience.

Improving Application Performance in .Net

Application performance has always been a concern for Web Application developers. This article contains guidelines and tips for maximizing application performance in ASP.NET.

Static Class for Beginners

In general, the members of a class can be accessed by instance of that class when they are in public, but there comes a situation to access or call members of a class without an instance and this can be achieved by declaring them as static

Logic behind N -Tier Application Development

In the arena of application development, developers all over the world use various programming languages, technical processes, and technical tools. While using number of technical & non technical elements the processes or methods become complex, for simplification of the application development process, developers use various techniques such as dividing the application architecture into layers or parts. In the professional & technical language this separation or division process is called N-Tier Application Development process, where "N" stands for number and "tire" stands for layers or parts.

How to create 3 tier application using LINQ

As you know that in 3 tier architecture there are three layers

User interface layer. (Is our Form in Windows application and .aspx page in Web application)
Data Access layer. (Which provides interface between Business logic layer and Database)
Business Logic layer.(Which stores your application logic)

Using Lightbox in an ASP.NET Application

The article describes Lightbox as, "... a simple, unobtrusive script used to overlay images on the current page." It delivers a nice, professional looking method for displaying images as overlays through the use of hyperlinks.
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend