0

I'm trying call a method that takes a database name and data stored in a session. I get a nullreferenceexception when I retrieve the database name using Server.MapPath("PayrollSystem_DB.mdb").

I use the same code to pass the database name in other methods and they work fine.

// Sends data to SavePersonel() to write to personnel table
    if (clsDataLayer.SavePersonnel(Server.MapPath("PayrollSystem_DB.mdb"),
                                   Session["txtFirstName"].ToString(),
                                   Session["txtLastName"].ToString(),
                                   Session["txtPayRate"].ToString(),
                                   Session["txtStartDate"].ToString(),
                                   Session["txtEndDate"].ToString()))
    {
        txtVerifiedInfo.Text = txtVerifiedInfo.Text +
                              "\nThe information was successfully saved!";

    }
    else
    {
        txtVerifiedInfo.Text = txtVerifiedInfo.Text +
                             "\nThe information was NOT saved.";


    }

This code takes the data in frmPersonel, saves it to session and redirects to frmPersonelVerified

        //If nothing is added to the error message data is recorded to session.
        if (errorMessage == "")
        {
            //saves data to session
            Session["firstName"] = txtFirstName.Text;
            Session["lastName"] = txtLastName.Text;
            Session["payRate"] = txtPayRate.Text;
            Session["startDate"] = txtStartDate.Text;
            Session["endDate"] = txtEndDate.Text;

            Response.Redirect("frmPersonnelVerified.aspx");

        }

This is frmPersonelVerified it takes the variables from session, displays them in a textbox with a message whether or not writing to the database was successful.

public partial class frmPersonalVerified : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    string output = "";

    output += Session["firstName"].ToString() + Environment.NewLine;
    output += Session["lastName"].ToString() + Environment.NewLine;
    output += "Pay Rate: " + Session["payRate"].ToString() + Environment.NewLine;
    output += "Start Date: " + Session["startDate"].ToString() + Environment.NewLine;
    output += "End Date: " + Session["endDate"].ToString() + Environment.NewLine;
    txtVerifiedInfo.Text = output;

    Debug.Assert(Session != null);
    Debug.Assert(Session["txtLastName"] != null);
    Debug.Assert(Session["txtPayRate"] != null);
    Debug.Assert(Session["txtStartDate"] != null);
    Debug.Assert(Session["txtEndDate"] != null);

    // Add your comments here
    if (clsDataLayer.SavePersonnel(Server.MapPath("PayrollSystem_DB.mdb"),
                                   Session["txtFirstName"].ToString(),
                                   Session["txtLastName"].ToString(),
                                   Session["txtPayRate"].ToString(),
                                   Session["txtStartDate"].ToString(),
                                   Session["txtEndDate"].ToString())
                                   )
    {
        txtVerifiedInfo.Text = txtVerifiedInfo.Text +
                              "\nThe information was successfully saved!";

    }
    else
    {
        txtVerifiedInfo.Text = txtVerifiedInfo.Text +
                             "\nThe information was NOT saved.";


    }
}



protected void btnViewPersonnel_Click(object sender, EventArgs e)
{
    Response.Redirect("frmViewPersonnel.aspx");
}


}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 04 '14 at 18:22

1 Answers1

0

Your session variables should not have the txt prefix. Remove them. Use the same ones as the ones you use to set them.

Raja Nadar
  • 9,409
  • 2
  • 32
  • 41
  • I get the same error, even with the whole file path. Thanks though. –  Apr 04 '14 at 00:44
  • relative to the root of your web project, where is this mdb file stored? – Raja Nadar Apr 04 '14 at 01:18
  • ProjectFile/App_Data/PayrollSystem.mdb –  Apr 04 '14 at 01:32
  • What happened to the _DB at the end of the file name? – Raja Nadar Apr 04 '14 at 01:56
  • So what is the value of Server.MapPath("~/App_Data/PayrollSystem_DB.mdb"): – Raja Nadar Apr 04 '14 at 03:05
  • C:\Users\Owner\Desktop\Web App class\Mans2\App_Data\PayrollSystem_DB.mdb –  Apr 04 '14 at 03:19
  • Cool. That means you're getting a value now. No null reference? If u still get a null reference, then one of the other session variable is null. Do a check before doing ToString() – Raja Nadar Apr 04 '14 at 04:55
  • Did some testing and I need to explain my code a little more: I enter the data in one page in a series of text boxes that are saved to session variables once the user clicks submit. I put a multiline text box on this page and created a string of these variables formatted with newlines and only end date would appear. I entered the complete set of data but only retrieved the first name variable session and it displayed in the text box. –  Apr 04 '14 at 06:11
  • Couldn't edit that last comment again. I can only get one variable out of session it seems. here is the code I'm using to test: `string output = ""; output = Session["firstName"].ToString() + Environment.NewLine; output = Session["lastName"].ToString() + Environment.NewLine; output = "Pay Rate: " + Session["payRate"].ToString(); output = "Start Date: " + Session["startDate"].ToString(); output = "End Date: " + Session["endDate"].ToString(); TextBox1.Text = output;` –  Apr 04 '14 at 06:34
  • you're missing the output = output + new stuff; you're overwriting output everytime by not adding to existing ouput string – Raja Nadar Apr 04 '14 at 06:48
  • That fixed my output on that text box and I can retrieve each variable from session, but I am back at square one. –  Apr 04 '14 at 07:14
  • can you post the full exception trace? what value is null? – Raja Nadar Apr 04 '14 at 08:29
  • Stack trace:[NullReferenceException: Object reference not set to an instance of an object.] frmPersonalVerified.Page_Load(Object sender, EventArgs e) in c:\Users\Owner\Desktop\Web App class\Mans2\frmPersonnelVerified.aspx.cs:23 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51 System.Web.UI.Control.OnLoad(EventArgs e) +92 System.Web.UI.Control.LoadRecursive() +54 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772 –  Apr 04 '14 at 16:48
  • if (clsDataLayer.SavePersonnel(Server.MapPath("PayrollSystem_DB.mdb"), This line is highlighted when the exception is thrown. –  Apr 04 '14 at 16:49
  • alright. edited the answer with additional code snippet to find out the culprit variable. – Raja Nadar Apr 04 '14 at 16:55
  • I got an assertion failed message for each of those lines of code. I couldn't copy any of it to paste here. –  Apr 04 '14 at 17:33
  • It means you don't have any of those values in Session or session itself is null. Can you check Debug.Assert(Session != null); – Raja Nadar Apr 04 '14 at 17:38
  • Assertion failed message for that too. Its weird though as I am using session variables just before that assertion code code and I get no error. –  Apr 04 '14 at 17:59
  • Then the problem is that the session is not available in this context. Is this a page method or a method called by a page or event handler? Can you post more code explaining how you reach here? – Raja Nadar Apr 04 '14 at 18:02
  • Edited original post to include saving data to session variables and redirecting to the next page. –  Apr 04 '14 at 18:11
  • Thanks. Edited my answer with the root cause. Makes sense? The session object should not be null since you re using it a few lines ago. – Raja Nadar Apr 04 '14 at 18:18