错误“对象引用未设置为对象的实例”。

时间:2022-01-28 16:55:14
protected void Page_Load(object sender, EventArgs e)
    {
        string query = "SELECT Posts.*, Import_Export.* FROM Import_Export INNER JOIN Posts ON Import_Export.CatID = Posts.CatID where Import_Export.Parent=@Parent ";
        if (Request.QueryString["CID"] != null)
        {
            query += " and Import_Export.CatID=@CatID";
            SqlDataSource2.SelectCommand = query;
            SqlDataSource2.SelectParameters["Parent"].DefaultValue = Request.QueryString["Type"].ToString();
            SqlDataSource2.SelectParameters["CatID"].DefaultValue = Request.QueryString["CID"].ToString();
        }
        else
        {
            SqlDataSource2.SelectCommand = query;
            SqlDataSource2.SelectParameters["Parent"].DefaultValue = Request.QueryString["Type"].ToString();
        }
    }

hi
I write this code and when I run this code below error was occurred:
Object reference not set to an instance of an object.

嗨,我编写此代码,当我运行此代码时,发生错误:对象引用未设置为对象的实例。

Why?

为什么?

2 个解决方案

#1


2  

To sum up here's what it should look like

总结这里应该是什么样子

SqlDataSource2.SelectCommand.Parameters.Add("Parent", System.Data.SqlDbType.NVarChar);
SqlDataSource2.SelectCommand.Parameters["Parent"].Value =   Request.QueryString["Type"].ToString();

OR

要么

    SqlDataSource2.SelectCommand.Parameters.AddWithValue("Parent",Request.QueryString["Type"].ToString());

#2


0  

Try this

尝试这个

protected void Page_Load(object sender, EventArgs e)
    {
        string query = "SELECT Posts.*, Import_Export.* FROM Import_Export INNER JOIN Posts ON Import_Export.CatID = Posts.CatID where Import_Export.Parent=@Parent ";
        if (Request.QueryString != null)
        {
            Parameter par = new Parameter("@Parent", DbType.String, Request.QueryString["Type"].ToString());
            SqlDataSource2.SelectParameters.Add(par);


            if (Request.QueryString["CID"] != null)
            {
                query += " and Import_Export.CatID=@CatID";
                SqlDataSource2.SelectCommand = query;
                Parameter par2 = new Parameter("@CatID", DbType.String, Request.QueryString["CID"].ToString());
                SqlDataSource2.SelectParameters.Add(par2);
            }
            else
            {
                SqlDataSource2.SelectCommand = query;
            }
        }
    }

#1


2  

To sum up here's what it should look like

总结这里应该是什么样子

SqlDataSource2.SelectCommand.Parameters.Add("Parent", System.Data.SqlDbType.NVarChar);
SqlDataSource2.SelectCommand.Parameters["Parent"].Value =   Request.QueryString["Type"].ToString();

OR

要么

    SqlDataSource2.SelectCommand.Parameters.AddWithValue("Parent",Request.QueryString["Type"].ToString());

#2


0  

Try this

尝试这个

protected void Page_Load(object sender, EventArgs e)
    {
        string query = "SELECT Posts.*, Import_Export.* FROM Import_Export INNER JOIN Posts ON Import_Export.CatID = Posts.CatID where Import_Export.Parent=@Parent ";
        if (Request.QueryString != null)
        {
            Parameter par = new Parameter("@Parent", DbType.String, Request.QueryString["Type"].ToString());
            SqlDataSource2.SelectParameters.Add(par);


            if (Request.QueryString["CID"] != null)
            {
                query += " and Import_Export.CatID=@CatID";
                SqlDataSource2.SelectCommand = query;
                Parameter par2 = new Parameter("@CatID", DbType.String, Request.QueryString["CID"].ToString());
                SqlDataSource2.SelectParameters.Add(par2);
            }
            else
            {
                SqlDataSource2.SelectCommand = query;
            }
        }
    }