troubleshooting Question

asp.net Getting error - Index was out of range. Must be non-negative and less than the size of the collection.

Avatar of westdh
westdhFlag for United States of America asked on
.NET ProgrammingASP.NETVisual Basic.NET
6 Comments1 Solution805 ViewsLast Modified:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
error line is Line 132:                sb.Append(uxItemDetailGrid.DataKeys(gr.RowIndex)("strBadgeName").ToString

Source Error:
Line 130:            Dim cb As CheckBox = DirectCast(gr.FindControl("RowLevelCheckBox"), CheckBox)
Line 131:            If cb.Checked Then
Line 132:                sb.Append(uxItemDetailGrid.DataKeys(gr.RowIndex)("strBadgeName").ToString())
Line 133:                sb.Append(",")
Line 134:            End If
 
Protected Sub EmailChecked_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles EmailChecked.Click
        Dim sb As New StringBuilder()

        For Each gr As GridViewRow In uxItemDetailGrid.Rows
            Dim cb As CheckBox = DirectCast(gr.FindControl("RowLevelCheckBox"), CheckBox)
            If cb.Checked Then
                sb.Append(uxItemDetailGrid.DataKeys(gr.RowIndex)("email").ToString())
                sb.Append(",")
            End If
        Next

        If sb.ToString() <> "" Then

            Dim msg As New MailMessage("westdh@charter.net", sb.ToString())
            msg.Subject = txtEmailSubject.Value
            msg.Body = txtArea.Value
            Dim sc As New SmtpClient("smtp.live.com", 25)
            sc.Credentials = New Net.NetworkCredential("westdh@charter.net", "Design12")
            sc.EnableSsl = True
            Try
                sc.Send(msg)
                Response.Write("<B>Email Has been sent successfully.</B>")
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try
        End If

    End Sub
ASKER CERTIFIED SOLUTION
Reza Rad
Consultant, Trainer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros