我正在抨擊我的頭對牆試圖找出爲什麼當我點擊按鈕,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();
}
}
}
變種查詢=從dbcontext.Patients患者 其中patient.Pat_Last_Name.StartsWith(textBox1.Text) 的OrderBy patient.Pat_Last_Name,patient.Pat_First_Name 選擇患者; – user1258396
以上評論是發生此異常的地方。當我嘗試保存時也會發生這種情況。由於某種原因,DbContext仍爲空! – user1258396