• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1325
  • Last Modified:

Using profile in Global.asax

Hello all,

I am hoping this is an easy 500 points for someone. I have an intranet application which is pulling the display name from the currently logged in user and all of that works fine.  What I want to do is store the information in the users profile class.  I was able to store this in ther session object but want to use the new profile class since it is strongly typed and new & pretty. I have followed all of the examples i can find and it works except when I try to set the value in the global.asax.

Profile.UserDisplayName = "GeorgeTest";

This works fine is i use it in an aspx page but gives me an error in the global.asax.

The error:

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="App_global.asax.rktz4fd-"
  StackTrace:
       at ASP.global_asax.Session_Start(Object sender, EventArgs e) in c:\Documents and Settings\dukeg\My Documents\Visual Studio 2005\WebSites\JanetsApp\Global.asax:line 46
       at System.Web.SessionState.SessionStateModule.RaiseOnStart(EventArgs e)
       at System.Web.SessionState.SessionStateModule.OnStart(EventArgs e)
       at System.Web.SessionState.SessionStateModule.CompleteAcquireState()
       at System.Web.SessionState.SessionStateModule.BeginAcquireState(Object source, EventArgs e, AsyncCallback cb, Object extraData)
       at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
0
dukestaTAI
Asked:
dukestaTAI
  • 7
  • 5
1 Solution
 
dukestaTAIAuthor Commented:
Should I not be setting the profile object in the global.asax?
0
 
dukestaTAIAuthor Commented:
Also I am using this in the Session start method.
0
 
dukestaTAIAuthor Commented:
I found out that the Profile has not been instantiated yet in the Session start mentod.  So i am changing my question, When should I set the profile value so it only sets it once?  I am bout to go back to just using the session object:(
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
mrichmonCommented:
You actually should consider caching the session object.

So if it exists you don't set it, if it doesn't exists then do set it.  Then it is only set whenever it needs to be and not every time.
0
 
dukestaTAIAuthor Commented:
If i set the session variable in the global.asax file in the on session start event then why would i need to cache it?  It will still only get set once, correct?
0
 
mrichmonCommented:
It iwll get set once per page load.

Versus caching it will get set only if it is not already set.
0
 
mrichmonCommented:
So to answer the question...

Different events in the global.asax page fire at different times.  Most fire on every page load.  Some only fire at certain times.

I read the question too quickly when I said above that it will fire every page load.  I should clarify.

The Session Start fires on every page load in which it detects one of the following conditions:
The session does not yet exist - as in there is no session ID
The session referenced has expired.

So in a way you can think of it firing each time a page request is made, but only executing when one of those conditions hold true.

To initialize session variables once per session you should use the Session_OnStart event.  More detailed information can be here:

ASP.NET Session State
http://msdn2.microsoft.com/en-US/library/87069683.aspx

Session-State Events
http://msdn2.microsoft.com/en-US/library/ms178583.aspx
0
 
dukestaTAIAuthor Commented:
mrichmon,

Thanks for the post, I pretty much knew that session_onstart was where i would set the session variable, but back to what you are implying with caching benefits.  How does caching help in this situation?  The code in the global.asax session_onstart is only going to execute once per session anyway.  Am i missing something?
0
 
mrichmonCommented:
Well I guess it really depends.  We tend to cache the session object when it is simply profile information so that if the session ends and we can re-create it without the user realizing the first session ended and the second one started.  To them it appears the same.  Now I don't know the complete set-up we have is "caching" in the context that it doesn't use built in caching, but it is how we have termed it.
0
 
dukestaTAIAuthor Commented:
I appreciate the input, however my original question is to find out how to use an ASP.NET 2.0 <profile> to store this information and only load it once.

I would like to hear from anyone who can shed some light on this for me.
0
 
mrichmonCommented:
Yes the actual profile object is created as the page request is loaded.  This is after the session begin occurs.  It happens on each page as I understand it.

You may want to read the bottom of this article I just found which talks about hte internal implementation of the profile in .NET 2
http://msdn.microsoft.com/msdnmag/issues/05/10/CuttingEdge/

However, I think the answer to the question you are looking for is that you can't control when the asp.net profile object is populated.  Usually only setting once is done by having a preferences page or something similar and that page sets the profile settings, not in the global.asax page.  That way it is set once, but read throughout the project.

Is that what you are wondering about?
0
 
dukestaTAIAuthor Commented:
Yeah that pretty much answers it, I guess I should leave this dead horse alone.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now