0

我正在尝试将 SQL 函数与 SQLDataAdabter 一起使用,但是当我运行我的应用程序时,我得到了这个异常:

你调用的对象是空的

在那一行:

adbtr.SelectCommand.CommandType = CommandType.Text;

当我删除该行时,我得到了相同的异常,但在后面的行中:

adbtr.SelectCommand.CommandText = "SELECT * FROM Select_gallery_names_FN()";

这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{

    DataSet dst = new DataSet();
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
    {
        SqlDataAdapter adbtr = new SqlDataAdapter();
        adbtr.SelectCommand.CommandType = CommandType.Text;
        adbtr.SelectCommand.CommandText = "SELECT * FROM Select_gallery_names_FN()";
        try
        {
            int result = adbtr.Fill(dst);
            if (result == 0)
            {
                return;
            }
            cat_repeater.DataSource = dst;
            cat_repeater.DataBind();
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }

    }

任何帮助,将不胜感激。谢谢。

4

1 回答 1

5

您需要初始化SelectCommand.. 它是null在您设置属性时:

SqlDataAdapter adbtr = new SqlDataAdapter();
adbtr.SelectCommand = new SqlCommand("SELECT * FROM Select_gallery_names_FN()");
adbtr.SelectCommand.CommandType = CommandType.Text;
于 2013-01-29T21:32:49.537 回答