急,使用DataGrid分页时出现错误...在线等...

i169net 2003-11-07 04:00:34
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
//定义一个全局变量mycx,存储DQL查询命令
string mycx;
void Page_Load(object sender, System.EventArgs e)
{
if(this.IsPostBack)
return;
}

//DataGrid的自动分页
void DataGrid1_PageIndexChanged(object sender,System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.myt.CurrentPageIndex=e.NewPageIndex;
this.bind(mycx);
}

//绑定数据到DataGrid
void bind(string mycx)
{
this.myt.DataSource=this.getDataSource(mycx);
this.myt.Columns[10].ItemStyle.Width=Unit.Parse("150");
this.myt.DataBind();

}

//获取数据源
DataTable getDataSource(string mycx)
{
System.Data.SqlClient.SqlConnection conn;
conn=new System.Data.SqlClient.SqlConnection(this.getConnectionString());
conn.Open();
System.Data.SqlClient.SqlDataAdapter sqlDataAdaper=new System.Data.SqlClient.SqlDataAdapter(mycx,conn);
System.Data.DataSet dset=new DataSet();
sqlDataAdaper.Fill(dset,"sc");

conn.Close();
return dset.Tables["sc"];
}

//SQL数据表打开命令字符串
string getConnectionString()
{
string strcon="server=server;database=sanfei;uid=sa;pwd=123456;";
strcon+="Connect Timeout=30";
return strcon;
}

//查询按钮的单击事件
void Button1_Click(object sender, EventArgs e) {
//处理从文本框、下拉菜单中获得的数据
string ye1=Request.Form["TextBox2"]+"/"+Request.Form["TextBox3"]+"/"+Request.Form["TextBox1"];
string ye2=Request.Form["TextBox5"]+"/"+Request.Form["TextBox6"]+"/"+Request.Form["TextBox4"];
string bancc=Request.Form["DropDownList2"];
string banzc=Request.Form["DropDownList1"];
string banc="";
string banz="";
switch (bancc)
{
case "全部":
banc="";
break;
case "白班":
banc="and name='白班'";
break;
case "夜班":
banc="and name='夜班'";
break;
}
switch (banzc)
{
case "全部":
banz="";
break;
case "朱小燕":
banz="and bz='朱小燕'";
break;
case "刘瑞良":
banz="and bz='刘瑞良'";
break;
case "陈少华":
banz="and bz='陈少华'";
break;
}
//得到SQL查询命令,写入变量mycx中
mycx="Select mdate,name,tl,zsl,ym,ztime,ttime,twhy,stime,gtime,bz from shaocheng where mdate>='";
mycx+=ye1+"' and mdate<='"+ye2+"' "+banc+" "+banz;
//运行数据绑定
this.bind(mycx);
}

</script>

<html>
-------------------略-----------------------------
查询条件输入的文本框、下拉菜单等控件...提交查询按钮
DataGrid 控件
-------------------略-----------------------------
</html>

==================================================
运行后DataGrid显示查询结果,‘第一页’时正常,点击链接进入‘下一页’时错误:
ExecuteReader:CommandText 属性尚未初始化
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: ExecuteReader:CommandText 属性尚未初始化

源错误:


行 53: System.Data.SqlClient.SqlDataAdapter sqlDataAdaper=new System.Data.SqlClient.SqlDataAdapter(mycx,conn);
行 54: System.Data.DataSet dset=new DataSet();
行 55: sqlDataAdaper.Fill(dset,"sc");
行 56:
行 57: conn.Close();


源文件: F:\sanfei\fshaocheng.aspx 行: 55

堆栈跟踪:


[InvalidOperationException: ExecuteReader:CommandText 属性尚未初始化]
System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean executing) +333
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +103
System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +9
System.Data.Common.DbDataAdapter.Fill(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +121
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +77
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +36
ASP.fshaocheng_aspx.getDataSource(String mycx) in F:\sanfei\fshaocheng.aspx:55
ASP.fshaocheng_aspx.bind(String mycx) in F:\sanfei\fshaocheng.aspx:42
ASP.fshaocheng_aspx.DataGrid1_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e) in F:\sanfei\fshaocheng.aspx:37
System.Web.UI.WebControls.DataGrid.OnPageIndexChanged(DataGridPageChangedEventArgs e) +110
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +349
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +100
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +120
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1263


