[Solved] ASP.Net Error: Unable to cast object of type System.EventArgs to type System.Web.UI.WebControls.GridViewRowEventArgs

makumbi
 
on Jul 25, 2022 04:18 AM
604 Views

Please help i am try to generate an editable GridView but getting error below

Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.WebControls.GridViewRowEventArgs'.

 

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TeachersInitials.aspx.vb" Inherits="SMIS2022WEB.TeachersInitials" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Panel ID="Panel1" runat="server" Height="316px" Width="721px">
            <table class="auto-style1">
                <tr>
                    <td class="auto-style2"><strong>Class</strong></td>
                    <td class="auto-style3">
                        <asp:DropDownList ID="Classddl" runat="server" AutoPostBack="True" DataSourceID="clrd" DataTextField="CLASS" DataValueField="CLASS">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="clrd" runat="server" ConnectionString="<%$ ConnectionStrings:SMIS2022ConnectionString %>" SelectCommand="SELECT [CLASS] FROM [CLASS]"></asp:SqlDataSource>
                    </td>
                    <td class="auto-style2">
                        <asp:Button ID="Button1" runat="server" Text="Save Initials" />
                    </td>
                </tr>
                <tr>
                    <td colspan="3">
                        <asp:GridView ID="TrapedlistGrd" runat="server" AllowPaging="True" AutoGenerateColumns="False" PageSize="20" OnDataBound="OnRowDataBound">
                            <Columns>
                                <asp:TemplateField HeaderText="Subject">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="Subject" runat="server" AutoPostBack="True" DataSourceID="Subjectr" DataTextField="subject" DataValueField="subject">
                                        </asp:DropDownList>
                                        <asp:SqlDataSource ID="Subjectr" runat="server" ConnectionString="<%$ ConnectionStrings:SMIS2022ConnectionString %>" SelectCommand="SELECT [subject] FROM [subjects]"></asp:SqlDataSource>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Initials">
                                    <ItemTemplate>                                   
                                         <asp:TextBox ID="initials" runat="server" Text='<%# Eval("Initials") %>'></asp:TextBox>                                   
                                         </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Class">
                                    <ItemTemplate>  
                                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                                        </asp:DropDownList>
                                        <asp:Label ID="Lclasses" runat="server" Text='<%# Eval("class") %>' Visible="False"></asp:Label>  
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Stream">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="Streamddl" runat="server" AutoPostBack="True" DataSourceID="strdd" DataTextField="Stream" DataValueField="Stream">
                                        </asp:DropDownList>
                                        <asp:SqlDataSource ID="strdd" runat="server" ConnectionString="<%$ ConnectionStrings:SMIS2022ConnectionString %>" SelectCommand="SELECT [Stream] FROM [Streamdata]"></asp:SqlDataSource>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Type">
                                    <ItemTemplate>
                                        <asp:TextBox ID="Type" runat="server" Text='<%# Eval("type") %>'></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Full Names">
                                    <ItemTemplate>                                      
                                         <asp:TextBox ID="Fullnames" runat="server" Text='<%# Eval("fulnames") %>'></asp:TextBox>                                        
                                         </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </asp:Panel>    
    </div>
    </form>
</body>
</html>

 

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class TeachersInitials
    Inherits System.Web.UI.Page
    Dim constr As String = ConfigurationManager.ConnectionStrings("SMIS2022ConnectionString").ConnectionString

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            BindGrid()
        End If
    End Sub
    Private Sub BindGrid()
        Dim constr As String = ConfigurationManager.ConnectionStrings("SMIS2022ConnectionString").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("SELECT Subject, class, stream, Initials, Type, Fulnames FROM Trapedlist  where class = '" & "P1" & "'", con)
                Using sda As New SqlDataAdapter()
                    cmd.Connection = con
                    sda.SelectCommand = cmd
                    Using dt As New DataTable()
                        sda.Fill(dt)
                        TrapedlistGrd.DataSource = dt
                        TrapedlistGrd.DataBind()
                    End Using
                End Using
            End Using
        End Using
    End Sub

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shivam.M
 
on Jul 25, 2022 07:00 AM
on Jul 25, 2022 07:11 AM
makumbi says:
<asp:GridView ID="TrapedlistGrd" runat="server" AllowPaging="True" AutoGenerateColumns="False" PageSize="20" OnDataBound="OnRowDataBound">

You are using OnDataBound which is wrong, Please use OnRowDataBound event instead of OnDataBound