I am using Visual studio 2010, targeting 3.5 framework with MySQL database as the backend.
My gridview loads fine, I click the 'Edit' link and it allows me to edit. Once I hit the 'Update' link I keep getting the following error, "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
The line it points to is in gvCourses_RowUpdating --> Dim iCourseID As Integer = Int32.Parse(gvCourses.DataKeys(e.RowIndex).Value.ToString)
Below is the code I am using. Does anyone know see why I keep getting this?
Html code
<asp:GridView ID="gvCourses" runat="server" AutoGenerateColumns="false" DataKeyField="course_id" HeaderStyle-CssClass="dgStyle" OnPageIndexChanging="gvCourses_PageIndexChanging" OnPageIndexChanged="gvCourses_PageIndexChanged" OnRowCancelingEdit="gvCourses_RowCancelingEdit" OnRowDataBound="gvCourses_RowDataBound" OnRowDeleting="gvCourses_RowDeleting" OnRowEditing="gvCourses_RowEditing" OnRowUpdating="gvCourses_RowUpdating" OnSorting="gvCourses_Sorting" OnSorted="gvCourses_Sorted"><Columns><asp:CommandField ShowEditButton="true" /><asp:CommandField ShowDeleteButton="true" /><asp:BoundField DataField="course_id" HeaderText="ID" ReadOnly="true" /><asp:BoundField DataField="course_name" HeaderText="Course Name" /><asp:BoundField DataField="course_desc" HeaderText="Course Desc" /><asp:BoundField DataField="course_order" HeaderText="Course Order" /><asp:BoundField DataField="course_folder" HeaderText="Course Folder" ReadOnly="true" /><asp:TemplateField HeaderText="Course Certificate"><ItemTemplate><%# Container.DataItem("course_certificate")%></ItemTemplate><EditItemTemplate><asp:DropDownList id="ddlCourseCertificate" runat="server" DataTextField="CourseCertificate" DataValueField="CourseCertificateVal"></asp:DropDownList></EditItemTemplate></asp:TemplateField><asp:BoundField DataField="course_entry_dt" HeaderText="Entry Dt" ReadOnly="true" /></Columns></asp:GridView>
Code behind:
Imports System.Web.Security Imports System.Security.Principal Imports System.IO Partial Class admin_courses Inherits System.Web.UI.Page Dim oFn As clsFunctions = New clsFunctions Protected dvwCourseCertificates As DataView = New DataView() Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load If User.Identity.IsAuthenticated = True Then Dim id As FormsIdentity = CType(HttpContext.Current.User.Identity, FormsIdentity) Dim ticket As FormsAuthenticationTicket = id.Ticket Dim userData As String = ticket.UserData Dim roles() As String = userData.Split(Microsoft.VisualBasic.ChrW(44)) ' Create a new Generic Principal Instance and assign to Current User HttpContext.Current.User = New GenericPrincipal(id, roles) Dim objFormsID As FormsIdentity objFormsID = User.Identity 'Had issues with activated users not passing the right userdata this signs you out if so If objFormsID.Ticket.UserData = "0" Then Session.Abandon() FormsAuthentication.SignOut() Response.Redirect("/login.aspx") End If 'Finally check if admin user If Session("User_Role") <> "SUPER USER" Then Session.Abandon() FormsAuthentication.SignOut() Response.Redirect("/login.aspx") End If If Not IsPostBack() Then gvCourses.AllowPaging = True gvCourses.PageSize = 25 gvCourses.AllowSorting = True ViewState("SortExpression") = "course_order ASC" sbLoadPage() End If Else Session.Abandon() FormsAuthentication.SignOut() Response.Redirect("/login.aspx") End If End Sub #Region " sbLoadPage() " Private Sub sbLoadPage() 'load the gridview control Dim ds As DataSet = oFn.GetSQLData("SELECT * FROM `course`") If Not (ds Is Nothing) Then Dim dvCourses As DataView = ds.Tables(0).DefaultView dvCourses.Sort = ViewState("SortExpression").ToString() gvCourses.DataSource = dvCourses gvCourses.DataBind() End If End Sub #End Region Sub PopulateDropdownlist() Dim dtCourses As DataTable = New DataTable() dtCourses = oFn.CertificatesInDataTable(Server.MapPath("../certificates")) dvwCourseCertificates = New DataView(dtCourses) End Sub Protected Sub gvCourses_PageIndexChanged(sender As Object, e As System.EventArgs) Handles gvCourses.PageIndexChanged sbLoadPage() End Sub Protected Sub gvCourses_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvCourses.PageIndexChanging gvCourses.PageIndex = e.NewPageIndex gvCourses.EditIndex = -1 gvCourses.SelectedIndex = -1 'sbLoadPage() End Sub Protected Sub gvCourses_RowCancelingEdit(sender As Object, e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvCourses.RowCancelingEdit ' Exit edit mode. gvCourses.EditIndex = -1 ' Rebind the GridView control to show data in view mode. sbLoadPage() End Sub Protected Sub gvCourses_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCourses.RowDataBound Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView) If (e.Row.RowType = DataControlRowType.DataRow) AndAlso (e.Row.RowState = DataControlRowState.Normal OrElse e.Row.RowState = DataControlRowState.Alternate) Then ' Add client-side confirmation when deleting. DirectCast(e.Row.Cells(1).Controls(0), LinkButton).Attributes("onclick") = "if(!confirm('Are you certain you want to delete this course?')) return false;" End If 'Load dropdown in Edit mode If (e.Row.RowType = DataControlRowType.DataRow) AndAlso (e.Row.RowState = DataControlRowState.Edit Or e.Row.RowState = DataControlRowState.Alternate + DataControlRowState.Edit) Then Dim ddlCourseCertificate As DropDownList = e.Row.FindControl("ddlCourseCertificate") Dim iRecruiter_ID As Integer = e.Row.Cells(2).Text Dim dtCourses As DataTable = New DataTable() dtCourses = oFn.CertificatesInDataTable(Server.MapPath("../certificates")) PopulateDropdownlist() ddlCourseCertificate.DataSource = dtCourses ddlCourseCertificate.DataTextField = "CourseCertificate" ddlCourseCertificate.DataValueField = "CourseCertificateVal" ddlCourseCertificate.DataBind() Dim iLoop As Integer = 0 For Each dRow As DataRow In dtCourses.Rows If dRow.Item(0) = drv("course_certificate") Then ddlCourseCertificate.Items(iLoop).Selected = True Else ddlCourseCertificate.Items(iLoop).Selected = False End If iLoop += 1 Next End If End Sub Protected Sub gvCourses_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvCourses.RowDeleting 'code that deletes row Dim sCourse_ID As String = gvCourses.Rows(e.RowIndex).Cells(2).Text oFn.GetSQLData("DELETE FROM course WHERE course_id = " & sCourse_ID) sbLoadPage() End Sub Protected Sub gvCourses_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvCourses.RowEditing gvCourses.EditIndex = e.NewEditIndex sbLoadPage() End Sub Protected Sub gvCourses_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvCourses.RowUpdating 'e.Keys, e.NewValues, and e.OldValues are only populated if using DataSourceID Dim iCourseID As Integer = Int32.Parse(gvCourses.DataKeys(e.RowIndex).Value.ToString) Dim txtCourse_ID As TextBox = DirectCast(gvCourses.Rows(e.RowIndex).Cells(2).Controls(0), TextBox) gvCourses.EditIndex = -1 sbLoadPage() End Sub Protected Sub gvCourses_Sorted(sender As Object, e As System.EventArgs) Handles gvCourses.Sorted sbLoadPage() End Sub Protected Sub gvCourses_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvCourses.Sorting Dim strSortExpression As String() = ViewState("SortExpression").ToString().Split(" "c) ' If the sorting column is the same as the previous one, ' then change the sort order. If strSortExpression(0) = e.SortExpression Then If strSortExpression(1) = "ASC" Then ViewState("SortExpression") = Convert.ToString(e.SortExpression) & " " & "DESC" Else ViewState("SortExpression") = Convert.ToString(e.SortExpression) & " " & "ASC" End If Else ' If sorting column is another column, ' then specify the sort order to "Ascending". ViewState("SortExpression") = Convert.ToString(e.SortExpression) & " " & "ASC" End If gvCourses.EditIndex = -1 gvCourses.SelectedIndex = -1 End Sub End Class