Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am running the code below and getiing an exception "Unable to use object of type System.EventArgs to type GridRowEventArgs" on the gvAvailability.DataBind. I am looking for info on this and am not finding that much. Tried of looking for solution for three days. Tried solution #1 and still same exception keeps coming.

My vb.net code:
VB
Public Sub BindGridView()
        Try
            Dim objConnection As SqlConnection
            Dim ObjReader As SqlDataReader
            Dim com As New SqlCommand

            Dim dAvailabilityDate As Date
            Dim dStartTime As Date
            Dim dEndTime As Date
            Dim dNotes As String = ""

            ' Creating columns for the table
            Dim dc As New DataColumn("AvailabilityDate")
            dtAvailability.Columns.Add(dc)

            dc = New DataColumn("StartTime")
            dtAvailability.Columns.Add(dc)

            dc = New DataColumn("EndTime")
            dtAvailability.Columns.Add(dc)

            dc = New DataColumn("Notes")
            dtAvailability.Columns.Add(dc)

            Dim dSelectedStartDate As Date = CDate(lblSelectedStartDate.Text)
            Dim dSelectedEndDate As Date = CDate(lblSelectedEndDate.Text)

            objConnection = New SqlConnection(ConnectionStringFansilDB)
            objConnection.Open()

            com.CommandText = "SELECT * FROM TBL_EmployeeAvailability WHERE EmployeeID=@employeeID AND AvailabilityDate BETWEEN @dSelectedStartDate AND @dSelectedEndDate"
            com.Parameters.Add("@employeeID", SqlDbType.Int).Value = employeeID
            com.Parameters.Add("@dSelectedStartDate", SqlDbType.DateTime).Value = dSelectedStartDate
            com.Parameters.Add("@dSelectedEndDate", SqlDbType.DateTime).Value = dSelectedEndDate
            com.Parameters.Add("@dNote", SqlDbType.VarChar).Value = dNotes


            com.Connection = objConnection

            ObjReader = com.ExecuteReader

            If ObjReader IsNot Nothing Then
                If ObjReader.HasRows Then
                    While ObjReader.Read

                        If Not DBNull.Value.Equals(ObjReader.GetValue(2)) Then dAvailabilityDate = ObjReader.GetValue(2)
                        If Not DBNull.Value.Equals(ObjReader.GetValue(3)) Then dStartTime = ObjReader.GetValue(3)
                        If Not DBNull.Value.Equals(ObjReader.GetValue(4)) Then dEndTime = ObjReader.GetValue(4)
                        If Not DBNull.Value.Equals(ObjReader.GetValue(5)) Then dNotes = ObjReader.GetValue(5)

                        Dim dr As DataRow = dtAvailability.NewRow()

                        dr("AvailabilityDate") = Format(dAvailabilityDate, "dd/mm/yyyy")
                        dr("StartTime") = dStartTime.ToString("hh:mm tt")
                        dr("EndTime") = dEndTime.ToString("hh:mm tt")
                        dr("Notes") = dNotes

                        dtAvailability.Rows.Add(dr)

                    End While

                End If

                ObjReader.Close()
            End If

            If Not objConnection.State = ConnectionState.Closed Then
                objConnection.Close()
            End If

            gvAvailability.DataSource = dtAvailability.DefaultView
            gvAvailability.DataBind()

        Catch ex As Exception
            MsgBox("Error loading Employee's Available Time: " & ex.Message)

        End Try

    End Sub

And my DataGridView
XML
<asp:GridView ID="gvAvailability" runat="server" AutoGenerateColumns="True"
                        PageSize="50"  AllowSorting="True" ShowFooter="True"
                         BackColor="White" BorderColor="#CC9966" BorderStyle="None"
                         BorderWidth="1px" CellPadding="4" AutoGenerateEditButton="True"
                         AutoGenerateDeleteButton="True" EmptyDataText="No records found!"
                        EnableSortingAndPagingCallbacks="True" GridLines="None">

                        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                        <RowStyle BackColor="#FFFFFF" ForeColor="#330099" />
                        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                     <Columns>

                         <asp:BoundField DataField="AvailabiltyDate" HeaderText="Availability Date"
                             ReadOnly="True" />
                         <asp:BoundField DataField="Start Time" HeaderText="Start Time" />
                         <asp:BoundField DataField="End Time" HeaderText="End Time" />
                         <asp:BoundField DataField="Note" HeaderText="Note" />

            </Columns>
                    </asp:GridView>
Posted
Updated 27-Sep-13 7:39am
v4
Comments
Abdul Quader Mamun 27-Sep-13 0:11am    
Have you bind any event to GridView?
Alla Bernshtein 27-Sep-13 13:21pm    
no,I have not. I had them before but I erased thinking it'll help.

Rather than getting records in the DataReader object & then transferring it to Datatable, why don't you populate the Datatable directly & bind it to the grid?
 
Share this answer
 
Instead of
VB
gvAvailability.DataSource = dtAvailability.DefaultView

Do
VB
gvAvailability.DataSource = dtAvailability


Another problem is the DataTable Column names should match with the GridView BoundField's DataField Property.

So, instead of
XML
<asp:BoundField DataField="AvailabiltyDate" HeaderText="Availability Date"
                            ReadOnly="True" />
<asp:BoundField DataField="Start Time" HeaderText="Start Time" />
<asp:BoundField DataField="End Time" HeaderText="End Time" />
<asp:BoundField DataField="Note" HeaderText="Note" />

Do
XML
<asp:BoundField DataField="AvailabilityDate" HeaderText="Availability Date"
                            ReadOnly="True" />
<asp:BoundField DataField="StartTime" HeaderText="Start Time" />
<asp:BoundField DataField="EndTime" HeaderText="End Time" />
<asp:BoundField DataField="Notes" HeaderText="Note" />
 
Share this answer
 
v2
I removed handles gvAvailability.DataBinding from the initial row sub RowDataBound.
 
Share this answer
 

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