ADO - ADO.NET DataRowState and DataRowVersion for Tracking Record Changes
1. Introduction
ADO.NET provides several classes and properties to manage data retrieved from databases. When an application retrieves records from a database and stores them in a DataTable, users may perform different operations such as adding new records, modifying existing records, or deleting records.
To manage these changes effectively, ADO.NET provides two important concepts: DataRowState and DataRowVersion.
DataRowState identifies the current state of a particular row in a DataTable. It helps developers determine whether a row is newly added, modified, deleted, unchanged, or detached from the table.
DataRowVersion represents different versions of the data stored in a DataRow. It allows developers to access the original values, current values, and proposed values of a record during data editing.
Together, these concepts help applications track changes, maintain data consistency, and update database records accurately.
2. Understanding DataRowState
DataRowState is an enumeration available in ADO.NET that indicates the current condition of a DataRow object.
Whenever a record is added, modified, deleted, or accepted into a DataTable, its DataRowState changes according to the operation performed.
The DataRowState enumeration contains five important values:
1. Added
The Added state indicates that a new row has been inserted into a DataTable but has not yet been accepted as part of the original data.
For example, consider a student management application where a developer inserts a new student record into a DataTable. The newly inserted row receives the Added state.
Example:
DataTable dt = new DataTable();
dt.Columns.Add("StudentID", typeof(int));
dt.Columns.Add("StudentName", typeof(string));
DataRow row = dt.NewRow();
row["StudentID"] = 101;
row["StudentName"] = "Rahul";
dt.Rows.Add(row);
Console.WriteLine(row.RowState);
Output:
Added
This indicates that the row is newly inserted and has not yet been accepted through AcceptChanges().
2. Modified
The Modified state indicates that an existing row has been changed after it was loaded into a DataTable or after its changes were accepted.
For example, suppose a student's name is stored as Rahul. If the application changes the name to Rohan, the row enters the Modified state.
Example:
DataTable dt = new DataTable();
dt.Columns.Add("StudentID", typeof(int));
dt.Columns.Add("StudentName", typeof(string));
dt.Rows.Add(101, "Rahul");
dt.AcceptChanges();
dt.Rows[0]["StudentName"] = "Rohan";
Console.WriteLine(dt.Rows[0].RowState);
Output:
Modified
The Modified state helps developers identify records that require database updates.
3. Deleted
The Deleted state indicates that an existing row has been marked for deletion from a DataTable.
When the Delete() method is called, the row is marked as deleted but may remain available internally until the changes are accepted.
Example:
DataTable dt = new DataTable();
dt.Columns.Add("StudentID", typeof(int));
dt.Columns.Add("StudentName", typeof(string));
dt.Rows.Add(101, "Rahul");
dt.AcceptChanges();
dt.Rows[0].Delete();
Console.WriteLine(dt.Rows[0].RowState);
Output:
Deleted
A deleted row can still provide access to its original values through DataRowVersion. However, accessing its Current version is not possible because the row has been marked for deletion.
4. Unchanged
The Unchanged state indicates that a row has not been modified since it was loaded into the DataTable or since AcceptChanges() was last called.
For example, when records are retrieved from a database, they are generally marked as Unchanged after being loaded.
Example:
DataTable dt = new DataTable();
dt.Columns.Add("StudentID", typeof(int));
dt.Columns.Add("StudentName", typeof(string));
dt.Rows.Add(101, "Rahul");
dt.AcceptChanges();
Console.WriteLine(dt.Rows[0].RowState);
Output:
Unchanged
This state helps developers identify records that do not require any database updates.
5. Detached
The Detached state indicates that a DataRow object is not currently part of a DataTable.
A row created using the NewRow() method initially remains in the Detached state. After adding it to the DataTable, its state changes to Added.
Example:
DataTable dt = new DataTable();
dt.Columns.Add("StudentID", typeof(int));
dt.Columns.Add("StudentName", typeof(string));
DataRow row = dt.NewRow();
Console.WriteLine(row.RowState);
Output:
Detached
This state is useful when creating and preparing new records before inserting them into a DataTable.
3. Understanding DataRowVersion
DataRowVersion is another important enumeration in ADO.NET. It allows developers to access different versions of a record stored in a DataRow.
When a record is modified, the original values are preserved so that developers can compare them with the updated values.
DataRowVersion contains four important values.
1. Original
The Original version represents the values stored in a row before the most recent changes were accepted.
For example, suppose a student's original name is Rahul. If the application changes the name to Rohan, the Original version continues to contain Rahul.
Example:
DataTable dt = new DataTable();
dt.Columns.Add("StudentID", typeof(int));
dt.Columns.Add("StudentName", typeof(string));
dt.Rows.Add(101, "Rahul");
dt.AcceptChanges();
dt.Rows[0]["StudentName"] = "Rohan";
Console.WriteLine(
dt.Rows[0]["StudentName", DataRowVersion.Original]
);
Output:
Rahul
The Original version is especially useful when comparing database values with modified application values.
2. Current
The Current version represents the latest values stored in a DataRow.
If a record is modified, the Current version contains the updated values.
Example:
Console.WriteLine(
dt.Rows[0]["StudentName", DataRowVersion.Current]
);
Output:
Rohan
The Current version is commonly used when retrieving updated values before saving them to a database.
3. Proposed
The Proposed version represents values that are being edited but have not yet been committed to the DataRow.
This version is available during the BeginEdit() and EndEdit() editing process.
Example:
DataRow row = dt.Rows[0];
row.BeginEdit();
row["StudentName"] = "Arun";
Console.WriteLine(
row["StudentName", DataRowVersion.Proposed]
);
row.EndEdit();
Output:
Arun
The Proposed version helps developers examine temporary changes before committing them.
If the editing operation is cancelled using CancelEdit(), the proposed changes are discarded.
4. Default
The Default version represents the default version of a row for the current state of that row.
For most ordinary situations, it provides the Current version. However, its behaviour depends on the row's state and the available versions.
Developers can use this version when they want ADO.NET to select the appropriate default data version automatically.
4. Difference Between DataRowState and DataRowVersion
Although DataRowState and DataRowVersion work together, they perform different functions.
|
DataRowState |
DataRowVersion |
|---|---|
|
Identifies the state of a row. |
Identifies the version of data in a row. |
|
Tracks whether a row is added, modified, deleted, unchanged, or detached. |
Provides access to original, current, proposed, or default values. |
|
Used to identify the type of change performed. |
Used to compare different values of a record. |
|
Available through the RowState property. |
Used through the DataRowVersion enumeration. |
|
Helps determine which database operation is required. |
Helps determine which data values should be used. |
For example, if a student's name changes from Rahul to Rohan, the DataRowState becomes Modified. At the same time, DataRowVersion.Original contains Rahul, while DataRowVersion.Current contains Rohan.
5. Using DataRowState and DataRowVersion Together
These two concepts are especially useful when an application needs to identify changes and prepare database updates.
Consider a student management system that stores student information in a DataTable.
Initially, the table contains the following information:
|
Student ID |
Student Name |
Row State |
|---|---|---|
|
101 |
Rahul |
Unchanged |
|
102 |
Priya |
Unchanged |
Suppose the application performs the following operations:
-
Changes Rahul's name to Rohan.
-
Adds a new student named Arun.
-
Deletes the record of Priya.
After these operations, the table contains the following logical changes:
|
Student ID |
Student Name |
Row State |
|---|---|---|
|
101 |
Rohan |
Modified |
|
102 |
Priya |
Deleted |
|
103 |
Arun |
Added |
Developers can inspect these changes using the RowState property.
Example:
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("State: " + row.RowState);
if (row.RowState == DataRowState.Modified)
{
Console.WriteLine(
"Old Name: " +
row["StudentName", DataRowVersion.Original]
);
Console.WriteLine(
"New Name: " +
row["StudentName", DataRowVersion.Current]
);
}
}
This program checks every row and displays its state. If a row has been modified, it displays both the original and updated values.
For a deleted row, developers can retrieve its original values using DataRowVersion.Original.
6. Importance of AcceptChanges() and RejectChanges()
Two important DataTable methods are closely related to DataRowState and DataRowVersion.
AcceptChanges():
The AcceptChanges() method accepts all modifications made to the DataTable.
After calling this method:
-
Added rows become Unchanged.
-
Modified rows become Unchanged.
-
Deleted rows are permanently removed from the DataTable.
-
Original values are updated to reflect the accepted values.
Example:
dt.AcceptChanges();
RejectChanges():
The RejectChanges() method cancels pending changes and restores the DataTable to its previously accepted state.
For example, if a student's name changes from Rahul to Rohan, calling RejectChanges() restores the original name Rahul.
Example:
dt.RejectChanges();
An important point is that AcceptChanges() does not automatically save changes to the database. It only updates the change-tracking state within the DataTable. Database updates must be performed separately, usually through a DataAdapter or database commands.
7. Real-World Applications
DataRowState and DataRowVersion are useful in several types of software applications.
Student Management Systems: Educational applications use these concepts to identify newly registered students, updated student details, and deleted student records.
Banking Applications: Banking software can track modifications to customer details and compare previous information with updated information.
Employee Management Systems: Human resource applications can identify changes in employee names, salaries, departments, and contact information.
Inventory Management Systems: Inventory applications can track newly added products, modified stock quantities, and deleted product records.
Business Applications: Enterprise software uses these features to manage changes in customer records, orders, invoices, and other business information.
8. Advantages of DataRowState and DataRowVersion
-
Efficient Change Tracking: Developers can easily identify which records have been added, modified, or deleted.
-
Data Comparison: Original and Current versions make it possible to compare old and updated values.
-
Improved Data Consistency: Applications can identify changes before sending updates to a database.
-
Better Error Handling: Developers can examine previous values when validating or correcting records.
-
Simplified Database Updates: These concepts help applications determine whether an INSERT, UPDATE, or DELETE operation is required.
-
Support for Data Editing: The Proposed version allows developers to work with temporary values before committing changes.
9. Conclusion
DataRowState and DataRowVersion are important features of ADO.NET that help developers manage and track changes in tabular data. DataRowState identifies the current condition of a row, while DataRowVersion provides access to different versions of its values.
By combining these features, developers can identify newly added records, track modifications, manage deleted records, and compare original values with updated values. These capabilities are particularly useful in database-driven applications where accurate data management and consistency are essential.