.net连续刷新报错~!~

charmjs 2008-07-29 12:32:37
错误
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.Data.OleDb.OleDbException: 未指定的错误

Source Error:


Line 71: OleDbConnection myconnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + HttpContext.Current.Server.MapPath(@"~/App_Data/kh1454578s.mdb"));
Line 72: OleDbCommand addCommand = new OleDbCommand(strCount, myconnection);
Line 73: addCommand.Connection.Open();
Line 74: OleDbDataReader dr;
Line 75: dr = addCommand.ExecuteReader(CommandBehavior.CloseConnection);


Source File: d:\2008\yunshengtang.com.cn\Product.aspx.cs Line: 73

Stack Trace:


[OleDbException (0x80004005): 未指定的错误]
System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection) +1054881
System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) +53
System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) +27
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +47
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +105
System.Data.OleDb.OleDbConnection.Open() +37
Product.CalculateRecord() in d:\2008\yunshengtang.com.cn\Product.aspx.cs:73
Product.Page_Load(Object sender, EventArgs e) in d:\2008\yunshengtang.com.cn\Product.aspx.cs:38
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061




我代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class Product : System.Web.UI.Page
{
int CurrentPage;
int PageSize;
int PageCount;
int RecordCount;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

PageSize = 6;
string sql;
if (Request.QueryString["type"] == null)
{
sql = "select * FROM [product]";
}
else
{
sql = "select * FROM [product] where type='" + Request.QueryString["type"] + "'";

}
if (!Page.IsPostBack)
{
//计算总共有多少记录
RecordCount = CalculateRecord();
//计算总共有多少页
//取整
PageCount = RecordCount / PageSize;
if (RecordCount % PageSize > 0)
PageCount = PageCount + 1;
lblPageCount.Text = PageCount.ToString();
lblRecordCount.Text = RecordCount.ToString();
ViewState["PageCount"] = PageCount;
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//绑定
ListBind();
}
}




}
public string strCount;
public int CalculateRecord()
{
int intCount;
int bianlang1 = System.Convert.ToInt32(Request.QueryString["id"]);
if (Request.QueryString["type"] == null)
{
strCount = "select count(*) as co from [product]";
}
else
{
strCount = "select count(*) as co from [product] where type='" + Request.QueryString["type"] + "'";
}
OleDbConnection myconnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + HttpContext.Current.Server.MapPath(@"~/App_Data/kh1454578s.mdb"));
OleDbCommand addCommand = new OleDbCommand(strCount, myconnection);
addCommand.Connection.Open();
OleDbDataReader dr;
dr = addCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
addCommand.Connection.Close();
myconnection.Close();
}
public string strSel;
ICollection CreateSource()
{
int bianlang1 = System.Convert.ToInt32(Request.QueryString["id"]);
int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage * PageSize;
if (Request.QueryString["type"] == null)
{
strSel = "select * from [product]";
}
else
{
strSel = "select * from [product] where type='" + Request.QueryString["type"] + "'";

}
OleDbConnection myconnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + HttpContext.Current.Server.MapPath(@"~/App_Data/kh1454578s.mdb"));
myconnection.Open();
DataSet ds = new DataSet();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel, myconnection);
MyAdapter.Fill(ds, StartIndex, PageSize, "products");
return ds.Tables["products"].DefaultView;
myconnection.Close();
}

public void ListBind()
{
DtProList.DataSource = CreateSource();
DtProList.DataBind();
DtProList.RepeatColumns = 3;
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if (PageCount == 0)
{
lblCurrentPage.Text = "0";
lbnNextPage.Enabled = false;
lbnPrevPage.Enabled = false;
}
else
{
if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;
if (CurrentPage == 0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage + 1).ToString();
}
}
public void Page_OnClick(Object sender, CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch (cmd)
{
case "next":
if (CurrentPage < (PageCount - 1)) CurrentPage++;
break;
case "prev":
if (CurrentPage > 0) CurrentPage--;
break;
case "first":
CurrentPage = 0;
break;
case "last":
CurrentPage = PageCount - 1;
break;
}


ViewState["PageIndex"] = CurrentPage;
ListBind();
}
}
...全文
88 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
charmjs 2008-07-29
  • 打赏
  • 举报
回复
我的好象读关闭了?
哪个没关闭?
大正他爹 2008-07-29
  • 打赏
  • 举报
回复
一些对像打开后,及时关闭就可以了。
Adechen 2008-07-29
  • 打赏
  • 举报
回复
估计是访问的权限不够,多出现在access数据库的连接,你的代码没有大的问题
SeerMi 2008-07-29
  • 打赏
  • 举报
回复
象你这样的情况,连续刷新的话,避免错误,就用事务试试

如果用完链接及时关闭,应该没有这样的问题
SeerMi 2008-07-29
  • 打赏
  • 举报
回复
写程序养成好的习惯,写几个try catch finally 不就有了 ?
ViewStates 2008-07-29
  • 打赏
  • 举报
回复
dr = addCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
addCommand.Connection.Close();
myconnection.Close();

你上面已经写了CommandBehavior.CloseConnection,后面就没有必要在DR.CLOSE后再CLOSE掉CONNECTION了吧?
addCommand.Connection.Close();
myconnection.Close();
这两个是CLOSE的同一个CONNECTION吧?
winner2050 2008-07-29
  • 打赏
  • 举报
回复
myconnection.Open();
DataSet ds = new DataSet();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel, myconnection);
MyAdapter.Fill(ds, StartIndex, PageSize, "products");
return ds.Tables["products"].DefaultView;
myconnection.Close();
============================
不用自己写打开,关闭的.

它自己执行的.
wlj11111 2008-07-29
  • 打赏
  • 举报
回复
有关闭但是没有释放掉资源,dispose();有声明的都要释放掉。

62,025

社区成员

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

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

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

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