-
Mar 4th, 2011, 01:45 AM
#1
Thread Starter
Fanatic Member
[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
-
Mar 4th, 2011, 03:35 AM
#2
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
-
Mar 4th, 2011, 06:23 AM
#3
Thread Starter
Fanatic Member
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.
-
Mar 4th, 2011, 07:14 AM
#4
Hyperactive Member
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
-
Mar 4th, 2011, 07:16 AM
#5
Hyperactive Member
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
-
Mar 4th, 2011, 07:18 AM
#6
Thread Starter
Fanatic Member
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.
-
Mar 6th, 2011, 05:53 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|