哪位大虾能够帮我看一下程序?

acui9937 2003-07-18 05:16:34
系统指出这个地方有错误:
using System.Web.UI.WebControls.DataGrid MyDataGrid;
说using无效。
为什么?
我的具体程序如下:
namespace yjs.WebUserControl
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// WebUserControl_BlockShown 的摘要说明。
/// </summary>
public class WebUserControl_BlockShown : System.Web.UI.UserControl
{
using System.Web.UI.WebControls.DataGrid MyDataGrid;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection myConnection;
myConnection = new SqlConnection
("server=(local);database=graduate_student;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter
("select distinct * from t_news",myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds,"t_news");

MyDataGrid.DataSource= ds.Tables["t_news"].DefaultView;
MyDataGrid.DataBind();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
...全文
15 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xixigongzhu 2003-07-21
  • 打赏
  • 举报
回复
up
xixigongzhu 2003-07-18
  • 打赏
  • 举报
回复
private void Page_Load(object sender, System.EventArgs e)
{
Response.Write("=======Begin========");
SqlConnection myConnection;
myConnection = new SqlConnection("server=(local);database=graduate_student;user id=sa;password=;");
SqlDataAdapter myCommand = new SqlDataAdapter("select distinct * from t_news",myConnection);
DataSet ds = new DataSet();
Response.Write("======fill data begin=========");
myCommand.Fill(ds,"t_news");
Response.Write("======fill data end=========");
MyDataGrid.DataSource= ds.Tables["t_news"].DefaultView;
Response.Write("======bind data begin=========");
MyDataGrid.DataBind();
Response.Write("======bind data end=========");
}

这样看看,看看执行到那句就没执行了,还是从来就没有执行呢。
acui9937 2003-07-18
  • 打赏
  • 举报
回复
我测试了一下,
他根本就没有执行Response.Write操作。
怎么回事?
acui9937 2003-07-18
  • 打赏
  • 举报
回复
没有纪录。
为什么呢?
数据库里面明明有的
xixigongzhu 2003-07-18
  • 打赏
  • 举报
回复
你在这句"MyDataGrid.DataSource= ds.Tables["t_news"].DefaultView;"前面加上这句先测试是否有记录:
Response.Write(ds.Tables["t_news"].Rows.Count+"===================");
acui9937 2003-07-18
  • 打赏
  • 举报
回复
还是没有值
batisituta 2003-07-18
  • 打赏
  • 举报
回复
using System.Web.UI.WebControls.DataGrid MyDataGrid;
改为System.Web.UI.WebControls.DataGrid MyDataGrid = new DataGrid();
acui9937 2003-07-18
  • 打赏
  • 举报
回复
但是没有显示表里面的东西。
我空间里面是这样编的:
<tr>
<td width="172" height="24" valign="top"><!--DWLayoutEmptyCell-->  
<asp:DataGrid ID="MyDataGrid" Runat="server">
<Columns>
<asp:BoundColumn HeaderText="序号" DataField="news_id"/>
<asp:BoundColumn HeaderText="题目" DataField="news_title"/>
<asp:BoundColumn HeaderText="时间" DataField="news_date"/>
</Columns>
</asp:DataGrid>
</td>
</tr>
原来的查询语句我也修改了:
select distinct news_id,news_title,news_date from t_news

为什么没值呢?
acui9937 2003-07-18
  • 打赏
  • 举报
回复
谢谢!连上了
xixigongzhu 2003-07-18
  • 打赏
  • 举报
回复
你的数据库用的是什么验证?
如果是windows连接语句就是你这样写的,否则Trusted_Connection=yes就改为:
user id=sa;password=;

如果你不知道是什么验证,这样测试一下,打开查询分析器,出现连接对话框,选择本地sql server,选择windows身份验证,如果不能连上(服务必须启动了),改选sql server身份验证,输入用户名和密码(默认的是sa和空密码),如果可以连上那就是这种验证了。
acui9937 2003-07-18
  • 打赏
  • 举报
回复
谢谢!
可是我数据库登陆出错,
问题可能处在哪里呢?
是不是还需要输入密码呀?
或者要对数据库进行帮定?
我的数据库名为:graduate_student
panyee 2003-07-18
  • 打赏
  • 举报
回复
我看了一下ascx,你的代码应该没有错了,


using System.Web.UI.WebControls.DataGrid MyDataGrid;

改成不要using
panyee 2003-07-18
  • 打赏
  • 举报
回复
public class WebUserControl_BlockShown : System.Web.UI.UserControl

你继承的是UserControl

WebControls是继承自System.Web.UI.Page

你好像是用winforms的代码来做webform的事

110,825

社区成员

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

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

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