Keep gridview in edit mode when update fails due to data conflict - Part 20
Автор: kudvenkat
Загружено: 2013-03-10
Просмотров: 30188
Link for csharp, asp.net, ado.net, dotnet basics and sql server video tutorial playlists
/ kudvenkat
Link for text version of this video
http://csharp-video-tutorials.blogspo...
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
/ @aarvikitchen5572
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.
On the other hand if there are data conflicts, and if the update fails, then we want to display failure status message. 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: Use "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.
Step 3: Use "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.
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.
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: