Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

DataTableReader.GetValue Method

Gets the value of the specified column in its native format.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)
public override Object GetValue(
	int ordinal
)

Parameters

ordinal
Type: System.Int32

The zero-based column ordinal

Return Value

Type: System.Object
The value of the specified column. This method returns DBNull for null columns.

Implements

IDataRecord.GetValue(Int32)
ExceptionCondition
ArgumentOutOfRangeException

The index passed was outside the range of 0 to FieldCount - 1.

DeletedRowInaccessibleException

An attempt was made to retrieve data from a deleted row.

InvalidOperationException

An attempt was made to read or access columns in a closed DataTableReader .

Although you can call IsDBNull to see if there are null values before calling this method, you do not have to do this.

The following example iterates through all the columns within the current row in a DataTableReader, displaying the contents of each column and the column name. Generally, if your intent is to work with all the columns within a row retrieved by a DataTableReader, consider using the GetValues method instead, because it is more efficient.

private static void GetAllValues(DataTableReader reader)
{
    // Given a DataTableReader, retrieve the value of  
    // each column, and display the name, value, and type. 
    // Make sure you have called reader.Read at least once before 
    // calling this procedure. 

    // Loop through all the columns. 
    object value = null;
    for (int i = 0; i < reader.FieldCount; i++)
    {
        if (reader.IsDBNull(i))
        {
            value = "<NULL>";
        }
        else
        {
            value = reader.GetValue(i);
        }
        Console.WriteLine("{0}: {1} ({2})", reader.GetName(i), 
            value, reader.GetFieldType(i).Name);
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.