如何解决Linq编辑查询中的空引用异常? [英] How to resolved Null Reference Exception in Linq edit query?

查看:178
本文介绍了如何解决Linq编辑查询中的空引用异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我在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。



调试代码以找出是哪种情况。
Either dgInvoiceRecord.SelectedItem is null, or dgInvoiceRecord.SelectedItem is not an instance of the InvoiceRecord class.

Debug your code to find out which is the case.


这篇关于如何解决Linq编辑查询中的空引用异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