分页的时候,删除把某个分页上的数据全部删除的时候就出现错误,该如何是好?

sylmoon 2005-11-30 10:59:47
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.Web.HttpException: Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.

Source Error:


Line 153: view.Sort="Gid ASC";
Line 154: DataGrid1.DataSource=view;
Line 155: DataGrid1.DataBind();
Line 156: conn.Close();
Line 157:


Source File: d:\dotnet\test\wms_new\googsmanage.aspx.cs Line: 155

代码如果想看在下面:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration ;

namespace WMS_NEW
{
/// <summary>
/// Summary description for googsManage.
/// </summary>
public class googsManage : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected WMS_NEW.DataSetQueryGoods dataSetQueryGoods1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button Btn_Search;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.TextBox TextBox1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
//this.sqlDataAdapter1.Fill(this.dataSetQueryGoods1);
BindGrid();

}
protected void OnNewPage(object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
BindGrid();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.dataSetQueryGoods1 = new WMS_NEW.DataSetQueryGoods();
((System.ComponentModel.ISupportInitialize)(this.dataSetQueryGoods1)).BeginInit();
this.Btn_Search.Click += new System.EventHandler(this.Btn_Search_Click);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Goods", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("GProvider1", "GProvider1"),
new System.Data.Common.DataColumnMapping("GProvider2", "GProvider2"),
new System.Data.Common.DataColumnMapping("GPrice", "GPrice"),
new System.Data.Common.DataColumnMapping("GName", "GName")})});
this.sqlDataAdapter1.RowUpdated += new System.Data.SqlClient.SqlRowUpdatedEventHandler(this.sqlDataAdapter1_RowUpdated);
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT GID, GName, GPrice, GProvider1, GProvider2 FROM Goods";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=AARON;packet size=4096;integrated security=SSPI;data source=AARON;" +
"persist security info=False;initial catalog=WMS";
//
// dataSetQueryGoods1
//
this.dataSetQueryGoods1.DataSetName = "DataSetQueryGoods";
this.dataSetQueryGoods1.Locale = new System.Globalization.CultureInfo("zh-TW");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSetQueryGoods1)).EndInit();

}
#endregion

private void Btn_Search_Click(object sender, System.EventArgs e)
{
try
{
int varGid=Convert.ToInt32(TextBox1.Text.ToString());
Response.Redirect("goodsmodify.aspx?varGid="+varGid);

}
catch
{
Response.Write("沒有輸入或輸入錯誤!");
Response.End();

}


}
public void DataGrid_Delete (object sender, DataGridCommandEventArgs e)
{

//step1
string strconn=ConfigurationSettings.AppSettings["dsn"];//ConfigurationSettings.AppSettings["dsn"];

SqlConnection conn=new SqlConnection(strconn);

//step2
SqlCommand cmd=new SqlCommand("goodsdelete",conn);

//step3: define cmd type:
cmd.CommandType =CommandType.StoredProcedure ;

//step4: define the parameter
cmd.Parameters .Add ("@GID",SqlDbType.Int );

cmd.Parameters ["@GID"].Value =Convert.ToInt32 (e.Item.Cells [1].Text.ToString ());
conn.Open();
//step5:execute
cmd.ExecuteNonQuery ();
DataGrid1.EditItemIndex =-1;
//finally :bind grid

BindGrid();



}
public void BindGrid()
{
string strconn=ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn=new SqlConnection(strconn);

SqlDataAdapter adapter=new SqlDataAdapter("select * from goods ", conn);
DataSet ds=new DataSet();
adapter.Fill(ds);
DataView view=new DataView(ds.Tables[0]);
view.Sort="Gid ASC";
DataGrid1.DataSource=view;
DataGrid1.DataBind();
conn.Close();

}
private void sqlDataAdapter1_RowUpdated(object sender, System.Data.SqlClient.SqlRowUpdatedEventArgs e)
{

}

private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{

}


}
}

大家都是如何解决此类问题的?
...全文
161 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
y4339528 2005-12-01
  • 打赏
  • 举报
回复
up
sylmoon 2005-12-01
  • 打赏
  • 举报
回复
up
sunnystar365 2005-11-30
  • 打赏
  • 举报
回复
删除之后,判断currentpageindex是不是还存在
或者你删除之后可以把currentpageindex设置为0或者1
sylmoon 2005-11-30
  • 打赏
  • 举报
回复
是啊,那要判断是否进行了整页删除?
zmj122112 2005-11-30
  • 打赏
  • 举报
回复
是你的当前页取不到值了
比较方便的方法是你删除整页时,把currentpageindex指向上一页

62,046

社区成员

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

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

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

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