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. "http://www.w3.org/1999/xhtml">  
  5. "server">  
  6.       
  7.       
  8.   
  9.   
  10.     "form1" runat="server">  
  11.    
      
  12.     "height: 79px; width: 429px"
    >  
  13.         "DataList1" runat="server">  
  14.           
  15.          Details   
  16.           
  17.         
  18.           
  19.    
  20.         My Custom Datalist Template here  
  21.           
  22.    
  23.           
  24.           
  25.          
  26.      
  27.     
  28.       
  29.         "btnfirst" runat="server" Font-Bold="true" Text="<<" Height="31px"  
  30.                     Width="43px" onclick="btnfirst_Click" />  
  31.           
  32.             "btnprevious" runat="server" Font-Bold="true" Text="<" Height="31px"  
  33.                     Width="43px" onclick="btnprevious_Click" />  
  34.               
  35.                 "btnnext" runat="server" Font-Bold="true" Text=">" Height="31px"  
  36.                     Width="43px" onclick="btnnext_Click" />  
  37.                   
  38.                     "btnlast" runat="server" Font-Bold="true" Text=">>" Height="31px"  
  39.                     Width="43px" onclick="btnlast_Click" />  
  40.       
  41.      
  42.     
  
  •       
  •   
  •  
  •  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.     }  
    67. }