Click here to Skip to main content
15,921,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello programmers got an error here. It says Index was out of range. Must be non-negative and less than the size of the collection. pointing at
Dim coursecatId As String = String.Copy(grdCategory.DataKeys(e.RowIndex).Value)
I just want a delete confirmation using gridview. Here is my code:
ServerSide

VB
Protected Sub grdCategory_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdCategory.RowCommand
    If e.CommandName = "cmdDelete" Then
        Dim coursecatId As String = Convert.ToString(e.CommandArgument)
        _trainingplantemplateDAL = New TrainingPlanTemplateDAL
        Dim tptemp As New TrainingPlanTemplate

        tptemp.Year = drpdwnYear.Text
        tptemp.CourseCatID = coursecatId
        _trainingplantemplateDAL.GetAllbyYearandCatID(tptemp)


    End If
    CategoryBindGrid()
End Sub
Protected Sub grdCategory_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdCategory.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim l As ImageButton = DirectCast(e.Row.FindControl("ImageDelete"), ImageButton)
        l.Attributes.Add("onclick", "javascript:return " & "confirm('Are you sure you want to delete this record " & DataBinder.Eval(e.Row.DataItem, "CourseCatID") & "')")
    End If
End Sub

Protected Sub grdCategory_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles grdCategory.RowDeleting
    Dim coursecatId As String = String.Copy(grdCategory.DataKeys(e.RowIndex).Value)
    _trainingplantemplateDAL = New TrainingPlanTemplateDAL
    Dim tptemp As New TrainingPlanTemplate

    tptemp.Year = drpdwnYear.Text
    tptemp.CourseCatID = coursecatId
    _trainingplantemplateDAL.GetAllbyYearandCatID(tptemp)

    CategoryBindGrid()
End Sub

Client Side
XML
<asp:GridView ID="grdCategory" runat="server" AutoGenerateColumns="False" Height="190px"
                           Width="538px">
                           <Columns>
                            <asp:BoundField HeaderText = "ID" DataField="CourseCatID" />

                            <asp:TemplateField HeaderText="Course Category">
                                <ItemTemplate>
                                      <asp:Label ID="lbllastname" runat="server" Text='<%# Eval("CourseCatName")%>'> </asp:Label>
                               </ItemTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="Edit">
                                <ItemTemplate>
                                     <asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl='<%#"CourseList.aspx?ID=" & Eval("CourseCatID")%>'  Text="Edit Courses"></asp:LinkButton>
                               </ItemTemplate>
                            </asp:TemplateField>

                              <asp:TemplateField HeaderText="Delete" >
                                <ItemTemplate>
                                    <asp:ImageButton ID="ImageDelete" ImageUrl="../Images/Delete.png" runat="server" CommandName="cmdDelete" CommandArgument='<%# Eval("CourseCatID") %>'
                                       />
                                </ItemTemplate>
                               </asp:TemplateField>
                                 <asp:CommandField ShowDeleteButton="True" />
                           </Columns>
                       </asp:GridView>



Do you know whats my error? Thanks and more power Sirs/Mams
Posted

Hi,

See this code...

In your client code:

  <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript" language="javascript">

        function DeleteData() {
            return confirm("Are you sure you want to delete this record");
        }

        function EditData() {
            return confirm("Are you sure you want to edit thise record");
        }

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <tr>
            <td>
                <asp:GridView ID="grdCategory" runat="server" AutoGenerateColumns="False" Height="190px"
                    Width="538px" EnableModelValidation="True">
                    <columns>
                        <asp:BoundField HeaderText="ID" DataField="CourseCatID" ItemStyle-HorizontalAlign="Left"
                            HeaderStyle-HorizontalAlign="Left">
                            <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                            <itemstyle horizontalalign="Left"></itemstyle>
                        
                        <asp:TemplateField HeaderText="Course Category" ItemStyle-HorizontalAlign="Left"
                            HeaderStyle-HorizontalAlign="Left">
                            <itemtemplate>
                                <asp:Label ID="lblCourseCatName" runat="server" Text='<%# Eval("CourseCatName")%>'> 
                            </itemtemplate>
                            <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                            <itemstyle horizontalalign="Left"></itemstyle>
                        
                        <asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
                            <itemtemplate>
                                <asp:LinkButton ID="lnkCourseCatID" runat="server" 
                                 PostBackUrl='<%#"CourseList.aspx?ID=" & Eval("CourseCatID")%>'
                                    Text="Edit Courses">
                            </itemtemplate>
                            <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                            <itemstyle horizontalalign="Center"></itemstyle>
                        
                        <asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
                            <itemtemplate>
                                <asp:ImageButton ID="ImageDelete" ImageUrl="../Images/Delete.png" runat="server"
                                    CausesValidation="false" OnClientClick="return DeleteData();" CommandName="cmdDelete"
                                    CommandArgument='<%# Eval("CourseCatID") %>' />
                            </itemtemplate>
                            <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                            <itemstyle horizontalalign="Center"></itemstyle>
                        
                    </columns>
                
            </td>
        </tr>
    </div>
    </form>
