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 IamaSherpa 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. Must be non-negative and less than the size of the collection. Parameter nam

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
Getting the error 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 gvHosts_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)

        If e.CommandName = "Allocate" Then

            ' First create a new Guid
            Dim strGuid As Guid = System.Guid.NewGuid()

            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim gvSelectedRow As GridViewRow = gvHosts.Rows(index)

            'Dim strTotalOccupancy As Decimal = Convert.ToDecimal(gvHosts.Rows(index).Cells(10).Text.TrimEnd())
            Dim strHostID As String = gvSelectedRow.Cells(0).Text.Trim()

            Dim strMaleCount As Decimal = Convert.ToDecimal(CType(gvSelectedRow.FindControl("txtMaleStudents"), TextBox).Text)
            Dim strFemaleCount As Decimal = Convert.ToDecimal(CType(gvSelectedRow.FindControl("txtFemaleStudents"), TextBox).Text)
            Dim strLeaderCount As Decimal = Convert.ToDecimal(CType(gvSelectedRow.FindControl("txtLeaders"), TextBox).Text)

            'Dim strAllocationTotal As String = (strMaleDecimal + strFemaleDecimal).ToString

            Try


                Dim ConnString As [String] = ConfigurationManager.ConnectionStrings("HostLinkConnectionString").ConnectionString
                Dim con As New SqlConnection(ConnString)
                Dim cmd As New SqlCommand()
                cmd.CommandType = CommandType.StoredProcedure
                cmd.CommandText = "spAddAllocation"
                cmd.Parameters.Add("@BookingID", SqlDbType.NVarChar).Value = Request.QueryString("BookingID")
                cmd.Parameters.Add("@HostID", SqlDbType.NVarChar).Value = strHostID
                cmd.Parameters.Add("@NumberMales", SqlDbType.VarChar).Value = strMaleCount
                cmd.Parameters.Add("@NumberFemales", SqlDbType.VarChar).Value = strFemaleCount
                cmd.Parameters.Add("@NumberLeaders", SqlDbType.VarChar).Value = strLeaderCount
                cmd.Parameters.Add("@Status", SqlDbType.NVarChar).Value = "Allocated"
                cmd.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = strGuid


                cmd.Connection = con

                con.Open()
                cmd.ExecuteScalar()



            Catch ex As Exception
                'Throw New Exception(ex.Message.ToString)
            End Try

            dlAllocationSummary.DataBind()
            gvHosts.DataBind()

            Page.ClientScript.RegisterClientScriptBlock(Me.[GetType](), "A", "Saved();", True)
            

        End If

    End Sub

The error is appearing on the line
Code:
Dim gvSelectedRow As GridViewRow = gvHosts.Rows(index)

It works on page 1 but not the other pages.
 
You have to debug your code. Step through and see what the value of index is.
I suspect it is Nothing or a number larger than the number of rows in the gridview.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top