本文介绍了如何解决未设置为对象实例的对象引用。 Asp.net C#web的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..

我面临着解决此错误的问题我一次又一次地尝试解决这个问题但我没有解决这个问题,我在sql中执行数据库查询工作很好,但是这里出现了这个错误。

这里错误:

Hi..
I'm facing problem to resolve this error i try again and again to solve this but I'm failed to resolve this issue,I execute the database query in sql it working fine but here this error come.
here error :

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 33:                     string value = row["Database_Name"] + "";
Line 34:                     parentNode.ChildNodes.Add(new TreeNode(value));
Line 35:                     lv.Items.Add(value);//HERE ERROR COME RED 
Line 36: 
Line 37: 





我尝试了什么:



这是我的代码:



What I have tried:

Here is my code :

protected void Page_Load(object sender, EventArgs e)
    {
        welcome.InnerText = Session["name"].ToString();
        if (!Page.IsPostBack)
        {
            SqlConnection cnn = new SqlConnection("Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("SELECT Database_Name FROM  Create_db", cnn);
            DataTable dt = new DataTable();
            Master.FindControl("TreeView1");
            TreeView tv = (TreeView)Master.FindControl("TreeView1");
            ListBox lv = (ListBox)Master.FindControl("ListBox1");
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cnn.Open();
            da.Fill(dt);
            cnn.Close();

            var parentNode = new TreeNode("Database");
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    string value = row["Database_Name"] + "";
                    parentNode.ChildNodes.Add(new TreeNode(value));
                    lv.Items.Add(value);


                }
            }
            tv.Nodes.Add(parentNode);
        }
    }

推荐答案


这篇关于如何解决未设置为对象实例的对象引用。 Asp.net C#web的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:58