Tim A
Tim A

Reputation: 19

The GridView ' ' fired event RowUpdating which wasn't handled. C# code behind asp.net

There are similar questions on Stackoverflow and other websites, but I seem to miss something.

I have a GridView bound to a DataTable which comes from a database. My goal is to update the one row at the time with a button which is in the same row which calls the following Method.

        protected void TestsToBeDone_RowUpdating(object sender, GridViewEditEventArgs e)
    {
        SqlConnection myConnection = getConnection();
        myConnection.Open();

        int index = Convert.ToInt32(e.NewEditIndex);

        GridViewRow selectedRow = TestsToBeDone.Rows[index];
        TableCell LABAVIDc = selectedRow.Cells[1];
        int LABAVID = Convert.ToInt32(LABAVIDc.Text);

        TableCell assistentc = selectedRow.Cells[5];

        string query = "UPDATE Labaanvraag " +
                       "SET Status='4' "+
                                 "WHERE LABAVID ='"+LABAVID+"'";
        }

        SqlCommand commandZ = new SqlCommand(query, myConnection);
        commandZ.ExecuteNonQuery();            

        myConnection.Close();

        SetPatientTestGrid(Session["PATID"], e);
    }

It is being called from here:

    <asp:GridView ID="TestsToBeDone" runat="server" EnableModelValidation="True" OnRowEditing="TestsToBeDone_RowUpdating">
    <Columns>
        <asp:ButtonField ButtonType="Button" CommandName="Update" 
            HeaderText="Afgenomen" ShowHeader="True" Text="Afgenomen" />
    </Columns>
</asp:GridView>

At my first try I had a OnRowCommand with the GridViewCommandEventArgs at the code behind.

But it still throws the same exception, although I've read on a couple of sites that changing it to OnRowEditing and GridViewEditEventArgs would solve the problem.

Thanks in advance,

Tim A

Upvotes: 1

Views: 20118

Answers (2)

Hari
Hari

Reputation: 1

If you are using the Row_Command event to edit, then don't use the button command name "Update" for Update and "Delete" for Delete. Instead, use Edit or UP and Del respectively.

Upvotes: 0

Dave Zych
Dave Zych

Reputation: 21897

GridView's in ASP.NET have some built in commands - Deleted, Update, etc. What's happening is that your button has a command name of Update, and the Gridview is hijacking that from you and firing a RowUpdating event instead of your RowEditing event. Change your button command name to be Edit, and use a RowEditing event.

Upvotes: 7

Related Questions