You are on page 1of 2

8/28/2017 Sql server, .

net and c# video tutorial: ConflictDetection property of SqlDataSource control - Part 14

More Next Blog Create Blog Sign In

Sql server, .net and c# video tutorial


Free C#, .Net and Sql server video tutorial for beginners and intermediate programmers.

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

ConflictDetection property of SqlDataSource control - Part 14

Suggested Videos
Part 11 - Using stored procedures with sqldatasource control
Part 12 - Using stored procedures with objectdatasource control
Part 13 - Deleting data from gridview using sqldatasource control
Best software training and placements in
marathahalli, bangalore. For further
details please call 09945699393.

In this video we will discuss about ConflictDetection property of SqlDataSource control.


CBSE Class 9 Maths
Please watch Part 13 of asp.net gridview tutorial before proceeding. In this example, we
Number Systems
will be using tblEmployee table that we created in Part 13.

Drag and drop a GridView, Label and a SqlDataSource datasource control on CPT Tutorial
WebForm1.aspx. Flip WebForm1.aspx to source mode and change the "ID" of Label Part 1 : Video | Text | Slides
control from "Label1" to "lblMessage". Flip WebForm1.aspx to design mode.

Important Videos
The Gift of Education
Now let's configure "SqlDataSource1" control
Web application for your business
1. Right click on "SqlDataSource1" control and select "Show Smart Tag"
2. Now click on "Configure Data Source" link How to become .NET developer
3. Select connection string, from the dropdownlist on "Choose your data connection"
screen. You need to have a connection string specified in web.config file. Resources available to help you
4. Click Next
5. On "Configure the Select Statement" screen, select "tblEmployee" table from
Dot Net Video Tutorials
dropdownlist.
Angular 2 Tutorial
6. Click on "Advanced" button
7. Make sure you check CheckBoxes next to "Generate INSERT, UPDATE and
Design Patterns
DELETE statements" and "Use Optimistic Concurrency". Click OK.
8. Click Next and Finish ASP.NET Web API

Now flip "WebForm1.aspx" to "HTML Source" mode. Notice that, the wizard has Bootstrap
automatically generated INSERT, UPDATE and DELETE statements. Since, we only
want to enable the gridview control to delete data, get rid of InsertCommand, AngularJS Tutorial
InsertParameters, UpdateCommand, and UpdateParameters.
jQuery Tutorial

Since we have selected "Use Optimistic Concurrency" when configuring


JavaScript with ASP.NET Tutorial
"SqlDataSource1" control, the generated "DeleteCommand" compares the row data
with original values. JavaScript Tutorial
DeleteCommand="DELETE FROM [tblEmployee] WHERE [EmployeeId] =
@original_EmployeeId AND (([Name] = @original_Name) OR ([Name] IS NULL AND Charts Tutorial
@original_Name IS NULL)) AND (([Gender] = @original_Gender) OR ([Gender] IS
NULL AND @original_Gender IS NULL)) AND (([City] = @original_City) OR ([City] IS LINQ
NULL AND @original_City IS NULL))"
LINQ to SQL

Also, ConflictDetection property is set to "CompareAllValues".


LINQ to XML

Had we not selected "Use Optimistic Concurrency" checkbox, the generated Entity Framework
"DeleteCommand" would have been as shown below.

http://csharp-video-tutorials.blogspot.in/2013/03/conflictdetection-property-of.html 1/3
8/28/2017 Sql server, .net and c# video tutorial: ConflictDetection property of SqlDataSource control - Part 14
DeleteCommand="DELETE FROM [tblEmployee] WHERE [EmployeeId] = WCF
@original_EmployeeId"
ASP.NET Web Services
When Optimistic Concurrency is not enabled, ConflictDetection property is set to
Dot Net Basics
"OverwriteChanges".
C#
So, when optimistic concurrency option is used, and when we try to delete or update a
row thru gridview control, then all the columns of the row that is being deleted are SQL Server
compared to check if the data has changed since the row was loaded into the gridview
control. If data has changed, the row will not be deleted, and the gridview control simply ADO.NET
refreshes with new data.
ASP.NET
Now let us associate "SqlDataSource1" control with "GridView1" control
GridView
1. Right click on "GridView1" control and select "Show Smart Tag"
2. Select "SqlDataSource1" from "Choose Data Source" dropdownlist ASP.NET MVC
3. Select "Enable Deleting" checkbox. At this point "Delete" button should appear on the
gridview control. Visual Studio Tips and Tricks

Run the application. Click "Delete" button. The row gets deleted as expected. Dot Net Interview Questions

Now execute the following SQL statement to update "Name" from "John" to "Johny" for Slides
employee with EmployeeId=2
Entity Framework
Update tblemployee set Name='Johny' where EmployeeId = 2
WCF
At this point in the gridview control, row with EmployeeId=2, still shows the name as
"John". Now, click Delete button in this row. Notice that, the row is not deleted and the ASP.NET Web Services
grid gets refreshed with new data. This is because, we are using optmistic concurrency
option and the data for this row has changed in the database table, after it was loaded Dot Net Basics
into the gridview control.
C#
If the row data has changed, and when that row is not deleted, then I want to display a
SQL Server
message in a label control stating - "Employee Row with EmployeeID = 2 is not deleted
due to data conflict" ADO.NET

To achieve this, we can use "RowDeleted" event of the gridview control. This ASP.NET
event is raised after a row is deleted from the gridview control. Now let's generate the
event handler method for "RowDeleted" event. GridView
1. Right click on the gridview control and select "Properties"
ASP.NET MVC
2. In the "Properties" window click on events button
3. Double click on RowDeleted event. This should generate the event handler method in
Visual Studio Tips and Tricks
the code-behind file. Copy and paste the following code.
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{ Java Video Tutorials
lblMessage.Visible = true; Part 1 : Video | Text | Slides
// AffectedRows property will be zero, if no rows are deleted
if (e.AffectedRows > 0) Part 2 : Video | Text | Slides
{
Part 3 : Video | Text | Slides
lblMessage.Text = "Employee row with EmployeeID = \""
+ e.Keys[0].ToString() + "\" is successfully deleted";
lblMessage.ForeColor = System.Drawing.Color.Navy; Interview Questions
} C#
else
{ SQL Server
lblMessage.Text = "Employee Row with EmployeeID = \""
+ e.Keys[0].ToString() + "\" is not deleted due to data conflict"; Written Test
lblMessage.ForeColor = System.Drawing.Color.Red;
}
}

Run the application. After the data is loaded into gridview control, update any row in the
database table, and try to delete that, same row in the gridview control. You should get
the data conflict message as expected.

http://csharp-video-tutorials.blogspot.in/2013/03/conflictdetection-property-of.html 2/3

You might also like