Microsoft KB Archive/313547

= FIX: &quot;Unhandled Exception&quot; Error Message When You Use ExecuteReader Method with CommandBehavior.SingleRow =

Article ID: 313547

Article Last Modified on 4/9/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1

-



This article was previously published under Q313547



This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data
 * System.Data.OleDb



SYMPTOMS
If you try to return an empty DataReader object by using the ExecuteReader method of an OleDbCommand object with a CommandBehavior value of SingleRow, you receive the following error message or similar:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll

This occurs when you use the Microsoft OLE DB Provider for SQL Server with the OLE DB .NET Managed Provider.

If you catch the exception, the message of the exception states:

Object or data matching the name, range, or selection criteria was not found within the scope of this operation.

You expect the DataReader to be returned without an error. In addition, you expect the Read method of the DataReader to return False if no rows are found.



RESOLUTION
When you use the OLE DB Provider for SQL Server, do not use a CommandBehavior value of SingleRow if you do not return records with the ExecuteReader method of OleDBCommand.

Alternately, use the SQL Server .NET Managed Provider. The error does not occur when you use the SQL Server .NET Managed Provider, even if you use a CommandBehavior value of SingleRow.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Microsoft ADO.NET (included with the .NET Framework 1.1).



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET, and then create a new Visual Basic Console application. Module1 and a reference to the System.Data namespace are added by default.  Add the following Imports statements to the top of Module1's Code window: Imports System Imports System.Data.OleDb   Paste the following code into the Sub Main procedure: Dim cn As OleDbConnection = New OleDbConnection(&quot;Provider=SQLOLEDB;Data Source=servername;&quot; & _                                               &quot;User ID=login;Password=password;&quot; & _                                                &quot;Initial Catalog=Northwind;&quot;) cn.Open

'Specify that no records be returned. Dim cmd As OleDbCommand = New OleDbCommand(&quot;SELECT * FROM Customers WHERE 1=0&quot;, cn)

Dim dr As OleDbDataReader Try dr = cmd.ExecuteReader(CommandBehavior.SingleRow) 'WORKAROUND: Comment out the above line, and uncomment the following line. 'dr = cmd.ExecuteReader

Console.WriteLine(&quot;ExecuteReader is executed&quot;) dr.Close Catch ex As Exception Console.WriteLine(ex.Message.ToString) Finally 'Pause. Console.ReadLine 'Clean up. cn.Close End Try  Modify the connection string to connect to the computer that is running SQL Server. Press F5 to build and to start the project. You receive an error message. Press ENTER to return to the development environment.</li>  To work around this problem, comment out the following line dr = cmd.ExecuteReader(CommandBehavior.SingleRow) and uncomment the following line: 'dr = cmd.ExecuteReader </li> Press F5 to rebuild and start the project. Notice that the ExecuteReader method succeeds. Press ENTER to return to the Development Environment.</li></ol>

<div class="references_section">