Results 1 to 6 of 6

Thread: How To Store Global Variables?

  1. #1
    Join Date
    Nov 2009
    Posts
    117

    How To Store Global Variables?

    Hello, I am learning Asp.net programming and while making use of it, I want to know the details from which I will able to declare the global variables in Asp.net. If you are having any knowledge about it, the please provide details about it. I will be thankful to you.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    re: How To Store Global Variables?

    Hello, I have the simplest code below, which can be useful to you for declaring the global variables:
    Code:
    using System;
    using System.Data;
    using System.Linq;
    using System.Web;
    public static class varDeclaration
    {    
        static string str;
        public static string ImportantData
        {
            get
            {
                return str;
            }
            set
            {
                str = value;
            }
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    re: How To Store Global Variables?

    If you use the Application objects in your Asp.net then it is called as the global variables at the application level. And it is needed to be shared at all of the users of sessions. So, the data which is specific to the user is not saved at the application level. And while making use of the application object that object is locked and that object is used by only one page at a time. You will able to know the details about it, from the following code:

    Code:
    Application.Lock();
    Application("UserData") = "Techarena";
    Application.UnLock();
    Response.Redirect("mypage.aspx");
    String str = Application("UserData").ToString();

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    re: How To Store Global Variables?

    Hello, if you simply make use of the static variable in your program then you can able to get the global varibles in asp.net as below:

    Code:
    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial First : System.Web.UI.Page
    {
        protected void Page_Load(object obj, EventArgs ea)
        {
            string data = Global.ImportantData;
            if (data == null)
            {
                data = DateTime.Now.ToString();
                Global.ImportantData = data;
            }   
            Important1.Text = data;
        }
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    re: How To Store Global Variables?

    If you make use of the cache object then you will able to use the global variable in your asp.net. There is not need of the any type of explicit locking and unlocking. If you check the below mentioned code then you will able to get the use of it:
    Code:
    Cache("Userdata") = "TA";
    Response.Redirect("page.aspx");
    String str = Cache("Userdate").ToString();
    And if you are making use of the cache object then it will simply share all of your data with all of the sessions. And as we know it will simply expire after some specified time of interval.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    re: How To Store Global Variables?

    Hi, you can simply use the session object for your use if you want to declare the global variable. And it will store all of the data which is used by the any type of user till that session. You can simply make use of the session object with the help of following souce code:
    Code:
    Session("Name") = txtUserName.Text;
    Server.Transfer("end.aspx");
    String str = Session("Name").ToString();

Similar Threads

  1. Exporting or importing global variables from a DLL to an EXE
    By SALAZAR in forum Software Development
    Replies: 5
    Last Post: 28-05-2012, 02:26 PM
  2. Replies: 5
    Last Post: 01-03-2011, 10:14 AM
  3. Global Variables in C sharp
    By Harper 21 in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 08:43 AM
  4. Java bans Global Variables
    By KALLIYAN in forum Software Development
    Replies: 4
    Last Post: 10-11-2009, 04:17 AM
  5. iText: page events and global variables
    By ASHER in forum Software Development
    Replies: 3
    Last Post: 24-10-2009, 08:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,564,606.13110 seconds with 17 queries