[Solved] ASP.Net Error: The GridView GridView1 fired event RowUpdating which wasnt handled

indradeo
 
on Oct 19, 2021 04:38 AM
386 Views

Server Error in '/' Application.


The GridView 'GridView1' fired event RowUpdating which wasn't handled.

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.Web.HttpException: The GridView 'GridView1' fired event RowUpdating which wasn't handled. Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4330.0

namespace OCMS
{
    public partial class comp_mngt : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(@"Data Source=(localdb)\Projects;Initial Catalog=OCMS;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        // This method is used to bind gridview from database
        private void BindGrid()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
            //SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM comp_box order by COMP_LODGE_DATE DESC ", con);
            SqlDataAdapter sda = new SqlDataAdapter("select * from comp_box,emp where closing_date IS NULL and comp_box.emp_id=emp.emp_id order by comp_lodge_date desc", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string STATUS_ID = (GridView1.Rows[e.RowIndex].FindControl("DropDownList2") as DropDownList).SelectedItem.Value;
            string ATTEND_BY = (GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList).SelectedItem.Value;
            string REMARKS = (GridView1.Rows[e.RowIndex].FindControl("TextBox1") as DropDownList).SelectedItem.Value;
            string COMP_ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                string query = "update comp_box set ATTEND_BY = @DropDownList1,REMARKS=@REMARKS,STATUS_ID=@DropDownList2 where COMP_ID = @COMP_ID";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@DropDownList2", STATUS_ID);
                    cmd.Parameters.AddWithValue("@DropDownList1", ATTEND_BY);
                    cmd.Parameters.AddWithValue("@REMARKS", REMARKS);
                    cmd.Parameters.AddWithValue("@COMP_ID", COMP_ID);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    //Response.Redirect(Request.Url.AbsoluteUri);
                }
            }
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindGrid();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindGrid();
        }
    }
}
   

 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    BackColor="White" Width="1050px" BorderColor="#CCCCCC" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowDataBound="GridView1_RowDataBound"
    BorderStyle="None" BorderWidth="1px" CellPadding="3" CssClass="auto-style16" Height="170px">
    <Columns>
        <asp:TemplateField HeaderText = "SL.No." ItemStyle-Width="10">
                <ItemTemplate>
                    <asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Comp.Id">
            <ItemTemplate>
                <asp:Label ID="lblCOMP_ID" runat="server" Text='<%# Eval("COMP_ID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>              
    <FooterStyle BackColor="White" ForeColor="#000066" />
    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
    <RowStyle ForeColor="#000066" />
    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#007DBB" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Oct 19, 2021 04:50 AM
on Oct 19, 2021 05:00 AM

Dear Indradeo, 

  As i can see you haven't declared OnRowUpdating event kindly refer below modified code.

 HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowUpdating="GridView1_RowDataBound"
    BackColor="White" Width="1050px" BorderColor="#CCCCCC" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowDataBound="GridView1_RowDataBound"
    BorderStyle="None" BorderWidth="1px" CellPadding="3" CssClass="auto-style16" Height="170px">