global variables for concurrent access

Last post 06-16-2009 10:14 PM by Bober Song - MSFT. 9 replies.

Sort Posts:

  • global variables for concurrent access

    06-11-2009, 9:17 AM
    • Member
      2 point Member
    • nmufti
    • Member since 01-27-2009, 11:50 AM
    • Posts 26

    Hi,

    What is the best method to define gloabl variable in asp.net application that should be global for single instance of applicantion and different for each instance of application for each user connected.

  • Re: global variables for concurrent access

    06-11-2009, 9:57 AM
    Answer

    Application class: unique across entire application.

    Session class: unique for each user.

  • Re: global variables for concurrent access

    06-11-2009, 10:07 AM
    • Member
      2 point Member
    • nmufti
    • Member since 01-27-2009, 11:50 AM
    • Posts 26

    Sorry, don't understand!

    Can you please give some example of how to declare these variables...

  • Re: global variables for concurrent access

    06-11-2009, 10:21 AM
    • Member
      340 point Member
    • cyad
    • Member since 02-20-2003, 10:13 AM
    • Posts 85

    In VB.NET - Application("VariableName") = "Some Value"

    In C# - Application["VariableName"] = "Some Value";

    Keep in mind the web.config file is typically used for application level variables as well, like in the appsettings section, the mail settings, etc.

  • Re: global variables for concurrent access

    06-11-2009, 10:44 AM
    • Member
      2 point Member
    • nmufti
    • Member since 01-27-2009, 11:50 AM
    • Posts 26

     I have declared some variables in my web application like this:

    Public SelectedProductID As Integer

    the problem is that when one user loads the website and select a product, the other user gets the same SelectedProductID as the user 1. How can i define SelectedProductID variable so that it is diffirent for each web user?

    Thanks.

  • Re: global variables for concurrent access

    06-11-2009, 10:46 AM
    • Contributor
      2,397 point Contributor
    • maverickhyd
    • Member since 03-25-2009, 6:38 AM
    • Posts 416

    nmufti:

    Hi,

    What is the best method to define gloabl variable in asp.net application that should be global for single instance of applicantion and different for each instance of application for each user connected.

     

    For Your requirement session state is suitable

    signle instance of application

    different for each user

    http://msdn.microsoft.com/en-us/library/ms178581.aspx

    Please Mark as Answer if it helped You!
  • Re: global variables for concurrent access

    06-11-2009, 11:06 AM
    • Member
      340 point Member
    • cyad
    • Member since 02-20-2003, 10:13 AM
    • Posts 85

    nmufti:

     I have declared some variables in my web application like this:

    Public SelectedProductID As Integer

    the problem is that when one user loads the website and select a product, the other user gets the same SelectedProductID as the user 1. How can i define SelectedProductID variable so that it is diffirent for each web user?

    Thanks.

    From the sounds of things, you may need to post some source code so we can see what you're talking about more clearly. But in this case, you may not need either application or session variables. For performance reasons, you should avoid session variables wherever possible.

  • Re: global variables for concurrent access

    06-16-2009, 5:39 AM
    Answer

    Hi nmufti,

    As far as I know, the best way is to define the varable in Global class. 

    //Global class
    public static class Global
    {
        static string _importantData;
    
        public static string ImportantData
        {
            get
            {
                return _importantData;
            }
            set
            {
                _importantData = value;
            }
        }
    }
    
    //Page.aspx
    
    protected void Page_Load(object sender, EventArgs e)
    {
            // Get the current ImportantData.
            string important1 = Global.ImportantData;
    
            // If we don't have the data yet, initialize it.
            if (important1 == null)
            {
                important1 = DateTime.Now.ToString();
                Global.ImportantData = important1;
            }
    
            // Render the important data.
            Important1.Text = important1;
        }

    By the way, the Session object is based on single user for application.

    For more information, please check the following links:

    http://dotnetperls.com/global-variables-aspnet
    http://steveorr.net/faq/GlobalVariables.aspx
    http://www.dotnetheaven.com/UploadFile/mahesh/ApplicationState05102005051501AM/ApplicationState.aspx?ArticleID=e42f0985-9927-4fba-86bb-0012e072a0dc
    http://www.devasp.net/net/articles/display/323.html

    If you have any questions, please feel free to let me know.

    Best Regards,
    Bober Song
    --------------------------------
    Please remember to click “Mark as Answer” on the post that helps you
  • Re: global variables for concurrent access

    06-16-2009, 6:30 AM
    • Member
      2 point Member
    • nmufti
    • Member since 01-27-2009, 11:50 AM
    • Posts 26

    Thanks,

    Will this "_importantData" holds different value for each user connected to website and loads the Page.aspx.

    e.g. I want to store userName like this and want to display userName on each page of my website. Will this example work for this case? 

  • Re: global variables for concurrent access

    06-16-2009, 10:14 PM
    Answer

    Hi nmufti,

    Maybe you have misunderstood my meaning, The global data will only be run once for your entire application. so it is not based on single user.

    From your description, the best way for your requirement is to use the Session object.

    A session is the time for which a particular user interacts with a web application . During a session the unique identity of the user is maintained internally. A session ends if there is a session timeout or if you end the visitor session in code.
     
    Sessions helps to preserve data across successive accesses. These can be done on a per user basis, via the use of session objects. Session objects give us the power to preserve user preferences and other user information when browsing a web application. To better understand the use of sessions, consider an example: Suppose you own a website in which you give the visitors the option to choose the background color of the pages they will browse. In such a case you need to remember the user’s choice on each of the page. This task can be accomplished using sessions.
    ASP.NET Session State
    http://msdn.microsoft.com/en-us/library/ms972429.aspx

    Session FAQ
    http://www.syncfusion.com/faq/aspnet/web_c9c.aspx

    Underpinnings of the Session State Implementation in ASP.NET
    http://msdn.microsoft.com/en-us/library/aa479041.aspx

    Let me know whether that answers your question, or if I've missed something.

     

    Best Regards,
    Bober Song
    --------------------------------
    Please remember to click “Mark as Answer” on the post that helps you
Page 1 of 1 (10 items)