Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Keep gridview in edit mode when update fails due to data conflict - Part 20

Suggested Videos 
Part 17 - Editing and updating data in gridview using sqldatasource control
Part 18 - Editing and updating data in gridview using objectdatasource control
Part 19 - Using optimistic concurrency when editing data in gridview



In this video, we will discuss about 
1. Diplaying appropriate status messages depending on the success of update.
2. If the update fails, leave the row in "Edit" mode.



Please watch Part 19 of asp.net gridview tutorial before proceeding. 

If there are no concurrency conflicts and if the update succeeds, then we want to display successful status message as shown below.


On the other hand if there are data conflicts, and if the update fails, then we want to display failure status message as shown below. At the same time, we also want to retain the row in EDIT mode.


There are 3 steps to achieve this
Step 1: In EmployeeDataAccessLayer class, change the return type of UpdateEmployee() method from "void" to "int"
public static int UpdateEmployee(int original_EmployeeId, string original_Name, string original_Gender, string original_City, string Name, string Gender, string City)

ExecuteNonQuery() method of the command object, returns an integer indicating the number of rows updated. We want our UpdateEmployee() method to return this value. 
So, instead of "cmd.ExecuteNonQuery()" use "return cmd.ExecuteNonQuery()"

Step 2: Copy and paste the following code in the "Updated" event handler of "ObjectDataSource1" control. This event is fired after the "Update" operation has completed, that is, after the execution of UpdateEmployee() method. Remember UpdateEmployee() method returns an integer back. Notice that we are checking if the "ReturnValue" is an integer. If it is an integer and greater than zero, then we are using the "RetunValue"(the value that is returned by UpdateEmployee() method) to initialize "AffectedRows" property. This "AffectedRows" property will be used in "GridView1_RowUpdated" event handler.
protected void ObjectDataSource1_Updated(object sender, ObjectDataSourceStatusEventArgs e)
{
    if (e.ReturnValue is int && (int)e.ReturnValue > 0)
    {
        e.AffectedRows = (int)e.ReturnValue;
    }
}

Step 3: Copy and paste the following code in the "RowUpdated" event handler of "GridView1" control. Notice that we are using "AffectedRows" property to determine, if the update has succeeded or not. Remember this is the property that we initialized in Step 2.
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
    if (e.AffectedRows < 1)
    {
        e.KeepInEditMode = true;
        lblMessage.Text = "Row with EmployeeId = " + e.Keys[0].ToString() +
            " is not update due to data conflict";
        lblMessage.ForeColor = System.Drawing.Color.Red;
    }
    else
    {
        lblMessage.Text = "Row with EmployeeId = " + e.Keys[0].ToString() +
            " is successfully updated";
        lblMessage.ForeColor = System.Drawing.Color.Navy;
    }
}

To make things clear
1. First UpdateEmployee() method gets executed, and this method returns an integer value indicating the number of rows updated.
2. Then "Updated" event of "ObjectDataSource1" control is invoked. In this event handler method, we retrieve the value returned by "UpdateEmployee()" and then use it to initialize "AffectedRows" property.
3. Finally "RowUpdated" event of "GridView1" control is invoked. In this event handler method, we use "AffectedRows" property to determine, if the update has succeeded or not.

Make sure to include a "Label" control on your webform with ID=lblMessage.

1 comment:

  1. e.KeepInEditMode = true; when I am Using this I am not able to Update exact record if Sorting is aaplied on that Field . For Example ,... I am updating name "Vanket" and Some one change "Vanket" to "Ahmed" .. Then By Sorting Ahmed Goes Upward but edit index not change according to record after Sorting ... please Help.
    Thanks For this Great Tutorial.

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.