asp.net
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 留言板: System.Web.UI.Page
{
OleDbConnection conn = default(OleDbConnection);
int PageSize = 0;
int StartPosition = 0;
int CurrentPage = 0;
int TotalPage = 0;
public void Page_Load(object Sender, EventArgs E)
{
string conn = "Data Source=d:\\guest.mdb;Provider=Microsoft.Jet.OLEDB.4.0";
OleDbConnection myconn = new OleDbConnection(conn);
string connstring = System.Configuration.ConfigurationManager.AppSettings["connStr"];
if (!Page.IsPostBack)
{
BindData();
}
}
public void BindData()
{
TotalPage = Convert.ToInt32(GetTotalPage());
if (!string.IsNullOrEmpty(Request.QueryString["commandname"]))
{
switch (Request.QueryString["commandname"]) {
case "first":
CurrentPage = 1;
break;
case "prev":
CurrentPage = Convert.ToInt32(Session["CurrentPage"]) - 1;
if (CurrentPage < 1)
CurrentPage = 1;
break;
case "next":
CurrentPage = Convert.ToInt32(Session["CurrentPage"]) + 1;
if (CurrentPage > TotalPage)
CurrentPage = TotalPage;
break;
case "last":
CurrentPage = TotalPage;
break;
}
Session["CurrentPage"] = CurrentPage;
} else if ((Session["CurrentPage"] != null)) {
CurrentPage = Convert.ToInt32(Session["CurrentPage"]);
} else {
CurrentPage = 1;
Session["CurrentPage"] = CurrentPage;
}
OleDbDataAdapter adp = new OleDbDataAdapter("select * from guestorder by submit_date Desc", conn);
DataSet ds = new DataSet();
StartPosition = (CurrentPage - 1) * PageSize;
adp.Fill(ds, StartPosition, PageSize, "guest");
MyRepeater.DataSource = ds.Tables["guest"].DefaultView;
MyRepeater.DataBind();
theCurrentPage.Text = CurrentPage.ToString();
theTotalPage.Text = TotalPage.ToString();
}
public object GetTotalPage()
{
object functionReturnValue = null;
int TotalRecord = 0;
string db = Server.MapPath("guest.mdb");
string ConnectionStr = "Data Source=" + db + ";Provider=Microsoft.Jet.OLEDB.4.0";
OleDbConnection myConn = new OleDbConnection(ConnectionStr);
myConn.Open();
string strsql = "select count(*) as total From guest";
OleDbCommand cmd = new OleDbCommand(strsql, myConn);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read()) {
// intCount = Int32.Parse(dr.Item("total").ToString)
TotalRecord = Convert.ToInt32(dr["total"]);
} else {
TotalRecord = 0;
}
myConn.Close();
if ((TotalRecord % PageSize) == 0)
{
functionReturnValue = TotalRecord / PageSize;
}
else
{
functionReturnValue = TotalRecord / PageSize + 1;
}
return functionReturnValue;
}
public void tijiao_Click(object Sender, EventArgs E)
{
OleDbConnection conn = new OleDbConnection
("Data Source=d:\\guest.mdb;Provider=Microsoft.Jet.OLEDB.4.0");
string strA = null;
string strB = null;
string strSql = null;
strA = "Insert Into guest(title,name,submit_date";
strB = " Values('" + title.Text + "','" + name.Text + "',#" + DateTime.Now + "#";
if (!string.IsNullOrEmpty(body.Text)) {
strA = strA + ",body";
strB = strB + ",'" + (body.Text) + "'";
}
strSql = strA + ")" + strB + ")";
OleDbCommand cmd = new OleDbCommand(strSql, conn);
Exception Exp = null;
try {
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
BindData();
} catch {
BindData();
}
title.Text = "";
body.Text = "";
name.Text = "";
email.Text = "";
}
}
异常详细信息: System.DivideByZeroException: 试图除以零。
源错误:
行 127: myConn.Close();
行 128:
行 129: if ((TotalRecord % PageSize) == 0)
行 130: {
行 131:
源文件: d:\asp1\留言板.aspx.cs 行: 129
堆栈跟踪:
[DivideByZeroException: 试图除以零。]
留言板.GetTotalPage() in d:\asp1\留言板.aspx.cs:129
留言板.BindData() in d:\asp1\留言板.aspx.cs:48
留言板.Page_Load(Object Sender, EventArgs E) in d:\asp1\留言板.aspx.cs:39
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +13
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +45
System.Web.UI.Control.OnLoad(EventArgs e) +80
System.Web.UI.Control.LoadRecursive() +49
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3743
请大家帮帮忙,谢谢。 --------------------编程问答-------------------- PageSize 不能为0
你设置一下不为0就行了 --------------------编程问答-------------------- 分页看看aqspnetpager分页控件
if(PageSize>0)
{
} --------------------编程问答-------------------- OleDbConnection conn = default(OleDbConnection);
int PageSize = 0;
先PageSize=20看看 --------------------编程问答-------------------- PageSize 值错误。。
加个默认值啊。 --------------------编程问答-------------------- 一致认为 PageSize = 0 --------------------编程问答--------------------
补充:.NET技术 , ASP.NET