databind()绑定数据时总是报错
<%@ Page Language= "C# " Debug= "true " %>
<%@ Import NameSpace= "System "%>
<%@ Import NameSpace= "System.IO "%>
<%@ Import NameSpace= "System.Data "%>
<%@ Import NameSpace= "System.Collections "%>
<%@ Import NameSpace= "System.ComponentModel "%>
<%@ Import NameSpace= "System.Web.SessionState "%>
<%@ Import NameSpace= "System.Web "%>
<%@ Import NameSpace= "System.Drawing "%>
<%@ Import NameSpace= "System.Web.UI "%>
<%@ Import NameSpace= "System.Web.UI.HtmlControls "%>
<%@ Import NameSpace= "System.Web.UI.WebControls "%>
<%@ Import NameSpace= "System.Data.SqlClient "%>
<script language= "c# " runat= "server ">
void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
BindData();
}
private void BindData()
{
//使用Connection对象连接数据库
SqlConnection thisConnection = new SqlConnection(@ "workstation id=GINAGAO;packet size=4096;integrated security=SSPI;data source= 'GINAGAO\GINAGAO ';persist security info=False;initial catalog=GinaSky ");
//创建访问数据表的DataAdapter对象
string queryStr = "select count(*) from Register ";
SqlDataAdapter adapter = new SqlDataAdapter(queryStr,thisConnection);
DataSet myDataSet=new DataSet();
adapter.Fill(myDataSet, "Register ");
if(myDataSet.Tables[ "Register "].Rows.Count!=0)
{
DataGrid1.DataSource=myDataSet.Tables[ "Register "].DefaultView;
DataGrid1.DataBind();
}
else
{
string strJs1 = " <script> window.open( 'http://www.sina.com.cn ', 'newwindow ', 'height=300,width=250 '); < "+ "/ "+ "script> " ;
Response.Write(strJs1);
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 查看所有已注册信息 </title>
<style type= "text/css ">
<!--
body {
background-color: #000000;
}
.STYLE1 {color: #9999FF}
-->
</style> </head>
<body>
<form ID= "GinaForm " method= "post " runat= "server " >
<table width= "971 " height= "105 " border= "0 ">
<tr>
<td height= "101 " colspan= "2 " background= "../yuqing/15.gif "> </td>
</tr>
</table>
<table width= "971 " height= "72 " border= "0 ">
<tr>
<td width= "971 " height= "68 " colspan= "2 ">
<table width= "953 " border= "0 ">
<tr>
<td height= "78 " colspan= "3 "> <span class= "STYLE1 "> 目前在本论坛注册的用户名单如下: </span> </td>
</tr>
<tr>
<td height= "55 " colspan= "3 ">
<asp:DataGrid id= "DataGrid1 " runat= "server " BorderColor= "#999999 " BorderStyle= "None " BorderWidth= "1px "
BackColor= "White " CellPadding= "3 " GridLines= "Vertical " PageSize= "5 " AllowPaging= "True " Width= "440px ">
<SelectedItemStyle Font-Bold= "True " ForeColor= "White " BackColor= "#008A8C "> </SelectedItemStyle>
<AlternatingItemStyle BackColor= "#DCDCDC "> </AlternatingItemStyle>
<ItemStyle ForeColor= "Black " BackColor= "#EEEEEE "> </ItemStyle>
<HeaderStyle Font-Bold= "True " HorizontalAlign= "Center " ForeColor= "White " BackColor= "#000084 "> </HeaderStyle>
<FooterStyle HorizontalAlign= "Center " ForeColor= "Black " BackColor= "#CCCCCC "> </FooterStyle>
<Columns>
<asp:BoundColumn DataField= "username " HeaderText= "作者 "> </asp:BoundColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
</table>
</td>
</tr>
</table>
<p> </p>
</form>
</body>
</html>
[解决办法]
你的sql 语句有select username出来嘛?
[解决办法]
string queryStr = "select count(*) from Register ";
=======================================================
你这里是 count(*) 将来怎么会有你要的字段呢?