“在调用'Fill'之前,尚未初始化SelectCommand属性。 [英] "The SelectCommand property has not been initialized before calling 'Fill'.
本文介绍了“在调用'Fill'之前,尚未初始化SelectCommand属性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码是....
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data;
使用 System.Data.SqlClient;
public partial class _Default :System.Web.UI.Page
{
protected void Page_Load(< span class =code-keyword> object sender,EventArgs e)
{
}
protected void Button1_Click( object sender,EventArgs e)
{
SqlConnection cn = new SqlConnection( 数据源=。\\\ \\ _SQLEXPRESS; AttachDbFilename = C:\\Users\\MANTHAN \\Documents \\ Visual Studio 2010 \\WebSites\\sample\\App_Data \\Database。 mdf; Integrated Security = True; User Instance = True);
cn.Open();
SqlCommand cmd = new SqlCommand( SELECT * FROM stu1,cn);
SqlDataAdapter adp = new SqlDataAdapter();
DataSet ds = new DataSet();
adp.Fill(ds);
cn.Close();
GridView1.DataSource = ds.Tables [ 0 ];
}
}
解决方案
试试这个。
SqlDataAdapter adp = new SqlDataAdapter( cmd );
my code is....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\MANTHAN\\Documents\\Visual Studio 2010\\WebSites\\sample\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True");
cn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM stu1", cn);
SqlDataAdapter adp = new SqlDataAdapter();
DataSet ds = new DataSet();
adp.Fill(ds);
cn.Close();
GridView1.DataSource = ds.Tables[0];
}
}
解决方案
Try this.
SqlDataAdapter adp = new SqlDataAdapter(cmd);
这篇关于“在调用'Fill'之前,尚未初始化SelectCommand属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文