25.2 Properties Reference

HasErrors

 Boolean   hasErrors   = DataRow.HasErrors; 

Gets a value indicating whether there are errors in the DataRow .

Example

The following example shows how to use the HasErrors property to determine if there are any errors in the DataRow after the reconciliation of the modified DataTable with the data source:

 DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(); // ... define the DataAdapter // fill the DataTable da.Fill(dt); // ... modify the data in the DataTable da.Update(dt); if (dt.HasErrors) {     foreach(DataRow row in dt.Rows)     {         if (row.HasErrors)         {          // ... handle the errors for the row         }     } } 

Notes

The SetColumnError method can set an error on a column in the row.

The GetColumnsInError methods can retrieve the columns in the row with errors. The GetColumnError can retrieve the error description for a column.

The ClearErrors method can clear all errors on the row.

Item

 Object   colValue   = DataRow[String   columnName   ]; DataRow[String   columnName   ] =   colValue   ; Object   colValue   = DataRow[DataColumn   col   ]; DataRow[DataColumn   col   ] =   colValue   ; Object   colValue   = DataRow[DataColumn   col   ]; DataRow[DataColumn   col   ] =   colValue   ; Object   colValue   = DataRow[Integer   colIndex   ]; DataRow[Integer   colIndex   ] =   colValue   ; Object   colValue   = DataRow[DataColumn   col   ]; DataRow[DataColumn   col   ] =   colValue   ; Object   colValue   = DataRow[Integer   colIndex   ]; DataRow[Integer   colIndex   ] =   colValue   ; Object   colValue   = DataRow[String   columnName   , DataRowVersion   drv   ]; Object   colValue   = DataRow[DataColumn   col   , DataRowVersion   drv   ]; Object   colValue   = DataRow[Integer   colIndex   , DataRowVersion   drv   ]; 

The indexer for the class that gets or sets the data stored in the row for the specified column.

Parameters

colValue

An object containing the data to get or set for the DataColumn .

columnName

The name of the DataColumn .

col

A reference to a DataColumn object.

colIndex

The index of the column in the DataColumnCollection of the DataTable .

drv

The DataRowVersion value of the column to retrieve. For more on the DataRowVersion value, see Table 25-5 in the HasVersion section of this chapter.

Example

The following examples show how to get and set the values for a column using the different versions of the Item property:

 DataTable dt = new DataTable(); DataColumn col = dt.Columns.Add("MyColumn", typeof(System.String)); DataRow row = dt.NewRow(); // the next three statements have the same result row["MyColumn"] = "My value"; row[col] = "My value"; row[0] = "My value"; String rowValue=""; // the next three statements return the same result rowValue = (String) row["MyColumn", DataRowVersion.Current]; rowValue = (String) row[col, DataRowVersion.Current]; rowValue = (String) row[0, DataRowVersion.Current]; 

Notes

If the specified column can't be found, or the specified column index doesn't exist, an IndexOutOfRangeException is raised.

If the specified column reference is null , an ArgumentNullException is raised. If the column referenced doesn't belong to the table, an ArgumentException is raised.

If the data type of the column value specified doesn't match the DataType for the column, an InvalidCastException is raised.

If an attempt is made to set a value on a deleted row, a DeletedRowInaccessibleException is raised.

If an attempt is made to access a version of the row that doesn't exist, a VersionNotFoundException is raised.

ItemArray

 Object[]   itemArray   = DataRow.ItemArray; DataRow.ItemArray =   itemArray   ; 

Gets or sets the values of the row using an object array. Each item in the object array corresponds to a column in the DataTable .

Example

