Girdview 绑定后显示不出来数据为什么呢?

来源:百度知道 编辑:UC知道 时间:2024/05/26 14:23:01
public partial class _Default : System.Web.UI.Page
{
String connStr = @"Data Source=E8AAC22FD7514AC\sqlexpress;Initial Catalog=stu;Persist Security Info=True;User ID=sa;Password=123";

protected void Page_Load(object sender, EventArgs e)
{
this.aa();
}
public void aa()
{
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from student";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
con.Open();
DataSet ds = new DataSet();
sda.Fill(ds, "student");
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
http://tieba.baidu.com/f?kz=623047467&

没数据不好试验
con.Close();
拉到最后一行看看

你的con.Open();要放在声明con的后面一行。也就是说放在 cmd.Connection = con;的前面就行了。

还有。GridView1.DataSource=ds.Tables["student"];

首先使用适配器不需要con.Open()
至于没有数据,可以调试一下,首先可以用Sql数据库的事件探查器查一下是否执行了sql。