[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
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
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
Re: Defining Global Variables to use in whole Application
hay dana.. application variable for login page??? Is it fine???
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"]]
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
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