CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2002
    Posts
    1,798

    [RESOLVED] Session variable puzzle ?

    I have a relatively simple 3 page Asp.NET website with a default, input, and calculation pages. A user can load the default page, click on 'input' to fill in some info and press a button to make calculations on that info. The user can than use the browser back arrow to navigate back to the default page. The default page has some session variables that the user will need.

    My problem is that while the string session variables work ok, the float variables error out when the default page is first accessed. This makes little sense to me, but I must be doing something wrong.

    Here's some of the code showing the opening (default) page where the Session variables are first defined, and the calculation page where the Session variables are set.

    Code:
    public partial class _Default : System.Web.UI.Page 
    {
        protected string slname, sfname, sopproc;
        protected float pdeath, pstroke, prenal, presp, pwound, preop, pany, plstay, psstay;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            slname = "Fudd";
            sfname = "Elmer";
            sopproc = "CABG";
            pdeath = 2.8F;
            pstroke = 1.8F;
            prenal = 3.2F;
            presp = 8.4F;
            pwound = 1.0F;
            preop = 3.2F;
            pany = 18.9F;
            plstay = 6.8F;
            psstay = 24.2F;
    
            slname = (string)Session["LNAM"];
            sfname = (string)Session["FNAM"];
            sopproc = (string)Session["OPPROC"];
            pdeath = (float)Session["PDEATH"];
          // ..
    
        }
    
    
    public partial class RiskCalc : System.Web.UI.Page
    {
    
        public double pdeath, pstroke, prenal, presp, pwound, preop, pany, plstay, psstay;
    
        protected void Page_Load(object sender, EventArgs e)
        {
    	//.. Do calculations for pdeath, ..., etc
    
            //====== Set the patient session variables
            Session["LNAM"] = s1;
            Session["FNAM"] = s2;
            Session["OPPROC"] = "Isolated Coronary Artery Bypass";
            Session["PDEATH"] = (float)pdeath;
            //..
    While the first three string session variables work just fine, the float variables cause the following error when the default page is first accessed:
    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:


    Line 41: sfname = (string)Session["FNAM"];
    Line 42: sopproc = (string)Session["OPPROC"];
    Line 43: pdeath = (float)Session["PDEATH"]; <== here's the error
    //..
    Why the string variables should work and not the float variables is a mystery to me. Can anyone shed light on this puzzling state of affairs ?

    Thanks
    mpliam

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Session variable puzzle ?

    You should always check if an session variable is null or not.

    the string conversion does not fail because a string can be null. A float can't.

    Debug the following and check the results
    Code:
          object myobject = null;
          string s = (string)myobject;
          float f = (float)myobject;
    'myobject' is bassically the same as the Session. The Session will return an object.

    Anyway, 's' will be null and an error will occur on the float line..

  3. #3
    Join Date
    May 2002
    Posts
    1,798

    Re: Session variable puzzle ?

    Thanks. I have implemented null checking and that seems to fix most of the problems.
    mpliam

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Session variable puzzle ?

    Please mark your thread Resolved if you feel that it has been solved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured