Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Index was out of range error.

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I am getting the followin error:
Code:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

here is my code:

Code:
Protected Sub btnRequestFiles_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRequestFiles.Click
        For Each item As GridDataItem In RadGrid1.SelectedItems

            Dim conFiles As SqlConnection
            Dim strConnection As String
            Dim cmd As New SqlCommand
            Dim cmdinsert As SqlCommand
            Dim strInsert As String

            strConnection = ConfigurationManager.ConnectionStrings("FileawaySQLConnectionString").ConnectionString
            conFiles = New SqlConnection(strConnection)
            conFiles.Open()
            cmd.Connection = conFiles
            cmd.CommandText = "UPDATE dbo.Files SET FileStatus = 2 WHERE FileID = '" + RowID.ToString() + "'"
            cmd.ExecuteNonQuery()
            conFiles.Close()
            Me.lblRequestSuccessful.Text = "Files requested successfully"
            RadGrid1.Rebind()

            'conFiles = New SqlConnection(strConnection)
            ' strInsert = "INSERT INTO dbo.FileHistory (FileID, Action) VALUES (@RowID, @Action)"
            ' cmdinsert = New SqlCommand(strInsert, conFiles)
            ' cmdinsert.Parameters.AddWithValue("@RowID", RowID)
            ' cmdinsert.Parameters.AddWithValue("@Action", 2)
            ' conFiles.Open()
            'cmdinsert.ExecuteNonQuery()
            'conFiles.Close()

 
        Next
    End Sub

The error is appearing on this line:
Code:
Dim RowID As String = item.GetDataKeyValue("FileID")
 
Where is that line in the code you posted? You need to post all the relevant code.
 
Here is the complete code

Code:
Protected Sub btnRequestFiles_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRequestFiles.Click
        For Each item As GridDataItem In RadGrid1.SelectedItems
            Dim RowID As String = item.GetDataKeyValue("FileID")
            Dim conFiles As SqlConnection
            Dim strConnection As String
            Dim cmd As New SqlCommand
            Dim cmdinsert As SqlCommand
            Dim strInsert As String

            strConnection = ConfigurationManager.ConnectionStrings("FileawaySQLConnectionString").ConnectionString
            conFiles = New SqlConnection(strConnection)
            conFiles.Open()
            cmd.Connection = conFiles
            cmd.CommandText = "UPDATE dbo.Files SET FileStatus = 2 WHERE FileID = '" + RowID.ToString() + "'"
            cmd.ExecuteNonQuery()
            conFiles.Close()
            Me.lblRequestSuccessful.Text = "Files requested successfully"
            RadGrid1.Rebind()

            'conFiles = New SqlConnection(strConnection)
            ' strInsert = "INSERT INTO dbo.FileHistory (FileID, Action) VALUES (@RowID, @Action)"
            ' cmdinsert = New SqlCommand(strInsert, conFiles)
            ' cmdinsert.Parameters.AddWithValue("@RowID", RowID)
            ' cmdinsert.Parameters.AddWithValue("@Action", 2)
            ' conFiles.Open()
            'cmdinsert.ExecuteNonQuery()
            'conFiles.Close()

 
        Next
    End Sub
 
This question pertains more to the Telerik RadGrid than to ASP.NET. Did you check that "FileID" is actually deficed as a DataKeyName for the grid?
 
Yes it is defined. It works sometimes and not others. I will post the problem onto telerik forums.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top