?
Solved

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

Posted on 2009-12-19
6
Medium Priority
?
716 Views
Last Modified: 2013-11-08
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
0
Comment
Question by:westdh
  • 3
  • 3
6 Comments
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26089676
Did you even fill DataKeyNames property of grid view in your code or designer, this problem occurred because your DataKeys array has no data I think.
could you bring your whole code here? both .aspx and .vb
0
 

Author Comment

by:westdh
ID: 26089819
aspx page
<%@ Page Language="VB"  Debug="true"  MaintainScrollPositionOnPostback="true" AutoEventWireup="false" CodeFile="EventRegEmailRpt.aspx.vb" Inherits="wwwroot_EventRegMailRpt" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">     
<HTML>
<head>
</head>
     
<body id="Body1" align="center" runat="server"><table align="center" width="1024">
<tr><td align="center"><strong><font size="4">CMAA Northern California Event Email Attendees</font></strong></td></tr>
<tr align="center"><td>
<form align="center" id="form1" runat="server">
<br />
Select Event Name:
<asp:DropDownList ID="DroplistData" runat="server" Width="340px"  AutoPostBack="True" SelectedIndexChanged="DroplistData_SelectedIndexChanged">
<asp:ListItem> </asp:ListItem>
</asp:DropDownList>
<br /><br />
SelectSelectSelect Month:
<asp:DropDownList ID="ddlMth" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlMth_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="1">Jan</asp:ListItem>
<asp:ListItem Value="2">Feb</asp:ListItem>
<asp:ListItem Value="3">Mar</asp:ListItem>
<asp:ListItem Value="4">Apr</asp:ListItem>
<asp:ListItem Value="5">May</asp:ListItem>
<asp:ListItem Value="6">Jun</asp:ListItem>
<asp:ListItem Value="7">Jul</asp:ListItem>
<asp:ListItem Value="8">Aug</asp:ListItem>
<asp:ListItem Value="9">Sep</asp:ListItem>
<asp:ListItem Value="10">Oct</asp:ListItem>
<asp:ListItem Value="11">Nov</asp:ListItem>
<asp:ListItem Value="12">Dec</asp:ListItem>
</asp:DropDownList>

Select Year:
<asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlYear_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="2009">2009</asp:ListItem>
<asp:ListItem Value="2010">2010</asp:ListItem>
<asp:ListItem Value="2011">2011</asp:ListItem>
<asp:ListItem Value="2012">2012</asp:ListItem>
<asp:ListItem Value="2013">2013</asp:ListItem>
<asp:ListItem Value="2014">2014</asp:ListItem>
<asp:ListItem Value="2015">2015</asp:ListItem>
<asp:ListItem Value="2016">2016</asp:ListItem>
<asp:ListItem Value="2017">2017</asp:ListItem>
<asp:ListItem Value="2018">2018</asp:ListItem>
<asp:ListItem Value="2019">2019</asp:ListItem>
<asp:ListItem Value="2020">2020</asp:ListItem>
<asp:ListItem Value="2021">2021</asp:ListItem>
<asp:ListItem Value="2022">2022</asp:ListItem>
<asp:ListItem Value="2023">2023</asp:ListItem>
<asp:ListItem Value="2024">2024</asp:ListItem>
<asp:ListItem Value="2025">2025</asp:ListItem>
</asp:DropDownList>

