How do I fix "index is out of range" error? 
Author Message
 How do I fix "index is out of range" error?

I am developing a web application that enables users to
modify, delete and add data in a database using a
datagrid.  Currently I am working on creating Paged Data
Access using a Web Forms Page.  At the bottom I have
pasted part of the code. I keep getting an error when i
try to build and browse the project that is the following:
If anyone can help me I'd appreciate it.

Index was out of range. Must be non-negative and less than
the size of the collection. Parameter name: index

Source Error:

Line 251:        SqlConnection1.Close()
Line 252:        ViewState("CurrentPage") = CurrentPage
Line 253:        ViewState("CurrentPage".ToString) =
DataGrid1.Items(1).Cells(1).Text
Line 254:        If DataGrid1.Items.Count <
DataGrid1.PageSize Then
Line 255:            btnNext.Enabled = False

Private Sub Page_Load(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles MyBase.Load

        If Me.IsPostBack Then
            DsRSTest1 = CType(Session("dsRSTest"),
dsRSTest)
        Else


= ""
            CurrentPage = 0
            FillGrid(cmdNext)
        End If
    End Sub

 Private Sub FillGrid(ByVal currentSqlCommand As
SqlClient.SqlCommand)
        Dim sr As SqlClient.SqlDataReader
        SqlConnection1.Open()
        sr = currentSqlCommand.ExecuteReader()
        DataGrid1.DataSource = sr
        DataGrid1.DataBind()
        sr.Close()
        SqlConnection1.Close()
        ViewState("CurrentPage") = CurrentPage
        ViewState(CurrentPage.ToString) = DataGrid1.Items
(0).Cells(1).Text
        If DataGrid1.Items.Count < DataGrid1.PageSize Then
            btnNext.Enabled = False
        End If

    End Sub

Private Sub btnNext_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnNext.Click
        CurrentPage = CType(ViewState("CurrentPage"),
Integer)
        CurrentPage += 1
        Dim lastid As String = DataGrid1.Items(19).Cells
(1).Text

lastid
        FillGrid(cmdNext)
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnPrevious.Click
        btnNext.Enabled = True
        CurrentPage = CType(ViewState("CurrentPage"),
Integer)
        CurrentPage -= 1
        If CurrentPage >= 0 Then
            Dim firstid As String
            firstid = CType(ViewState
(CurrentPage.ToString), String)
            cmdPrevious.Parameters

            FillGrid(cmdPrevious)
        End If
    End Sub



Tue, 08 Feb 2005 01:44:02 GMT  
 How do I fix "index is out of range" error?
Looks like you're still thinking 1-based rather than 0-based.

HTH,

--
Kevin Spencer
Microsoft FrontPage MVP
Internet Programmer
For ASP Tutorials and Information -
http://www.takempis.com


Quote:
> I am developing a web application that enables users to
> modify, delete and add data in a database using a
> datagrid.  Currently I am working on creating Paged Data
> Access using a Web Forms Page.  At the bottom I have
> pasted part of the code. I keep getting an error when i
> try to build and browse the project that is the following:
> If anyone can help me I'd appreciate it.

> Index was out of range. Must be non-negative and less than
> the size of the collection. Parameter name: index

> Source Error:

> Line 251:        SqlConnection1.Close()
> Line 252:        ViewState("CurrentPage") = CurrentPage
> Line 253:        ViewState("CurrentPage".ToString) =
> DataGrid1.Items(1).Cells(1).Text
> Line 254:        If DataGrid1.Items.Count <
> DataGrid1.PageSize Then
> Line 255:            btnNext.Enabled = False

> Private Sub Page_Load(ByVal sender As System.Object, ByVal
> e As System.EventArgs) Handles MyBase.Load

>         If Me.IsPostBack Then
>             DsRSTest1 = CType(Session("dsRSTest"),
> dsRSTest)
>         Else


> = ""
>             CurrentPage = 0
>             FillGrid(cmdNext)
>         End If
>     End Sub

>  Private Sub FillGrid(ByVal currentSqlCommand As
> SqlClient.SqlCommand)
>         Dim sr As SqlClient.SqlDataReader
>         SqlConnection1.Open()
>         sr = currentSqlCommand.ExecuteReader()
>         DataGrid1.DataSource = sr
>         DataGrid1.DataBind()
>         sr.Close()
>         SqlConnection1.Close()
>         ViewState("CurrentPage") = CurrentPage
>         ViewState(CurrentPage.ToString) = DataGrid1.Items
> (0).Cells(1).Text
>         If DataGrid1.Items.Count < DataGrid1.PageSize Then
>             btnNext.Enabled = False
>         End If

>     End Sub

> Private Sub btnNext_Click(ByVal sender As System.Object,
> ByVal e As System.EventArgs) Handles btnNext.Click
>         CurrentPage = CType(ViewState("CurrentPage"),
> Integer)
>         CurrentPage += 1
>         Dim lastid As String = DataGrid1.Items(19).Cells
> (1).Text

> lastid
>         FillGrid(cmdNext)
>     End Sub

>     Private Sub btnPrevious_Click(ByVal sender As
> System.Object, ByVal e As System.EventArgs) Handles
> btnPrevious.Click
>         btnNext.Enabled = True
>         CurrentPage = CType(ViewState("CurrentPage"),
> Integer)
>         CurrentPage -= 1
>         If CurrentPage >= 0 Then
>             Dim firstid As String
>             firstid = CType(ViewState
> (CurrentPage.ToString), String)
>             cmdPrevious.Parameters

>             FillGrid(cmdPrevious)
>         End If
>     End Sub



Tue, 08 Feb 2005 03:51:11 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. "Subscirpt out of range error"

2. "Subscript out of range Error"

3. "Fetch type out of range" error

4. "Subscript out of Range" error message

5. Dbase "Index not found" error

6. Undo "Range"?

7. HELP with .Bookmarks("variable").Range.InsertFile

8. Selection.Sort Key1:=Range("??")

9. Selection.Sort Key1:=Range("???")

10. Selection.Sort Key1:=Range("???")

11. "subscript out of range"

12. "subscript out of range" - Excel

 

 
Powered by phpBB® Forum Software