c#.net奇怪问题。大虾们帮帮忙
为什么我运行程序时会出现这个错误?其他机子正常。而且对数据库进行写或更改操作时提示dataset以打开,需要关闭???请大家帮忙!
错误代码
There is already an open DataReader associated with this Connection which must be closed first.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.
Source Error:
The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:
1. Add a "Debug=true" directive at the top of the file that generated the error. Example:
<%@ Page Language="C#" Debug="true" %>
or:
2) Add the following section to the configuration file of your application:
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>
Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.
Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.
Stack Trace:
[InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.]
System.Data.OleDb.OleDbConnection.SetStateExecuting(OleDbCommand attempt, String method, Boolean flag) +103
System.Data.OleDb.OleDbCommand.ValidateConnectionAndTransaction(String method, Int32& localState) +103
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +61
System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) +69
System.Data.OleDb.OleDbCommand.ExecuteReader() +7
ASP.index_aspx.Page_Load(Object Src, EventArgs E) +170
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731
问题点数:20、回复次数:4Top
1 楼birdxxxx(爱上老鼠的猫)回复于 2005-08-03 23:10:07 得分 0
错误提示为你使用的时候Connection对象已经关联了一个DataReader,即有一个连接的Reader没有被关闭。你查一下你的代码吧Top
2 楼gavinluo()回复于 2005-08-03 23:13:52 得分 0
可是在其他机子上就没事,代码如下:
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
strConnection+="Data Source=";
strConnection+=MapPath("data\\data.mdb");
OleDbConnection objConnection=new OleDbConnection(strConnection);
objConnection.Open();
for(int i=0;i<=2;i++)
{
int tp=i+1;
// OleDbCommand objCommand = new OleDbCommand("select *,wen len(title)>10 then(substring(title,0,10)+'...') else title end as title from '文字' where 位置='index"+ tp +"'" , objConnection);
OleDbCommand objCommand = new OleDbCommand("select * from 文字 where 位置='index"+ tp +"'" , objConnection);
OleDbDataReader objDataReader=objCommand.ExecuteReader();
if(objDataReader.Read())
{
string lcID="lable"+i*3+1;
Control loCtrl=this.FindControl(lcID);
if (loCtrl!=null&&loCtrl is Label)
{
//string myString = Convert.ToString(objDataReader["标题"]);
((Label)loCtrl).Text=Convert.ToString(objDataReader["标题"]);
}
lcID="lable"+i*3+2;
loCtrl=this.FindControl(lcID);
if (loCtrl!=null&&loCtrl is Label)
{
// string myString = Convert.ToString(objDataReader["内容"]);
((Label)loCtrl).Text=Convert.ToString(objDataReader["内容"]);
}
}
}
for(int i=1;i<=3;i++)
{
OleDbCommand objCommand = new OleDbCommand("select * from 图片 where 位置='index"+ i +"'" , objConnection);
OleDbDataReader objDataReader=objCommand.ExecuteReader();
if(objDataReader.Read())
{
string lcID="image"+i;
Control loCtrl=this.FindControl(lcID);
if (loCtrl!=null&&loCtrl is Label)
{
((Image)loCtrl).ImageUrl=Convert.ToString(objDataReader["地址"]);
}
}
}
objConnection.Close();
}Top
3 楼birdxxxx(爱上老鼠的猫)回复于 2005-08-04 00:12:36 得分 20
你创建了两次objDataReader对象,你最好把它们提到For循环的外面,只创建一次,然后使用。Top
4 楼jack1026(杭杭)回复于 2005-08-04 00:39:52 得分 0
你objDataReader对象创建了多少次啊.只要在循环的最外面一次就够了啊.Top




