2014-04-03 11 views
0

세션에 저장된 데이터베이스 이름과 데이터를 사용하는 메서드를 호출하려고합니다. Server.MapPath("PayrollSystem_DB.mdb")을 사용하여 데이터베이스 이름을 검색 할 때 nullreferenceexception이 발생합니다.데이터베이스를 매개 변수로 보낼 때 Null 참조 예외가 발생했습니다.

나는 다른 코드에서 데이터베이스 이름을 전달하기 위해 동일한 코드를 사용하며 정상적으로 작동한다.

// 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

'NullReferenceException'의 거의 모든 사례가 동일합니다. 일부 힌트는 "[.NET의 NullReferenceException은 무엇입니까?] (http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)"를 참조하십시오. –

답변

0

세션 변수에 txt 접두어가 없어야합니다. 그들을 제거하십시오. 설정에 사용하는 것과 동일한 것을 사용하십시오.

+0

전체 파일 경로에서도 동일한 오류가 발생합니다. 그래도 고마워. 웹 프로젝트의 루트에 상대적인 – Bizmark

+0

이 mdb 파일은 어디에 저장되어 있습니까? –

+0

ProjectFile/App_Data/PayrollSystem.mdb – Bizmark

관련 문제