如何解决Linq编辑查询中的空引用异常? [英] How to resolved Null Reference Exception in Linq edit query?
问题描述
您好,
我在wpf datagrid中使用此代码进行编辑,但是它获得了空引用异常的错误,但数据(SNo)已经填入数据网格。
private void btnEdit_Click(object sender,RoutedEventArgs e)
{
InvoiceRecordsDataContext dataContext = new InvoiceRecordsDataContext( );;
InvoiceRecord irRow = dgInvoiceRecord.SelectedItem as InvoiceRecord;
int m = irRow.SNo; 错误获取空引用异常
InvoiceRecord ir =(来自dataContext.InvoiceRecords中的p
其中p.SNo == irRow.SNo
select p).Single() ;
ir.ProjectDetail = irRow.ProjectDetail;
ir.InvoiceDetail = irRow.InvoiceDetail;
ir.MilestonePercentage = irRow.MilestonePercentage;
ir.Amount = irRow.Amount;
dataContext.SubmitChanges();
MessageBox.Show(行已成功更新。);
DisplayInvoiceRecords() ;
}
请帮帮我。
先谢谢。
Ankit Agarwal
软件工程师
Hello,
I am using this code for edit in wpf datagrid, but it getting error of null reference exception but data (SNo) is already filled in datagrid.
private void btnEdit_Click(object sender, RoutedEventArgs e)
{
InvoiceRecordsDataContext dataContext = new InvoiceRecordsDataContext();
InvoiceRecord irRow = dgInvoiceRecord.SelectedItem as InvoiceRecord;
int m = irRow.SNo;"error its getting "Null Reference Exception"
InvoiceRecord ir = (from p in dataContext.InvoiceRecords
where p.SNo == irRow.SNo
select p).Single();
ir.ProjectDetail = irRow.ProjectDetail;
ir.InvoiceDetail = irRow.InvoiceDetail;
ir.MilestonePercentage = irRow.MilestonePercentage;
ir.Amount = irRow.Amount;
dataContext.SubmitChanges();
MessageBox.Show("Row Updated Successfully.");
DisplayInvoiceRecords();
}
Please help me.
Thanks in Advance.
Ankit Agarwal
Software Engineer
推荐答案
dgInvoiceRecord.SelectedItem
是null
,或dgInvoiceRecord.SelectedItem
不是InvoiceRecord的实例
class。
调试代码以找出是哪种情况。
EitherdgInvoiceRecord.SelectedItem
isnull
, ordgInvoiceRecord.SelectedItem
is not an instance of theInvoiceRecord
class.
Debug your code to find out which is the case.
这篇关于如何解决Linq编辑查询中的空引用异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!