Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
Quote:
Index -5 is either negative or above rows count.


This is my 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">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 672px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
   <div>
    <table style="height: 79px; width: 429px">
        <asp:DataList ID="DataList1" runat="server">
        <HeaderTemplate>
        <h1> Details </h1>
        </HeaderTemplate>
      <ItemTemplate>
        <tr>

        My Custom Datalist Template here
        </tr>

        </ItemTemplate>
        </asp:DataList>
       </table>
   <table>
  <tr>
    <td>
        <asp:Button ID="btnfirst" runat="server" Font-Bold="true" Text="<<" Height="31px"
                    Width="43px" onclick="btnfirst_Click" /></td>
        <td>
            <asp:Button ID="btnprevious" runat="server" Font-Bold="true" Text="<" Height="31px"
                    Width="43px" onclick="btnprevious_Click" /></td>
            <td>
                <asp:Button ID="btnnext" runat="server" Font-Bold="true" Text=">" Height="31px"
                    Width="43px" onclick="btnnext_Click" /></td>
                <td>
                    <asp:Button ID="btnlast" runat="server" Font-Bold="true" Text=">>" Height="31px"
                    Width="43px" onclick="btnlast_Click" /></td>
    </tr>
   </table>
    </div>
    </form>
</body>
</html>


this is my C# code

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(); 
        }
    }
}


What I have tried:

I try changing the page size, but it still give the error.
Posted
Updated 22-Sep-17 23:35pm
Comments
Richard MacCutchan 23-Sep-17 6:24am    
Well -5 is definitely negative, so you need to find out how it got that value, and fix your code.

1 solution

Well ... if you found the code on csharp corner, then the obvious thing to do is: ask there.
Talk to the author, explain your problem, see how much help he can be.

Asking at a random site for support on software from a different site isn't the most sensible way to get started...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900