0
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();
        }
    }


,我写了这段代码,当我运行这段代码时,发生了以下错误:
对象引用未设置为对象的实例。

为什么?

4

2 回答 2

2

总结一下它应该是什么样子

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

或者

    SqlDataSource2.SelectCommand.Parameters.AddWithValue("Parent",Request.QueryString["Type"].ToString());
于 2013-04-23T12:02:35.477 回答
0

尝试这个

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;
            }
        }
    }
于 2013-04-23T12:03:45.723 回答