asp.net读取器关闭时对FieldCount的无效尝试错误 [英] asp.net Invalid attempt to FieldCount when reader is closed error

查看:96
本文介绍了asp.net读取器关闭时对FieldCount的无效尝试错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在阅读器关闭时无效调用FieldCount的尝试

im使用带有c#的asp.net.我试图关闭aspx2.cs上finally语句的连接.

在aspx1.cs中:

  private void BindDataGrids(){尝试{DataGrid1.DataSource = instance.ShowDifferences(Convert.ToInt32(Request.QueryString["CrewId"]),DateTime.Parse(Request.QueryString ["StartDate"]),DateTime.Parse(Request.QueryString ["EndDate"])));DataGrid1.DataBind();}} 

在aspx2.cs中:

 公共静态SqlDataReader ShowDifferences(int crewID,DateTime date1,DateTime date2){数据库db = DatabaseFactory.CreateDatabase();字符串sqlCommand ="stored_procedure";DBCommandWrapper命令= db.GetStoredProcCommandWrapper(sqlCommand);尝试{........代码...........command.AddInParameter("@ EndDate",DbType.Date,date2);IDataReader reader = db.ExecuteReader(command);返回(SqlDataReader)reader;}最后{command.Command.Connection.Close();} 

到达DataGrid1.DataBind();时在aspx1.cs上.

我收到错误消息:

 读取器关闭时,对FieldCount的无效尝试错误" 

如何解决这个问题?

解决方案

您返回的阅读器需要打开连接才能进行 Read ,但是在之前关闭了连接> ShowDifferences 退出.读取器是一种游标,它以增量方式获取数据,并且在整个读取过程中必须将其连接到数据库.如果要使内存数据集( DataTable )与后端数据库断开连接,请考虑使用 SqlDataAdapter Fill 方法.

更新:

要在 DataGrid1.DataBind(); 之后关闭连接",请移动行

  command.Command.Connection.Close(); 

转到 instance 的类的单独方法(例如 CloseConnection )并调用

  instance.CloseConnection(); 

DataGrid1.DataBind(); 之后.当然,为此,您需要将 DBCommandWrapper命令更改为 instance 的类成员.

Possible Duplicate:
Invalid attempt to call FieldCount when reader is closed

im using asp.net with c#. Im atempting to close the connection on the finally statement on aspx2.cs.

In aspx1.cs:

private void BindDataGrids()
{
     try
     {
          DataGrid1.DataSource = instance.ShowDifferences(Convert.ToInt32(Request.QueryString

["CrewId"]), DateTime.Parse(Request.QueryString["StartDate"]), DateTime.Parse(Request.QueryString["EndDate"]));
          DataGrid1.DataBind();
      }
 }

In aspx2.cs:

 public static SqlDataReader ShowDifferences(int crewID, DateTime date1, DateTime date2)
 {
      Database db = DatabaseFactory.CreateDatabase();
      string sqlCommand = "stored_procedure";
      DBCommandWrapper command = db.GetStoredProcCommandWrapper(sqlCommand);
      try
      {
........code...........
         command.AddInParameter("@EndDate", DbType.Date, date2);
                IDataReader reader = db.ExecuteReader(command);
         return (SqlDataReader)reader;
      }
      finally
      {
          command.Command.Connection.Close();
      }

When it reaches DataGrid1.DataBind(); on aspx1.cs.

i get error:

"Invalid attempt to FieldCount when reader is closed error"

How to solve this?

解决方案

You are returning a reader that needs the connection to be open to be able to Read, but you close the connection before ShowDifferences exits. The reader is cursor that fetches data incrementally and has to be connected to the database for the whole process of reading. If you want to have an in-memory dataset (DataTable) disconnected from back-end database, consider using Fill method of SqlDataAdapter.

Update:

To "close the connection after the DataGrid1.DataBind();" move line

command.Command.Connection.Close();

to a separate method of instance's class (e.g. CloseConnection) and call

instance.CloseConnection();

after DataGrid1.DataBind();. For this, of course, you need to change DBCommandWrapper command into instance's class member.

这篇关于asp.net读取器关闭时对FieldCount的无效尝试错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