为什么对象引用没有设置错误来自此代码。 [英] why object refference not set error comes on this code.

查看:62
本文介绍了为什么对象引用没有设置错误来自此代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class user_apointmentTicketPrint : System.Web.UI.Page
{
    Class1 ob = new Class1();
    protected void Page_Load(object sender, EventArgs e)
    {
        ob.conn();
            string pno = Session["pno"].ToString();
        ob.fetch("select * from patient_reg where pname='" + pno + "'");
        lblpno.Text = ob.ds.Tables[0].Rows[0].ItemArray[0].ToString();
        lbldrname.Text = ob.ds.Tables[0].Rows[0].ItemArray[7].ToString();
        lbldept.Text = ob.ds.Tables[0].Rows[0].ItemArray[6].ToString();
        lblpname.Text = ob.ds.Tables[0].Rows[0].ItemArray[1].ToString();
        lblage.Text = ob.ds.Tables[0].Rows[0].ItemArray[4].ToString();
        lblplace.Text = ob.ds.Tables[0].Rows[0].ItemArray[10].ToString();
    }
}

推荐答案

请参阅最受欢迎的答案 Sergey 对象引用未设置为对象的实例,请参阅下面的代码。 [ ^ ]。



此外,在使用 Session 之类的任何属性之前,请检查是否 null
Refer the most popular answer by Sergey "Object Reference not set to Instance of an object", please see the code below.[^].

Moreover, before using any property like Session, check if that is null or not.


检查下列其中一项是否为空 -

Check if one of the following are null -
ob.fetch("select * from patient_reg where pname='" + pno + "'");
       lblpno.Text = ob.ds.Tables[0].Rows[0].ItemArray[0].ToString();
       lbldrname.Text = ob.ds.Tables[0].Rows[0].ItemArray[7].ToString();
       lbldept.Text = ob.ds.Tables[0].Rows[0].ItemArray[6].ToString();
       lblpname.Text = ob.ds.Tables[0].Rows[0].ItemArray[1].ToString();
       lblage.Text = ob.ds.Tables[0].Rows[0].ItemArray[4].ToString();
       lblplace.Text = ob.ds.Tables[0].Rows[0].ItemArray[10].ToString();





因为你正在使用所有这些都是ToString, null 会抛出错误。

尝试使用 Convert.ToString()而不是。



Since you are using ToString on all of them, null will throw errors.
Try using Convert.ToString() instead.


这篇关于为什么对象引用没有设置错误来自此代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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