我尝试调用一个方法来获取存储在会话中的数据库名称和数据。当我使用Server.MapPath("PayrollSystem_DB.mdb")
检索数据库名称时,我得到一个nullreferenceexception。
我使用相同的代码在其他方法中传递数据库名称,它们工作正常。
// 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.";
}
此代码获取frmPersonel中的数据,将其保存到会话并重定向到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");
}
这是frmPersonelVerified它从会话中获取变量,在文本框中显示它们,并显示是否写入数据库是否成功。
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");
}
}
答案 0 :(得分:0)
您的会话变量不应包含txt前缀。删除它们。使用与您用来设置它们相同的那些。