Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

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



In this video we will discuss about ConflictDetection property of SqlDataSource control. Please watch Part 13 of asp.net gridview tutorial before proceeding. In this example, we will be using tblEmployee table that we created in Part 13.

Drag and drop a GridView, Label and a SqlDataSource datasource control on WebForm1.aspx. Flip WebForm1.aspx to source mode and change the "ID" of Label control from "Label1" to "lblMessage". Flip WebForm1.aspx to design mode.



Now let's configure "SqlDataSource1" control 
1. Right click on "SqlDataSource1" control and select "Show Smart Tag" 
2. Now click on "Configure Data Source" link
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.
4. Click Next
5. On "Configure the Select Statement" screen, select "tblEmployee" table from dropdownlist.
6. Click on "Advanced" button
7. Make sure you check CheckBoxes next to "Generate INSERT, UPDATE and DELETE statements" and "Use Optimistic Concurrency". Click OK.
8. Click Next and Finish

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

Since we have selected "Use Optimistic Concurrency" when configuring "SqlDataSource1" control, the generated "DeleteCommand" compares the row data with original values. 
DeleteCommand="DELETE FROM [tblEmployee] WHERE [EmployeeId] = @original_EmployeeId AND (([Name] = @original_Name) OR ([Name] IS NULL AND @original_Name IS NULL)) AND (([Gender] = @original_Gender) OR ([Gender] IS NULL AND @original_Gender IS NULL)) AND (([City] = @original_City) OR ([City] IS NULL AND @original_City IS NULL))"

Also, ConflictDetection property is set to "CompareAllValues"

Had we not selected "Use Optimistic Concurrency" checkbox, the generated "DeleteCommand" would have been as shown below.
DeleteCommand="DELETE FROM [tblEmployee] WHERE [EmployeeId] = @original_EmployeeId"

When Optimistic Concurrency is not enabled, ConflictDetection property is set to "OverwriteChanges".

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 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 refreshes with new data.

Now let us associate "SqlDataSource1" control with "GridView1" control
1. Right click on "GridView1" control and select "Show Smart Tag" 
2. Select "SqlDataSource1" from "Choose Data Source" dropdownlist
3. Select "Enable Deleting" checkbox. At this point "Delete" button should appear on the gridview control.

Run the application. Click "Delete" button. The row gets deleted as expected.

Now execute the following SQL statement to update "Name" from "John" to "Johny" for employee with EmployeeId=2
Update tblemployee set Name='Johny' where EmployeeId = 2

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 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 into the gridview control.

If the row data has changed, and when that row is not deleted, then I want to display a message in a label control stating - "Employee Row with EmployeeID =  2 is not deleted due to data conflict"

To achieve this, we can use "RowDeleted" event of the gridview control. This event is raised after a row is deleted from the gridview control. Now let's generate the event handler method for "RowDeleted" event.
1. Right click on the gridview control and select "Properties"
2. In the "Properties" window click on events button
3. Double click on RowDeleted event. This should generate the event handler method in the code-behind file. Copy and paste the following code. 
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
    lblMessage.Visible = true;
    // AffectedRows property will be zero, if no rows are deleted
    if (e.AffectedRows > 0)
    {
        lblMessage.Text = "Employee row with EmployeeID = \"" 
            + e.Keys[0].ToString() + "\" is successfully deleted";
        lblMessage.ForeColor = System.Drawing.Color.Navy;
    }
    else
    {
        lblMessage.Text = "Employee Row with EmployeeID = \"" 
            + e.Keys[0].ToString() + "\" is not deleted due to data conflict";
        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.

1 comment:

  1. By using following.........

    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
    lblMessage.Visible = true;
    // AffectedRows property will be zero, if no rows are deleted
    if (e.AffectedRows > 0)
    {
    lblMessage.Text = "Employee row with EmployeeID = \""
    + e.Keys[0].ToString() + "\" is successfully deleted";
    lblMessage.ForeColor = System.Drawing.Color.Navy;
    }
    else
    {
    lblMessage.Text = "Employee Row with EmployeeID = \""
    + e.Keys[0].ToString() + "\" is not deleted due to data conflict";
    lblMessage.ForeColor = System.Drawing.Color.Red;
    }
    }

    i got an exception please give me solution....
    Index was out of range. Must be non-negative and less than the size of the collection.

    ReplyDelete

It would be great if you can help share these free resources