About Me

Professional Practical HumanBeing

Saturday, December 4, 2010

DataRowState Enumeration:-


The DataRowState enumeration is returned by the RowState property of the DataRow class.


Member nameDescription
Supported by the .NET Compact FrameworkAddedThe row has been added to a DataRowCollection, andAcceptChanges has not been called.
Supported by the .NET Compact FrameworkDeletedThe row was deleted using the Delete method of the DataRow.
Supported by the .NET Compact FrameworkDetachedThe row has been created but is not part of anyDataRowCollection. A DataRow is in this state immediately after it has been created and before it is added to a collection, or if it has been removed from a collection.
Supported by the .NET Compact FrameworkModifiedThe row has been modified and AcceptChanges has not been called.
Supported by the .NET Compact FrameworkUnchangedThe row has not changed since AcceptChanges was last called.

DataRow Class :-

The DataRow and DataColumn objects are primary components of a DataTable. Use theDataRowobject and its properties and methods to retrieve and evaluate; and insert, delete, and update the values in the DataTable. The DataRowCollection represents the actual DataRow objects in theDataTable, and the DataColumnCollection contains the DataColumn objects that describe the schema of the DataTable. Use the overloaded Item property to return or set the value of aDataColumn.

Use the HasVersion and IsNull properties to determine the status of a particular row value, and theRowState property to determine the state of the row relative to its parent DataTable.

To create a new DataRow, use the NewRow method of the DataTable object. After creating a newDataRow, use the Add method to add the new DataRow to the DataRowCollection. Finally, call theAcceptChanges method of the DataTable object to confirm the addition. For more information about adding data to a DataTable, see Adding Data to a Table.

You can delete a DataRow from the DataRowCollection by calling the Remove method of theDataRowCollection, or by calling the Delete method of the DataRow object. The Removemethod removes the row from the collection. In contrast, Delete marks the DataRow for removal. The actual removal occurs when you call AcceptChanges method. By calling Delete, you can programmatically check which rows are marked for removal before actually deleting them. For more information, seeDeleting a Row from a Table.


Deleting Row from a Table :-

There are two methods you can use to delete a DataRow object from a DataTable object: theRemovemethod of the DataRowCollection object, and the Delete method of the DataRow object. Whereas theRemove method deletes a DataRow from the DataRowCollection, the Delete method only marks the row for deletion. The actual removal occurs when the application calls theAcceptChanges method. By using Delete, you can programmatically check which rows are marked for deletion before actually removing them. When a row is marked for deletion, its RowState property is set to Deleted.

When using a DataSet or DataTable in conjunction with a DataAdapter and a relational data source, use the Delete method of the DataRow to remove the row. The Delete method marks the row asDeleted in the DataSet or DataTable but does not remove it. Instead, when theDataAdapterencounters a row marked as Deleted, it executes its DeleteCommand method to delete the row at the data source. The row can then be permanently removed using theAcceptChanges method. If you use Remove to delete the row, the row is removed entirely from the table, but the DataAdapter will not delete the row at the data source.

If a row is marked for deletion and you call the AcceptChanges method of the DataTable object, the row is removed from the DataTable. In contrast, if you call RejectChanges, the RowState of the row reverts to what it was before being marked as Deleted.

Note:-

If the RowState of a DataRow is Added, meaning it has just been added to the table, and it is then marked as Deleted, it is removed from the table

Difference between Delete and Remove in ADO.Net (DataTable):-

Delete is a Soft Delete( you can roll back,since the row will be just marked for deletion)

Remove is a Hard Delete, you will not be able to roll back the datarow

The Delete method performs only a logical deletion by marking the row as Deleted. Hence when the Dataset batch update is done the row is removed from the datasource.

The Remove method, instead, physically removes the row from the Rows collection. As a result, a row deleted through Remove is not marked for deletion and subsequently not processed during batch update.

The basic rule of the thumb to be followed is this, if the goal of your deletion is removing the row from the data source, then use Delete

No comments:

Post a Comment