Emeka Okoye

Emeka Okoye

  • NA
  • 59
  • 2.2k

resolve index -5 is either negative or above rows count

Sep 23 2017 8:05 AM
 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
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="PagininDataList.WebForm1" %>   
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.    
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         .style1  
  9.         {  
  10.             width: 672px;  
  11.         }  
  12.     </style>  
  13. </head>  
  14. <body>  
  15.     <form id="form1" runat="server">  
  16.    <div>  
  17.     <table style="height: 79px; width: 429px">  
  18.         <asp:DataList ID="DataList1" runat="server">  
  19.         <HeaderTemplate>  
  20.         <h1> Details </h1>  
  21.         </HeaderTemplate>  
  22.       <ItemTemplate>  
  23.         <tr>  
  24.    
  25.         My Custom Datalist Template here  
  26.         </tr>  
  27.    
  28.         </ItemTemplate>  
  29.         </asp:DataList>  
  30.        </table>  
  31.    <table>  
  32.   <tr>  
  33.     <td>  
  34.         <asp:Button ID="btnfirst" runat="server" Font-Bold="true" Text="<<" Height="31px"  
  35.                     Width="43px" onclick="btnfirst_Click" /></td>  
  36.         <td>  
  37.             <asp:Button ID="btnprevious" runat="server" Font-Bold="true" Text="<" Height="31px"  
  38.                     Width="43px" onclick="btnprevious_Click" /></td>  
  39.             <td>  
  40.                 <asp:Button ID="btnnext" runat="server" Font-Bold="true" Text=">" Height="31px"  
  41.                     Width="43px" onclick="btnnext_Click" /></td>  
  42.                 <td>  
  43.                     <asp:Button ID="btnlast" runat="server" Font-Bold="true" Text=">>" Height="31px"  
  44.                     Width="43px" onclick="btnlast_Click" /></td>  
  45.     </tr>  
  46.    </table>  
  47.     </div>  
  48.     </form>  
  49. </body>  
  50. </html> 
 This is the C# Code
  1. namespace PagininDataList  
  2. {  
  3.     public partial class WebForm1 : System.Web.UI.Page  
  4.     {  
  5.         SqlDataAdapter dadapter;  
  6.         DataSet dset;  
  7.         PagedDataSource adsource = new PagedDataSource();  
  8.         string connstring = "My connection string";  
  9.         int pos;  
  10.         protected void Page_Load(object sender, EventArgs e)  
  11.         {  
  12.             if (!IsPostBack)  
  13.             {  
  14.                 this.ViewState["vs"] = 0;  
  15.             }  
  16.             pos = (int)this.ViewState["vs"];          
  17.             databind();  
  18.         }               
  19.         public void databind()  
  20.         {  
  21.             dadapter = new SqlDataAdapter("my SELECT query", connstring);  
  22.             dset = new DataSet();  
  23.               
  24.             dadapter.Fill(dset);  
  25.             adsource.DataSource = dset.Tables[0].DefaultView;  
  26.             adsource.PageSize = 3;  
  27.             adsource.AllowPaging=true;  
  28.             adsource.CurrentPageIndex = pos;  
  29.             btnfirst.Enabled = !adsource.IsFirstPage;  
  30.             btnprevious.Enabled = !adsource.IsFirstPage;  
  31.             btnlast.Enabled = !adsource.IsLastPage;  
  32.             btnnext.Enabled = !adsource.IsLastPage;  
  33.             DataList1.DataSource = adsource;  
  34.             DataList1.DataBind();   
  35.         }  
  36.    
  37.         protected void btnfirst_Click(object sender, EventArgs e)  
  38.         {  
  39.             pos = 0;   
  40.             this.ViewState["vs"] = 0;   
  41.             databind();  
  42.         }  
  43.    
  44.         protected void btnprevious_Click(object sender, EventArgs e)  
  45.         {  
  46.             pos = (int)this.ViewState["vs"];  
  47.             pos -= 1;  
  48.             this.ViewState["vs"] = pos;  
  49.             databind();  
  50.         }  
  51.    
  52.         protected void btnnext_Click(object sender, EventArgs e)  
  53.         {  
  54.             pos = (int)this.ViewState["vs"];  
  55.             pos += 1;  
  56.             this.ViewState["vs"] = pos;  
  57.             databind();  
  58.         }  
  59.    
  60.         protected void btnlast_Click(object sender, EventArgs e)  
  61.         {  
  62.             pos = adsource.PageCount - 1;   
  63.         this.ViewState["vs"] = pos;   
  64.         databind();   
  65.         }  
  66.     }