Thursday, 27 June 2013

Editing Disconnected Data

Editing Disconnected Data
The various objects in ADO.NET that compose the disconnected part of ADO.NET  , have  ability to make edits or modifications to themselves.

there are mainly three kinds of modification  operations that can  be  perform on disconnected data:
  • Add new rows
  • Modify existing rows
  • Delete existing rows
Add New Rows
There are two ways to add a new row to a DataTable, and two ways to add existing rows (from
other DataTables or Detached rows) to a DataTable.

1)The first way assumes thate we have a schema or at least  a structure preloaded to the DataTable.

example:-    
rowInQuestion = StudentTable.NewRow();

rowInQuestion["StudentID"] = 4;

rowInQuestion["SudentName"] = "Ashish";

studentTable.Rows.Add(rowInQuestion);

2)The second method to add a new DataRow into a DataTable is to use the LoadDataRow
method. Load data row  method allows you to add a new DataRow and set its various values in 
one convenient
method call.

Example:-  
object[] rowVals = {"4", "Ashish"} ;

StudentTable.LoadDataRow(rowVals, false);

Modify Existing Rows
Two methods exist for adding existing DataRows to a DataTable. The first is the  ImportRow method, which simply takes a DataRow as a parameter, and the second is the Merge
method, which merges two sets of disconnected data (DataSet, DataTable, DataRow array, etc.) and   The DataRowState at the end of this edit would be Modified

Example:- 
rowInQuestion = StudentTable.Rows[0];

rowInQuestion["StudentName"] = "Anil";

2)There second  way of  modifying rows, The DataRow object has a method called BeginEdit,

and a matching method called EndEdit.we can  modify the rows between these two method

calls and difference  is that all the changes are buffered until you call EndEdit. If, in case

you change your mind, you call CancelEdit instead, all the changes are rolled back.


Example:-rowInQuestion = StudentTable.Rows[0];

rowInQuestion.BeginEdit();

rowInQuestion["StudentName"] = "Aditya";

rowInQuestion.EndEdit() ;

rowInQuestion = StudentTable.Rows[0];


rowInQuestion.ItemArray = new object[] {null, "kamal"} ;




Delete Existing Rows

using delete method we can delete the row

rowInQuestion = StudentTable.Rows[1];

rowInQuestion.Delete()

Another way of removing a row is by using either the Remove or RemoveAt method on the

DataRowCollection object (DataTable.Rows is of DataRowCollection type)

StudentTable.Remove(rowInQuestion) ;

// or
StudentTable.RemoveAt(1) ;

The DataRowVersion Enumeration

ConstantDescription
CurrentThis constant will give you the current value stored inside a column.
OriginalThis constant allows you to fetch the original value stored inside a column.
ProposedThis constant allows you to fetch the proposed value after a BeginEdit and update  but before an EndEdit.
Default This constant retrieves the value as if the constant wasn’t specified


No comments: