Please how any I resolve this issue?
I am trying to modify my datalist view to have paging , I used the below code which I found on c-sharpconer, but I get error message when I click on the previous buttons, all other buttons work work.
The error message says:
Index -5 is either negative or above rows count.
this is the aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="PagininDataList.WebForm1" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- "http://www.w3.org/1999/xhtml">
- "server">
- "form1" runat="server">
-
-
"height: 79px; width: 429px"
> - "DataList1" runat="server">
- Details
-
- My Custom Datalist Template here
-
-
-
- "btnfirst" runat="server" Font-Bold="true" Text="<<" Height="31px"
- Width="43px" onclick="btnfirst_Click" />
-
- "btnprevious" runat="server" Font-Bold="true" Text="<" Height="31px"
- Width="43px" onclick="btnprevious_Click" />
-
- "btnnext" runat="server" Font-Bold="true" Text=">" Height="31px"
- Width="43px" onclick="btnnext_Click" />
-
- "btnlast" runat="server" Font-Bold="true" Text=">>" Height="31px"
- Width="43px" onclick="btnlast_Click" />
- namespace PagininDataList
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- SqlDataAdapter dadapter;
- DataSet dset;
- PagedDataSource adsource = new PagedDataSource();
- string connstring = "My connection string";
- int pos;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- this.ViewState["vs"] = 0;
- }
- pos = (int)this.ViewState["vs"];
- databind();
- }
- public void databind()
- {
- dadapter = new SqlDataAdapter("my SELECT query", connstring);
- dset = new DataSet();
- dadapter.Fill(dset);
- adsource.DataSource = dset.Tables[0].DefaultView;
- adsource.PageSize = 3;
- adsource.AllowPaging=true;
- adsource.CurrentPageIndex = pos;
- btnfirst.Enabled = !adsource.IsFirstPage;
- btnprevious.Enabled = !adsource.IsFirstPage;
- btnlast.Enabled = !adsource.IsLastPage;
- btnnext.Enabled = !adsource.IsLastPage;
- DataList1.DataSource = adsource;
- DataList1.DataBind();
- }
- protected void btnfirst_Click(object sender, EventArgs e)
- {
- pos = 0;
- this.ViewState["vs"] = 0;
- databind();
- }
- protected void btnprevious_Click(object sender, EventArgs e)
- {
- pos = (int)this.ViewState["vs"];
- pos -= 1;
- this.ViewState["vs"] = pos;
- databind();
- }
- protected void btnnext_Click(object sender, EventArgs e)
- {
- pos = (int)this.ViewState["vs"];
- pos += 1;
- this.ViewState["vs"] = pos;
- databind();
- }
- protected void btnlast_Click(object sender, EventArgs e)
- {
- pos = adsource.PageCount - 1;
- this.ViewState["vs"] = pos;
- databind();
- }
- }
- }
Answers
Know the answer? Post it. Somebody with the same question will find it here.