[英]Filter data grid view list with the value from the search text box: 'Object reference not set to an instance of an object.'
[英]Unable to update itemfrom Data-grid “Object reference not set to an instance of an object.”
关于我的应用程序,我遇到了一个问题,没有选择要使用EF更新的项目。
我正在创建一个应用程序,供用户输入其组织详细信息。 似乎很简单。 使用选项卡控件输入数据后,我将拥有一个包含一系列数据网格的视图,该视图使用户可以更新或插入另一条数据(如果需要)或具有删除功能。
在我的视图模型中,我遇到了一系列不同的事情(这可能不是实现MVVM的最佳方法,而是最终尝试实现这一目标)。
我有实现INOTIFYPROPERTYCHANGE
属性。 保存,更新和删除的命令。 还有我的实体方法(使用Entity Framework的CRUD操作-稍后将在我进一步了解MVVM等时使用存储库系统)。
到目前为止,我必须使用视图背后的代码来加载可以正常工作的数据(显然,我不应该这样做,因此向我展示任何建议将不胜感激),就像这样;
private void Page_Loaded(object sender, RoutedEventArgs e)
{
DBEntities context = new DBEntities();
var orgTypeDetails = (from o in context.OrganisationTypeDetails
select o).ToList();
dgOrgTypeDetail.ItemsSource = orgTypeDetails;
}
我还有一个事件处理程序,它仅允许您选择一行,然后更新该特定行;
private void btnUpdateOrgTypeDetail_Click(object sender, RoutedEventArgs e)
{
OrganisationTypeDetailViewModel org = new OrganisationTypeDetailViewModel();
OrganisationTypeDetail selected = dgOrgTypeDetail.SelectedItem as OrganisationTypeDetail;
if (selected == null)
MessageBox.Show("You must select a 'Type' before updating.");
else
{
OrganisationTypeDetailUpdateView update = new OrganisationTypeDetailUpdateView();
update.ShowDialog();
org.UpdateOrganisationTypeDetail(selected);
Page_Loaded(null, null);
}
}
最后,我的方法是使用View-Model中的EF更新表;
public void UpdateOrganisationTypeDetail(OrganisationTypeDetail orgTypeDetail)
{
using (DBEntities context = new DBEntities())
{
//var orgTD = context.OrganisationTypeDetails.Where(otd => otd.OrganisationTypeDetailID == OrganisationTypeDetailID).FirstOrDefault();
var orgTD = (from a in context.OrganisationTypeDetails
where a.OrganisationTypeDetailID == OrganisationTypeDetailID
select a).FirstOrDefault();
orgTD.OrganisationTypeDetailID = OrganisationTypeDetailID;
orgTD.OrganisationTypeID = OrganisationTypeID;
orgTD.Title = Title;
orgTD.FirstName = FirstName;
orgTD.Surname = Surname;
orgTD.Position = Position;
orgTD.DateOfBirth = DateOfBirth;
orgTD.Address = Address;
orgTD.Country = Country;
orgTD.Postcode = Postcode;
orgTD.PhoneNumber = PhoneNumber;
orgTD.MobileNumber = MobileNumber;
orgTD.FaxNumber = FaxNumber;
orgTD.Email = Email;
orgTD.NINumber = NINumber;
context.OrganisationTypeDetails.ApplyCurrentValues(orgTD);
context.SaveChanges();
MessageBox.Show("Updated Organisation Type Details");
}
执行此应用程序时,它会产生Null Reference exception
。
在我的属性中,为确保它不会崩溃,我不得不以编程方式设置ID; public int _OrganisationTypeDetailID=17;
但是我希望能够选择一个行,而不是只选择从17行中选择行。
我试过同时使用Linq和Lamba表达式,但似乎都不起作用。
抱歉,阅读本书有很多帮助,并愿意在需要时添加更多代码或解释:)。
如果查询未返回任何结果,则FirstOrDefault返回type的默认值,因此可以在FirstOrDefault之后检查结果是否为null。
它应该看起来像这样
var orgTD = (from a in context.OrganisationTypeDetails
where a.OrganisationTypeDetailID == OrganisationTypeDetailID
select a).FirstOrDefault();
if(orgTD == null)
return;
如果确定该查询返回至少一个元素,请使用“ 第一”代替
我花了两个星期的时间来处理与Visual Studio .NET数据实体模型连接到MySQL数据库的类似问题。 经过大量的讨论,并在StackOverflow,MySQL和MS网站论坛和帮助资源中进行了搜索之后,对空引用错误的答案就如预期的那样相当简单,因此如下所示:
我已经安装了64位MySQL服务器,但MS Visual Studio都是32位。 无论我做了什么app.config更改,无论我如何使用参考,无论通过Nuget安装了多少软件包,我都无法解决该问题,除非卸载MySQL 64位(确保对现有的反向工程)数据库首先要建模,因此您可以稍后重新加载它们),然后重新安装MySQL 32位。 重新创建数据库,重建数据实体模型和数据源之后,将解决空引用错误。
所以...如果您将MySQL与MS Visual Studio一起使用,请确保您拥有32位MySQL产品。
(我所做的可能会影响结果或可能不会影响结果的另一件事是安装Windows IIS来访问localHost-我认为这只是对实际问题的干扰,因此,仅将其添加为最后的选择)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.