An "Invalid attempt to read from column ordinal" error occurs when you use DataReader in Visual C# (308614)
The information in this article applies to:
- Microsoft ADO.Net 2.0
- Microsoft ADO.NET (included with the .NET Framework)
- Microsoft Visual C# .NET (2002)
- Microsoft Visual C# .NET (2003)
- Microsoft Visual C# 2005
This article was previously published under Q308614 For a Microsoft Visual Basic .NET version of this article, see 308069.
This article refers to the following Microsoft .NET Framework Class Library namespace:
SYMPTOMS
When you use DataReader to read a row, if you try to access columns in that row, you receive an error message that resembles the following:
System.InvalidOperationException: Invalid attempt to read from column ordinal '0'. With CommandBehavior.SequentialAccess, you may only read from column ordinal '2' or greater.
CAUSE
This problem occurs because you executed OleDbCommand or SqlCommand with the System.Data.CommandBehavior.SequentialAccess flag set but did not access the columns sequentially.
RESOLUTION
Use one of the following methods to work around this problem:
- Read each column only once and in the sequence in which it is defined by the SELECT query.
NOTE: For performance reasons, which are listed in the "More Information" section, this is the preferred resolution. - Do not use CommandBehavior.SequentialAccess. If you do not use CommandBehavior.SequentialAccess, you can access a column in a row twice and read columns out of sequence.
STATUSThis behavior is by design.REFERENCES
For more information, see the "SequentialAccess enumeration member" topic in the Visual Studio .NET Help documentation.
Modification Type: | Minor | Last Reviewed: | 10/4/2006 |
---|
Keywords: | kbtshoot kberrmsg kbDatabase kbprb kbSqlClient kbSystemData KB308614 kbAudDeveloper |
---|
|