</body>
</html>


In your code behind:

  ''' <summary>
''' This should be in your BusinessObject Layer ->Entity
''' </summary>
''' <remarks></remarks>
Public Class TrainingPlan
#Region "Defined private Variables"
    Private _courseCatID As Integer
    Private _courseCatName As String
#End Region
#Region "Defined public properties"
    Public Property CourseCatID() As Integer
        Get
            Return _courseCatID
        End Get
        Set(ByVal value As Integer)
            _courseCatID = value
        End Set
    End Property
    Public Property CourseCatName() As String
        Get
            Return _courseCatName
        End Get
        Set(ByVal value As String)
            _courseCatName = value
        End Set
    End Property
#End Region

#Region "The Constructor"
    ' Without parameter
    Public Sub New()
    End Sub
#End Region

End Class



Partial Class _Default
    Inherits System.Web.UI.Page
    Dim _trainingplantemplateDAL As New List(Of TrainingPlan)

    Protected Sub grdCategory_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdCategory.RowCommand
        If e.CommandName = "cmdDelete" Then
            Dim coursecatId As String = Convert.ToString(e.CommandArgument)
            ''Your code for deletetion is here....
            ''...

        End If
        _trainingplantemplateDAL = GetTrainigPlan()
        CategoryBindGrid(_trainingplantemplateDAL)

    End Sub
    Protected Sub lnkCourseCatID_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim x As String
    End Sub


    Protected Sub grdCategory_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdCategory.Load

        If Not IsPostBack Then
            _trainingplantemplateDAL = GetTrainigPlan()
            CategoryBindGrid(_trainingplantemplateDAL)

        End If

    End Sub

    Protected Sub CategoryBindGrid(ByVal trainingplantemplateDAL As List(Of TrainingPlan))
        Me.grdCategory.DataSource = trainingplantemplateDAL
        Me.grdCategory.DataBind()
    End Sub

    Public Function GetTrainigPlan() As List(Of TrainingPlan)
        ''This should be your DAL to full-out records
        '' Just to show records in grid...
        Dim tempRec1 As New TrainingPlan()
        tempRec1.CourseCatID = 10
        tempRec1.CourseCatName = "Computer Science"
        _trainingplantemplateDAL.Add(tempRec1)
        ''2
        Dim tempRec2 As New TrainingPlan()
        tempRec2.CourseCatID = 11
        tempRec2.CourseCatName = "BSBA Acounting"
        _trainingplantemplateDAL.Add(tempRec2)

        Return _trainingplantemplateDAL
    End Function

End Class


Please remenber to vote as answer if could help...

Regards,
Algem
 
Share this answer
 
v2
I guess because You haven't defined any datakeynames for the GridView "grdCategory" and your trying to get it.
Since grdCategory.DataKeys collection is empty and you are trying to get a value from it therefore you are getting this error.
 
Share this answer
 
Comments
janwel 9-Oct-11 23:55pm    
how can i debug it sir?
yogiCsharp 10-Oct-11 1:41am    
To solve this error you could try setting your gridview's DataKeyNames propert to CourseCatID.
Something like this below.
<asp:GridView ID="grdCategory" runat="server" AutoGenerateColumns="False" Height="190px"
Width="538px" DataKeyNames="CourseCatID" >

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900