<br />
<br />
  <asp:GridView ID="uxItemDetailGrid" runat="server" ShowFooter="true" AutoGenerateColumns="false"  HorizontalAlign="center" HeaderStyle-Height="80" CellPadding="2"  BorderColor="#A1C1D9"  GridLines="Both" RowStyle-Font-Size="8" width="1024">
        <HeaderStyle backcolor="#5D7B9D" font-bold="True" forecolor="White" /><FooterStyle backcolor="#5D7B9D" font-bold="True" forecolor="White" />
        <Columns>
             <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox runat="server" ID="RowLevelCheckBox" />
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField  ItemStyle-BackColor = "#F4F4F4" HeaderStyle-BackColor="#A1C1D9"   HeaderStyle-Font-Size="9"  HeaderText="Event<br>Date">
                <ItemTemplate>
                    <asp:Label ID="lblEventDate" runat="server"  BackColor = "#F4F4F4"
                            Text='<%# Eval("dtmEvent") %>'>
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ItemStyle-BackColor = "#F4F4F4" ItemStyle-HorizontalAlign="Left" HeaderStyle-BackColor="#A1C1D9" HeaderStyle-Font-Size="9" HeaderText="Event<br>Title">
                <ItemTemplate>
                    <asp:Label ID="lblTitle" runat="server" BackColor = "#F4F4F4"
                            Text='<%# Eval("strTitle") %>'>
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ItemStyle-BackColor = "#F4F4F4" ItemStyle-HorizontalAlign="Left" HeaderStyle-BackColor="#A1C1D9" HeaderStyle-Font-Size="9" HeaderText="Company<br>Name">
                <ItemTemplate>
                    <asp:Label ID="lblstrName" runat="server" BackColor = "#F4F4F4"
                            Text='<%# Eval("strName") %>'>
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

                <asp:TemplateField ItemStyle-BackColor = "#F4F4F4" ItemStyle-HorizontalAlign="Left" HeaderStyle-BackColor="#A1C1D9" HeaderStyle-Font-Size="9" HeaderText="Attendee<br>Name">
                <ItemTemplate>
                    <asp:Label ID="lblUserName" runat="server" BackColor = "#F4F4F4"
                            Text='<% #Eval("strRegistrationUserName") %>'>
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
           
               <asp:TemplateField ItemStyle-BackColor = "#F4F4F4" ItemStyle-HorizontalAlign="Left" HeaderStyle-BackColor="#A1C1D9" HeaderStyle-Font-Size="9" HeaderText="Attendee<br>Email" FooterStyle-Font-Size="9">
                <ItemTemplate>
                    <asp:Label ID="lblstrBadgeName" runat="server" BackColor = "#F4F4F4"
                          Text='<%# DataBinder.Eval(Container.DataItem,"strBadgeName") %>'>
                           
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

             <asp:TemplateField ItemStyle-BackColor = "#F4F4F4" ItemStyle-HorizontalAlign="Left" HeaderStyle-BackColor="#A1C1D9" HeaderStyle-Font-Size="9" HeaderText="Date Attendee<br>Registered">
                <ItemTemplate>
                    <asp:Label ID="lbldtmCreated" runat="server" BackColor = "#F4F4F4"
                            Text='<% #Eval("dtmCreated") %>'>
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
 
        </Columns>
    </asp:GridView>

   <p>
   <asp:Button ID="CheckAll" runat="server" Text="Check All" />
   &nbsp;
   <asp:Button ID="UncheckAll" runat="server" Text="Uncheck All" />
   </p>
            <table border="0" cellSpacing="0" cellPadding="0" width="600">
                  <tr>
                        <td height="43"  align="left" class="style1"><b>Email Subject:</b>&nbsp;<input id="txtEmailSubject" value="Enter subject here." size="35" runat="server" name="txtEmailSubject"
                                    runat="server" /></td>
                        <td height="43"></td>
                  </tr>
                  <tr>
                        <td colSpan="2" align="left"><b>Email Body Contents:</b></td>
                  </tr>
                  <tr>
                  <td>
                <textarea id="txtArea" runat="server" cols="80" rows="10" name="txtArea"></textarea></td>
                  </tr>
            </table>
            <p><asp:button id="EmailChecked" runat="server" Text="Send Email to Attendees"></asp:button></p>
</form>
</td></tr>
</table>
<table align="center" width="800">
<tr><td align="center"> This Schedule is for CMAA Norcal Records only.
</td></tr>
</table>
</body>
</HTML>
=========================================================================
vb codebehind page

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Collections
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Net.Mail

