Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 3rd, 2007, 04:53 PM
Registered User
 
Join Date: May 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default [Resolved] NullReference on Session variables

I can't seem to access or set session variables in my app... everything's null. I've tried both Session["var"] and HttpContext.Current.Session["var"] and get a NullReferenceException every time.

am I missing something? Do I have to check each and every variable to see if it's null before I try to use it?
 
Old January 4th, 2007, 02:58 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In Session_OnStart eventHander declair all session variable with default value.

Bijgupt
 
Old January 4th, 2007, 10:12 AM
Registered User
 
Join Date: May 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hmm.. did not think about that.

I ended up solving it by *casting* each session variable to a string, instead of .ToString()ing the session variables.

You cannot ToString() a null object, but you can cast it to a string.

 
Old April 10th, 2007, 10:08 AM
Authorized User
 
Join Date: Dec 2006
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
I have the same problem and casting it to string is not helping either. I am trying to debug the application and i have defines Session variable in Global.asax file but the control doesnt go there.
Here is my Global.asax code
 void httpmodule_SessionHandler(Object sender, EventArgs e)
    {
        /// <summary>
        ///Instance of cGlobal class witch will manage every request to web server
        /// </summary>
       Conversion _session = new Conversion();
    }
    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        //Conversion _session = new Conversion();

    }

      void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        Conversion _session = new Conversion();
        Session["Started"] = true;

    }

Conversion is another class file where i am trying to get the value of session variable and getting NullRefernceException Error.
Here is code for that class:

public class Conversion
{
    string strServerName;
    string logonid;
    Database db = null;
    HttpContext _http = HttpContext.Current;

   string _connString = ConfigurationSettings.AppSettings["connString"];

    public Conversion()
    {

            //get the environment and data connection string
            strServerName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
            strServerName = strServerName.ToString().Trim().ToUpper();
         if ((String)HttpContext.Current.Session["Started"]==null )
            {
                switch (strServerName)
                {
                                    }

   Any help will be appreciated.

Thanks
Kamal






Similar Threads
Thread Thread Starter Forum Replies Last Post
Session Variables in C# shikha09 C# 1 November 28th, 2006 10:38 AM
Session Variables testsubject Visual Studio 2005 8 March 8th, 2006 04:26 PM
Is it possible for me using session variables into see07 ASP.NET 1.x and 2.0 Application Design 4 March 9th, 2005 07:46 PM
Session Variables - help? Annoyamouse BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 11 August 31st, 2004 03:56 PM
session variables help face Classic ASP Databases 4 September 12th, 2003 03:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.