对象引用未设置为对象的实例

时间:2012-05-15 09:09:46

标签: c#

我正在从数据库表中检索数据。

中输入为1
textbox1(voucherno)-->damagedetail = 
                      web.getdamageddetail(Convert.ToInt32(voucherno.Text));

我希望将 branchno 作为textbox2(branchno)中的输出

branchno.Text = damagedetail.branchno.ToString();

但是收到错误

  

对象引用未设置为对象的实例。

检查我的第二行。

第二行编码是否正确?

2 个答案:

答案 0 :(得分:3)

damagedetail.branchno为空。

试试这个:

if (damagedetail.branchno != null)
    branchno.Text = damagedetail.branchno;
else
    branchno.Text = "abcd";

答案 1 :(得分:0)

您的对象属性未被填充,因此当您从对象中检索值“branchno”时,该值等于null,或者您未在“getdamageddetail”方法中填充该属性