...全文
19 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
i169net 2003-11-08
  • 打赏
  • 举报
回复
我想试试cpp2017(幕白兄) 建议的:ViewState 来保存变量~

但不知道如何定义,及提取...
是否象Session那样,要在global.asax文件中定义,具体代码怎么写呢?

谢谢!
dongbeiren 2003-11-08
  • 打赏
  • 举报
回复
public void Page_Load(Object sender, EventArgs e)
{
// Initialize only the first time...
if (!Page.IsPostBack)
{
SetVirtualItemCount();
}
}

SqlDataReader dr;
private SqlDataReader CreateDataSource(int nPageIndex)
{
// page index is assumed to be 0-based
int nPageSize = grid.PageSize;
int nBaseProductID = nPageSize * nPageIndex;


// Set up the connection
String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;";
SqlConnection conn = new SqlConnection(strConn);

// Set up the command
String strCmd = "SELECT TOP " + nPageSize + " " +
"productid, productname, quantityperunit, unitsinstock " +
"FROM products " +
"WHERE productid >" + nBaseProductID;
SqlCommand cmd = new SqlCommand(strCmd, conn);

// Execute the command
conn.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}


public void PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
{
grid.CurrentPageIndex = e.NewPageIndex;
grid.DataSource = CreateDataSource(grid.CurrentPageIndex);
grid.DataBind();
dr.Close();
}

public void OnLoadData(Object sender, EventArgs e)
{
grid.DataSource = CreateDataSource(0);
grid.DataBind();
dr.Close();
}

public void checkdatagrid(Object sender, EventArgs e)
{
selectdatagrid();
}

public void selectdatagrid()
{

for each(datagriditem dgi in grid.selecteditems)
{
checkbox cb=(checkbox)dgi.cells[0].controls[0];
cb.check=true;
}

}
public void SetVirtualItemCount()
{
// Set up the connection
String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;";
SqlConnection conn = new SqlConnection(strConn);

// Set up the command
String strCmd = "SELECT COUNT(*) FROM products";
SqlCommand cmd = new SqlCommand(strCmd, conn);

// Execute the command
conn.Open();
int nItemCount = (int) cmd.ExecuteScalar();
conn.Close();

grid.VirtualItemCount = nItemCount;
return;
}
realsnow 2003-11-08
  • 打赏
  • 举报
回复
感觉datagrid的自定义分页不要用,最好使用默认的分页,如果是真的想自己做分页最好是从数据绑定的地方自己写代码来完成分页的功能。
i169net 2003-11-08
  • 打赏
  • 举报
回复
还是没有解决...提~~~~~~~~~~

xydyh 2003-11-07
  • 打赏
  • 举报
回复
datagrid自定义分页功能适用于dataset,对于datareader好象并不支持!
要用datareader,必须自定义分页事件!
i169net 2003-11-07
  • 打赏
  • 举报
回复
“可以把mycx的值保存在ViewState中”

不好意思,我是新手,ViewState是什么东西?
具体的代码应该怎么写呢?

谢谢!
cpp2017 2003-11-07
  • 打赏
  • 举报
回复
可以把mycx的值保存在ViewState中
cpp2017 2003-11-07
  • 打赏
  • 举报
回复
你在button click时给了mycs赋值,但没有保存到 PageIndexChaged时mycx是null
i169net 2003-11-07
  • 打赏
  • 举报
回复
提示错误是 ExecuteReader:CommandText 属性尚未初始化 !
并且初次运行,显示DataGrid第一页的时候都一切正常,只是显示下页时有错误...

我认为是,显示第二页,页面重载时,变量mycx丢失?或者函数DataTable getDataSource(string mycx)有什么问题?

................

62,073

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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