View Single Post
  #1 (permalink)  
Old February 20th, 2007, 05:51 AM
AnotherAspdotnetDeveloper AnotherAspdotnetDeveloper is offline
Registered User
Join Date: Feb 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default DataList DataBinding Error: IndexOutOfRange


I am programming in 2.0 VS 2005.

I am using PageDataSource class to page between pages in a list of Items

I am getting a runtime exception IndexOutOfRange upon calling DataList's DataBind method afterwith PageDataSource object.

Following is the code I am trying:
 Dim objPds As PagedDataSource
  Protected Sub Page_Load()
        If Not Page.IsPostBack Then
        End If
    End Sub

    Public Property CurrentPage() As Integer
            Dim o As Object = Me.ViewState("_CurrentPage")
            If o Is Nothing Then
                Return 0
                Return Convert.ToInt32(o)

            End If
        End Get
        Set(ByVal value As Integer)
            Me.ViewState("_CurrentPage") = value
        End Set
    End Property

    Protected Sub lnkBtnCmdNext_Click() 
        CurrentPage = CurrentPage + 1
    End Sub

    Protected Sub lnkBtnCmdPrev_Click()
        CurrentPage = CurrentPage - 1
    End Sub

    Protected Sub ItemsGet()
        Dim Items As DataSet = New DataSet

        objPds = New PagedDataSource

        Dim da As New SqlDataAdapter("exec sp_View_Existing_Glossary", GetConnectionString)

        objPds.DataSource = Items.Tables(0).DefaultView

        objPds.AllowPaging = True
        objPds.PageSize = 3

        objPds.CurrentPageIndex = CurrentPage - 1

        lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() & " of " & _

        lnkBtnCmdPrev.Enabled = Not objPds.IsFirstPage
        lnkBtnCmdNext.Enabled = Not objPds.IsLastPage
        DataList1.DataSource = objPds

    End Sub
The corresponding ASPX file contains

 <asp:DataList ID="DataList1"  runat="server">
Please help. I get IndexOutofRange Exception upon Databind of DataList and PageDataSourceObject (indicated above in the code as bold). What am I doing wrong?

The DataSet contains 4 rows of result data

I got the source code from an 4GuysFromRolla article. This article showed for DataRepeater and said it should not be different for DataList. Hence I used the same code to structure the Datalist

Thanks in Advance.