Results 1 to 7 of 7

Thread: [RESOLVED] Defining Global Variables to use in whole Application

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Quetta-Pakistan
    Posts
    852

    Resolved [RESOLVED] Defining Global Variables to use in whole Application

    Dear Experts,

    I m very new in VS (ASP.NET) and using C# in code behind file. I want to define global variables to use in whole application. EXAMPLE
    I want to save some information in LOGIN Page and display it in other pages line UserName or AccessRights etc.
    Is there anyway to do this without using Cookies and Seassion ?

    Regards

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Defining Global Variables to use in whole Application

    You can Application Varriables.

    Declare Application variables:

    Application["NewVariable"] = "Hello World!";

    then anywhere in the application you can get it this way:

    string sTemp = (string)Application["NewVariable"] ;

    Hope this helps


    Other Way
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Quetta-Pakistan
    Posts
    852

    Re: Defining Global Variables to use in whole Application

    Thanks,
    I m facing the error NullReferenceException was unhandled by user code .. Object Reference not set to an Instance of an object. I used the following codes.

    IN CLASS
    Code:
    using System;
    using System.Data;
    using System.Configuration;
    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;
    
    /// <summary>
    /// Summary description for GlobalVar
    /// </summary>
    public static class Global
    {
        /// <summary>
        /// Global variable storing important stuff.
        /// </summary>
        static string _UserName;
    
        /// <summary>
        /// Get or set the static important data.
        /// </summary>
        public static string UserName
        {
            get
            {
                return _UserName;
            }
            set
            {
                _UserName = value;
            }
        }
    
        
    }
    IN LOGIN PAGE
    Code:
                while (rdr.Read())
                {
                    objCookie.Values.Add("UserName", rdr["UserName"].ToString());
                    objCookie.Values.Add("AccessRights", rdr["AccessRights"].ToString());
    
                    string mUserName = Global.UserName;
                    mUserName = rdr["UserName"].ToString();
                    Global.UserName = mUserName;
    
                };
    I want to show this variable to other page named Default.aspx
    Master.master.cs
    Code:
        protected void Page_Load(object sender, EventArgs e)
        {
            string myUser = Global.UserName;
            lblUserName.Text = myUser.ToString();
    
        }
    1 think is observed that if I use this code in Mater Page it gives the error but if I use it in other page it gives the proper result.
    Please help me to resolve the issue
    Last edited by hafizfarooq; Mar 4th, 2011 at 06:41 AM.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    India
    Posts
    310

    Re: Defining Global Variables to use in whole Application

    hay dana.. application variable for login page??? Is it fine???
    Sagar
    VB6, VB.net,C#,ASP, ASP.net MSSQL, MYSQL

  5. #5
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    India
    Posts
    310

    Re: Defining Global Variables to use in whole Application

    Check this
    Code:
    objCookie.Values.Add("UserName", rdr["UserName"].ToString());
    objCookie.Values.Add("AccessRights", rdr["AccessRights"].ToString());
    
    mUserName = rdr["UserName"].ToString();
    check is null here[rdr["AccessRights"]]
    Sagar
    VB6, VB.net,C#,ASP, ASP.net MSSQL, MYSQL

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Quetta-Pakistan
    Posts
    852

    Re: Defining Global Variables to use in whole Application

    RESOLVED...
    I was placing control on MASTER page inside the
    Code:
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    but when plcaed the control before ContentPlaceHolder, my issue resolved
    Last edited by hafizfarooq; Mar 6th, 2011 at 02:37 AM.

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Defining Global Variables to use in whole Application

    Hello,

    What I see here is another example of re-inventing the wheel. Don't get me wrong, this is ok, but there are other things that you could spend time developing for your site if you took what is already available and used that instead.

    For instance, ASP.Net ships with the Membership, Roles and Profile Providers. (Now, some would say that these are bloated, and provide more than you actually need, but that is not my opinion). These providers will do everything that you have spoken about above, so why not give them a try?

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width