我正在尝试将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);
}
}
任何帮助将不胜感激。感谢。
答案 0 :(得分:5)
您需要在设置属性时初始化SelectCommand
.. null
:
SqlDataAdapter adbtr = new SqlDataAdapter();
adbtr.SelectCommand = new SqlCommand("SELECT * FROM Select_gallery_names_FN()");
adbtr.SelectCommand.CommandType = CommandType.Text;