Partial Public Class wwwroot_EventRegMailRpt
    Inherits System.Web.UI.Page
    Private iRowsCount As Integer = 0

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        If Not IsPostBack Then

            Dim dtVolumeOrder As New DataTable()
            Dim strSelectCommand As String = "SELECT e.dtmEvent,e.strTitle,r.strName,u.strRegistrationUserName,u.strBadgeName,r.dtmCreated FROM i2Integration_EventRegv45_Event e,i2Integration_EventRegv45_Registration r, i2Integration_EventRegv45_RegistrationUser u where e.intEventID = r.intEventID and r.intRegistrationID = u.intRegistrationID and r.blnEmailSent=1 Order by e.dtmEvent"
            Using sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString)
                Using adapPatientBills As New SqlDataAdapter(strSelectCommand, sqlConn)
                    adapPatientBills.Fill(dtVolumeOrder)
                End Using
            End Using

            uxItemDetailGrid.DataSource = dtVolumeOrder
            uxItemDetailGrid.DataBind()

            Dim dtVolumeOrder2 As New DataTable()
            Dim strSelectCommand2 As String = "SELECT distinct(strTitle) FROM i2Integration_EventRegv45_Event"
            Using sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString)
                Using adapPatientBills As New SqlDataAdapter(strSelectCommand2, sqlConn)
                    adapPatientBills.Fill(dtVolumeOrder2)
                End Using
            End Using
            DroplistData.DataSource = dtVolumeOrder2
            DroplistData.DataTextField = "strTitle"
            DroplistData.DataBind()

        End If
    End Sub

    Protected Sub DroplistData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim dtVolumeOrder As New DataTable()

        'Where strTitle = '" + DroplistData.SelectedValue + "'"
        Dim strSelectCommand As String = "SELECT strTitle FROM i2Integration_EventRegv45_Event"
        Using sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString)
            Using adapPatientBills As New SqlDataAdapter(strSelectCommand, sqlConn)
                adapPatientBills.Fill(dtVolumeOrder)
            End Using
        End Using
        DroplistData.DataSource = dtVolumeOrder
        DroplistData.DataTextField = "strTitle"
        DroplistData.DataBind()

    End Sub

    Protected Sub ddlMth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        '+ "' AND e.strTitle = '" + DroplistData.SelectedValue
        Dim dtVolumeOrder As New DataTable()
        Dim strSelectCommand As String = "SELECT e.dtmEvent,e.strTitle,r.strName,u.strRegistrationUserName,u.strBadgeName,r.dtmCreated FROM i2Integration_EventRegv45_Event e,i2Integration_EventRegv45_Registration r, i2Integration_EventRegv45_RegistrationUser u where e.intEventID = r.intEventID and r.intRegistrationID = u.intRegistrationID and r.blnEmailSent=1 and(YEAR(e.dtmEvent) = '" + ddlYear.SelectedValue + "' AND MONTH(e.dtmEvent) = '" + ddlMth.SelectedValue + "' AND e.strTitle = '" + DroplistData.SelectedValue + "') Order by e.dtmEvent"
        Using sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString)
            Using adapPatientBills As New SqlDataAdapter(strSelectCommand, sqlConn)
                adapPatientBills.Fill(dtVolumeOrder)
            End Using
        End Using
        iRowsCount = dtVolumeOrder.Rows.Count - 1
        uxItemDetailGrid.DataSource = dtVolumeOrder
        uxItemDetailGrid.DataBind()

    End Sub

    Protected Sub ddlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        '+ "' AND e.strTitle = '" + DroplistData.SelectedValue
        Dim dtVolumeOrder As New DataTable()
        Dim strSelectCommand As String = "SELECT e.dtmEvent,e.strTitle,r.strName,u.strRegistrationUserName,u.strBadgeName, r.dtmCreated FROM i2Integration_EventRegv45_Event e,i2Integration_EventRegv45_Registration r, i2Integration_EventRegv45_RegistrationUser u where e.intEventID = r.intEventID and r.intRegistrationID = u.intRegistrationID and r.blnEmailSent=1 and(YEAR(e.dtmEvent) = '" + ddlYear.SelectedValue + "' AND MONTH(e.dtmEvent) = '" + ddlMth.SelectedValue + "' AND e.strTitle = '" + DroplistData.SelectedValue + "') Order by e.dtmEvent"
        Using sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString)
            Using adapPatientBills As New SqlDataAdapter(strSelectCommand, sqlConn)
                adapPatientBills.Fill(dtVolumeOrder)
            End Using
        End Using
        iRowsCount = dtVolumeOrder.Rows.Count - 1
        uxItemDetailGrid.DataSource = dtVolumeOrder
        uxItemDetailGrid.DataBind()
    End Sub

    Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        ' Verifies that the control is rendered
    End Sub
    Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        uxItemDetailGrid.PageIndex = e.NewPageIndex
        uxItemDetailGrid.DataBind()
    End Sub
    Protected Sub CheckAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckAll.Click
        'Enumerate each GridViewRow
        For Each gvr As GridViewRow In uxItemDetailGrid.Rows
            'Programmatically access the CheckBox from the TemplateField
            Dim cb As CheckBox = CType(gvr.FindControl("RowLevelCheckBox"), CheckBox)

            'Check it!
            cb.Checked = True
        Next
    End Sub


    Protected Sub UncheckAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UncheckAll.Click
        'Enumerate each GridViewRow
        For Each gvr As GridViewRow In uxItemDetailGrid.Rows
            'Programmatically access the CheckBox from the TemplateField
            Dim cb As CheckBox = CType(gvr.FindControl("RowLevelCheckBox"), CheckBox)

            'Uncheck it!
            cb.Checked = False
        Next
    End Sub

    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)("strBadgeName").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

End Class



0
 
LVL 30

Accepted Solution

by:
Reza Rad earned 2000 total points
ID: 26089843
change this section of page_load like this:

uxItemDetailGrid.DataSource = dtVolumeOrder
uxItemDetailGrid.DataKeyNames = New String() {"yourPKfieldname"}
            uxItemDetailGrid.DataBind()
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:westdh
ID: 26089921
that seemed to works...
but now I am getting this error when I send mail

Syntax error, command unrecognized. The server response was: imp09 smtp.charter.net MjE2LjEwLjI0Ni4xNjE= You must connect from Charter IP space. E1110
0
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26089930
Glad to hear it.
the mail is another problem at all
I think Its better to create new question with more details on code that you got this error
0
 

Author Closing Comment

by:westdh
ID: 31668204
OK thanks a lot
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question