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:
The error is appearing on the line
It works on page 1 but not the other pages.
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.