The following example demonstrates how to get and set the values for columns in a row using the ItemArray property:

 // create a table with two columns DataTable dt = new DataTable(); DataColumn colId = new DataColumn("ProductId", typeof(System.Int32)); DataColumn colDesc = new DataColumn("Description", typeof(System.String)); dt.Columns.AddRange(new DataColumn[] {colId, colDesc}); dt.Rows.Add(new object[] {1, "Widget"}); // get the data for the row using the ItemArray property object[] row = dt.Rows[0].ItemArray;  // set the ProductId to be AutoIncrement  colId.AutoIncrement = true; // pass null for the AutoIncrement value dt.Rows.Add(new object[] {null, "Thing"}); // let the description be null colDesc.AllowDBNull = true; // add a row with a null description, and AutoIncrement Id dt.Rows.Add(new object[] {null, null}); 

Notes

When the ItemArray property is used, an attempt is made to locate the row matching the primary key. If the row is found, it is updated with the values in the ItemArray ; otherwise , a new row is created.

Any columns with an array element set to null are set to the default value for a new column or the existing value for the column if the row is being updated.

The value for AutoIncrement columns should be set to null in the ItemArray .

RowError

 String   rowError   = DataRow.RowError; DataRow.RowError =   rowError   ; 

Gets or sets a value containing the error description text that applies to the entire DataRow .

Example

The following example shows how to set the error text for a DataRow :

 DataTable dt = new DataTable(); DataColumn col = dt.Columns.Add("MyColumn", typeof(System.String)); DataRow row = dt.NewRow(); row.RowError = "This row has an error."; 

Notes

The RowError property can be set when processing modified data against the business rules. Alternatively, when the Update( ) method of the DataAdapter is called and fails and the ContinueUpdateOnError property of the DataAdapter is set to true , the HasErrors property of the row is set to true and the RowError property is set to the error message. The RowUpdated event handler can be used to determine the status of the update attempt and set the error text for the row as necessary.

Use the GetColumnError( ) and SetColumnError( ) methods to get and set the error description for a particular column.

RowState

 DataRowState   rowState   = DataRow.RowState; 

Gets the current row state of a DataRow within the DataRowCollection . This value is one of the DataRowState values described in Table 25-4.

Table 25-4. DataRowState enumeration

Value

Description

 Added 

The row has been added to the DataRowCollection for the table, and AcceptChanges( ) hasn't been called.

 Deleted 

The row has been deleted, and AcceptChanges( ) hasn't been called.

 Detached 

The row isn't part of a DataRowCollection .

 Modified 

The row has been modified, and AcceptChanges( ) hasn't been called.

 Unchanged 

The row hasn't been changed since AcceptChanges( ) was last called.

Example

The value of the RowState property can't be directly set. ADO.NET sets the row state in response to actions that affect the DataRow . The AcceptChanges( ) and RejectChanges( ) methods, whether explicitly or implicitly called, reset the RowState value for the row to Unchanged as illustrated in the following code:

 // create a table with one column DataTable dt = new DataTable(); dt.Columns.Add("MyColumn",  typeof(System.String)); // create a new row DataRow row = dt.NewRow();        // RowState = Detached // add the row to the table dt.Rows.Add(row);                 // RowState = Added dt.AcceptChanges();               // RowState = Unchanged // modify the row row["MyColumn"] = "MyFieldValue"; // RowState = Modified // reject the changes row.RejectChanges();              // RowState = Unchanged // delete the row row.Delete();                     // RowState = Deleted row.AcceptChanges();              // row no longer exists 

Note

The RowState property is used primarily by ADO.NET to track the changes that have been made to a disconnected row so that changes to the data while disconnected can be updated back to the data source

Table

 DataTable   table   = DataRow.Table; 

Gets the table that the row belongs to and has a schema for.

Example

The following example demonstrates how to retrieve the DataTable the row belongs to:

 DataTable dt1 = new DataTable(); DataRow row = DataTable.NewRow(); // returns a reference to DataTable dt1 DataTable dt2 = row.Table; 

Note

A row belongs to the DataTable once it is added to the DataRowCollection . If the DataRow doesn't belong to a table, a null reference is returned.



ADO. NET in a Nutshell
ADO.NET in a Nutshell
ISBN: 0596003617
EAN: 2147483647
Year: 2005
Pages: 415

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net