2014-05-15 106 views
0

我正在抨擊我的頭對牆試圖找出爲什麼當我點擊按鈕,NullReferenceException是說我的dbcontext在LINQ查詢爲null!我不明白,因爲我已經在的DbContext得到填補加載窗體時與醫院實體(見下文):從數據庫項目linq查詢NullReferenceException

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Data.Entity; 
using System.Data.Entity.Validation; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace DisplayPatientsTable 
{ 
public partial class PatientForm : Form 
{ 


    public PatientForm() 
    { 
     InitializeComponent(); 
    } 

    //Entity Framework dbcontext. All data passes through this object and the database 
    private HospitalDatabase.HospitalEntities dbcontext = null; 

    private void PatientForm_Load(object sender, EventArgs e) 
    { 
     RefreshPatients(); 
    } 

    private void RefreshPatients() 
    { 
     //Dispose of old dbcontext, if any 
     if (dbcontext != null) 
     { 
      dbcontext.Dispose(); 

      //create new dbcontext so we can reorder records based on edits 

      dbcontext = new HospitalDatabase.HospitalEntities(); 

      //use LINQ to order the Addresses table contents by last name, then first name 

      dbcontext.Patients.OrderBy(Patients => Patients.Pat_Last_Name) 
      .ThenBy(Patients => Patients.Pat_First_Name) 
      .Load(); 


      // specify DataSource for PatientsBindingSource 
      patientBindingSource.DataSource = dbcontext.Patients.Local; 
      patientBindingSource.MoveFirst(); // go to the first result 
      textBox1.Clear(); //clear the Find TextBox 

     } 
    } 

    private void pat_First_NameLabel_Click(object sender, EventArgs e) 
    { 

    } 


    private void button1_Click(object sender, EventArgs e) 
    { 
     // use LINQ to create a data source that contains only people 
     // with last names that start with the specified text 

     // use LINQ to filter contacts with last names that 
     // start with findTextBox contents 
     //Entity Framework dbcontext. All data passes through this object and the database 

     if (dbcontext != null) 
     { 
      dbcontext.Dispose(); 

      //create new dbcontext so we can reorder records based on edits 

      dbcontext = new HospitalDatabase.HospitalEntities(); 
     } 

     var query = from patient in dbcontext.Patients 
        where patient.Pat_Last_Name.StartsWith(textBox1.Text) 
        orderby patient.Pat_Last_Name, patient.Pat_First_Name 
        select patient; 






     //display matching contacts 
     // patientBindingSource.DataSource = query.ToList(); 
     // patientBindingSource.MoveFirst(); // 

     // don't allow add/delete when contacts are filtered 
     bindingNavigatorAddNewItem.Enabled = false; 
     bindingNavigatorDeleteItem.Enabled = false; 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     // allow add/delete when contacts are not filtered 
     bindingNavigatorAddNewItem.Enabled = true; 
     bindingNavigatorDeleteItem.Enabled = true; 
     RefreshPatients(); //change back to initial unfiltered data 
    } 

    private void patientBindingNavigatorSaveItem_Click(object sender, EventArgs e) 
    { 
     Validate(); // validate input fields 
     patientBindingSource.EndEdit(); 

     //try to save changes 
     if (dbcontext == null) 
     { 
      MessageBox.Show("FirstName and LastName must contain values"); 
     } 
      dbcontext.SaveChanges(); 


     RefreshPatients(); 
    } 


    } 
} 
+0

變種查詢=從dbcontext.Patients患者 其中patient.Pat_Last_Name.StartsWith(textBox1.Text) 的OrderBy patient.Pat_Last_Name,patient.Pat_First_Name 選擇患者; – user1258396

+0

以上評論是發生此異常的地方。當我嘗試保存時也會發生這種情況。由於某種原因,DbContext仍爲空! – user1258396

回答

1

你只是運行以下行,如果dbcontext != null ...但它null時首先加載表單,因此if塊內的代碼永遠不會執行。

dbcontext = new HospitalDatabase.HospitalEntities(); 

您必須重新修改您的邏輯。也許像這樣簡單,在處理對象之前檢查值,但不管如何運行其餘的代碼。

//Dispose of old dbcontext, if any 
if (dbcontext != null) 
    dbcontext.Dispose(); 

//create new dbcontext so we can reorder records based on edits 
dbcontext = new HospitalDatabase.HospitalEntities(); 

請注意,我不能就是否部署和創造這樣一個新的實體註釋是一個很好的做法 - 我不是技術不夠熟悉。我會相信的。

+0

這樣做!我甚至沒有注意到我用if語句做了這個(把括號放在錯誤的地方!)但是我有另一個異常.... – user1258396

+0

當我得到以下內容時,我得到一個EntityCommandException: – user1258396

+0

dbcontext.Patients.OrderBy(Patients => Patients.Pat_Last_Name)。ThenBy(Patients => Patients.Pat_First_Name).Load(); – user1258396