Session State

G

Guest

I have a new Windows 2003 Web server freshly installed that is to be the production web server. In the application startup it sets up a session log. The application then refers to this session variable after a forms login on its default page. This works fine on the demo system but fails on the production system with a NullReferenceException and no log record is wriiten. Any suggestions. The web.config is set up to use InProc session state processing.
 
S

Steve C. Orr [MVP, MCSD]

Perhaps the app does not have permission to write the log and therefore it's
throwing an error.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net


Timothy Elvidge said:
I have a new Windows 2003 Web server freshly installed that is to be the
production web server. In the application startup it sets up a session log.
The application then refers to this session variable after a forms login on
its default page. This works fine on the demo system but fails on the
production system with a NullReferenceException and no log record is
wriiten. Any suggestions. The web.config is set up to use InProc session
state processing.
 
S

Steve C. Orr [MVP, MCSD]

I don't know what "Logging" is but there's a good chance it is not being
instantiated correctly, possibly due to security issues.
Therefore when you try to set a reference to it in your page_load, it
doesn't exist, thus it throws the error you're seeing.
 
G

Guest

Logging is just a wrapper for a method to log pages to the database. I know the ASP.NET account has access to the database in question because this site uses Forms authentication and it can access the db to check for usernames and passwords
This is the Logging clas
public class Logging : System.Web.UI.Pag


public void LogPage (string PageName, string FromPage

SqlConnection conn = new SqlConnection (ConfigurationSettings.AppSettings["WebConnectionString"])
try

conn.Open ()
SqlCommand doClient = new SqlCommand ("dbo.CreateLog", conn)
doClient.CommandType = CommandType.StoredProcedure
doClient.Parameters.Add ("@Campaign",SqlDbType.VarChar)
doClient.Parameters["@Campaign"].Value = Session["Campaign"].ToString ()
doClient.Parameters.Add ("@ClientID", SqlDbType.Int)
doClient.Parameters["@ClientID"].Value = (int) Session["ClientID"]
doClient.Parameters.Add ("@Page", SqlDbType.VarChar)
doClient.Parameters["@Page"].Value = PageName
doClient.Parameters.Add ("@From", SqlDbType.VarChar)
doClient.Parameters["@From"].Value = FromPage
doClient.ExecuteNonQuery ()

finally

conn.Close ()





As such instantiating it should cause a problem as no connection is mades to the database until the method is called
I beleive it throws the nullReferenceException when it try the Logging log = (Logging) Session["Logging"];
and finds a null Reference for the Session["Logging"] so I beleive it has something to with Session variables not being saved. Remember this code runs fine on the debugable development environment, but not the production environment where only the .aspx files, web.config and the loyaltynz.dll in the bin directory are available
line is executed ???
 
S

Sebastien Lambla

Timothy said:
No this is the code in global.asax :-
protected void Session_Start(Object sender, EventArgs e)
{
Session["MyShoppingBasket"] = new ShoppingBasket ();
Session["Logging"] = new Logging ();
}

And it throws an exception in the Page_load event
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
Logging log = (Logging) Session["Logging"];
log.LogPage ("default", Request.UrlReferrer.ToString ());
}

Ith throws the nullReferenceException when it try the Logging log = (Logging) Session["Logging"];
line is executed ???

Your NullReferenceException is thrown because your Session["Loggig"]
returns null. When you try to cast it using (Logging) it fails miserably.

Quick fix, rewrite as such:
Logging log = Session["Logging"] as Logging;
if (log == null)
{
Session["Logging"] = (log = new Logging());
}
then it should work.

On a wider note, it means your Session_Start was not called. Check to be
sure that the session is actually created (can be caused by cookies
problems or lack of authentication). Best way would be to put a
breakpoint on your Session["Logging"] = new Logging() line and see if,
when you request the page, it is indeed executed. My guess would be that
it is not.

As a side note, if this is supposed to be executed for all users, you
might want to put that object in Application isntead, as if it is a
utility lib, you don't need to keep it in memory.

Reading your code, i don't really understand why your Logging class
inherit from a Page? That could certainly cause problems as the Page
class is not supposed to be instanciated out of nowhere. Is there any
reason? Wouldn't it be easier and simpler to have a utility Logging
class with a static method named LogPage? That way you don't even need
to keep the object around.
 
S

Steve C. Orr [MVP, MCSD]

Errors that occur in the Global.asax (like in your situation) are
notoriously difficult to troubleshoot remotely.
You need to put a try/catch block around your Session_Start code and write a
log or something when it fails.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net




Timothy Elvidge said:
How can I turn of the ASP.NET error messages so that I can use
Reponse.Write to find where it is breaking the application.
 
G

Guest

I removed the code from the global.ax.cd Session_Start block to the Login code and the application works fine. Any ideas why Session_Start would run on one web server but not another? The reason the Logging class inherits from System.Web.UI.Page is that otherwise it doesn't have access to the session variables. I'd write it a different way if the method didn't work passing all Session variables from the calling page. However my problem is with the Session_Start code not being executed.
 
G

Guest

I removed the code from the global.ax.cd Session_Start block to the Login code and the application works fine. Any ideas why Session_Start would run on one web server but not another?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top