第2部分开始显示表格感谢Wayne对第1部分的回答 [英] Part2 Getting to display the table thanks to Wayne for the answer to part1
问题描述
特别感谢
现在,我要做的就是显示数据库中的表,这就是我尝试过的方法:
Special thanks to
Now what I want to do is display the table in the database, this is what I have tried:
int DisplayTable()
{
using (SqlConnection myConnection2 = new SqlConnection(DBConn))
{
SqlCommand MyCommand2 = new SqlCommand("SELECT * from TheTableName;", myConnection2);
myConnection2.Open();
SqlDataReader reader = MyCommand2.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
return MyCommand2.ExecuteNonQuery();
}
}
然后像以前一样放入按钮:
Then put in the button like I did before:
protected void btnDisplay_Click(object sender, EventArgs e)
{
DisplayTable();
}
but this does not work and gives the error:
"Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition."
What should I do?
特别感谢韦恩·盖拉德(Wayne Gaylard)对我的第一个问题的回答,如果您仍然在那里,请提供帮助.
在此先感谢...
Special thanks to Wayne Gaylard for the answer to my first question, if you are still out there please help.
Thanks in advance...
推荐答案
答案在错误中:
"DataSource和DataSourceID都在GridView1上定义.删除一个定义."
删除一个定义.猜测一下,检查您的Gridview定义,并取出DataSourceID分配,因为您在代码中设置了DataSource.
The answer is in the error:
"Both DataSource and DataSourceID are defined on ''GridView1''. Remove one definition."
Remove one definition. At a guess, check your Gridview definition, and take out the DataSourceID assignment, since you are setting the DataSource in the code.
.aspx页面上的这一行自动放置在此处"DataSourceID ="SqlDataSource1"这是我删除的内容.
然后我得到一个关于仍然打开的阅读器的错误...
我的工作方式如下:
This line on my .aspx page was put there automatically "DataSourceID="SqlDataSource1" Which is what I deleted.
Then I got an error about the reader that was still opened...
I got it work as follows:
int DisplayTable()
{
using (SqlConnection myConnection2 = new SqlConnection(DBConn))
{
SqlCommand MyCommand2 = new SqlCommand("SELECT * from TheTableName;", myConnection2);
myConnection2.Open();
SqlDataReader reader = MyCommand2.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
reader.Close();
return MyCommand2.ExecuteNonQuery();
}
}
作为一个刚开始编程的学生,我想感谢所有给我这么好的答案的人,这些答案适用于我的问题,只要有一种方法可以让他们知道我有多高兴.
As a student who has only just started programming, I would like to thank everyone who has given me such good answers that apply to my questions, if only there was a way I could let them know how gratefull I am.
这篇关于第2部分开始显示表格感谢Wayne对第1部分的回答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!