databind()绑定数据时总是报错

GinaGao 2007-04-20 06:28:00
<%@ 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>
...全文
354 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
GinaGao 2007-04-22
  • 打赏
  • 举报
回复
哦....对也,谢谢哦.
amandag 2007-04-21
  • 打赏
  • 举报
回复
string queryStr = "select count(*) from Register";
=======================================================

你这里是 count(*) 将来怎么会有你要的字段呢?
风上人 2007-04-21
  • 打赏
  • 举报
回复
代码没问题你看一下 有些单词的大小些是不一样
GinaGao 2007-04-21
  • 打赏
  • 举报
回复
对啊,可是还是报错,说绑定的数据源不存在userdata字段,5555.没人理我
孟子E章 2007-04-20
  • 打赏
  • 举报
回复
string queryStr = "select * from Register";
孟子E章 2007-04-20
  • 打赏
  • 举报
回复
你的sql 语句有select username出来嘛?
孟子E章 2007-04-20
  • 打赏
  • 举报
回复
报什么错误
GinaGao 2007-04-20
  • 打赏
  • 举报
回复
报错说是数据源中不存在userdata这个字段.
而事实上
<Columns>
<asp:BoundColumn DataField="username" HeaderText="作者"></asp:BoundColumn>
</Columns>
用来添加要显示的字段,我绑定的表Register中也的却存在username这个字段.
在这个位置报错:
DataGrid1.DataSource=myDataSet.Tables["Register"].DefaultView;
DataGrid1.DataBind();

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