Bug 41183 - "System.InvalidOperationException: No data available" when using BeginExecuteReader/EndExecuteReader
Summary: "System.InvalidOperationException: No data available" when using BeginExecute...
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Data ()
Version: 4.2.0 (C6)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2016-05-19 16:00 UTC by Andre Raabe
Modified: 2018-02-22 22:23 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Repro code (2.31 KB, text/plain)
2016-05-19 16:00 UTC, Andre Raabe

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Andre Raabe 2016-05-19 16:00:36 UTC
Created attachment 16037 [details]
Repro code

Mono Version (Using Docker with "FROM mono"):

Mono JIT compiler version 4.2.3 (Stable Wed Mar 16 13:19:08 UTC 2016)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen

SQL Server version:

Microsoft SQL Server 2014 (SP1-CU5) (KB3130926) - 12.0.4439.1 (X64) 
	Feb 15 2016 12:12:43 
	Copyright (c) Microsoft Corporation
	Developer Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)

Expected result:

2 times the output of "--> Hello"


- Create a simple test database at SQL Server
- Create stored procedure with out variables:

create procedure MonoTest (@InParameter varchar(5), @OutParameter varchar(5) output)
as begin
    -- Simple example by setting out to the same value of in
    select @OutParameter = @InParameter


- Use and compile sample script from attachment (SqlClientWithExecAndOutVar.cs)
  mcs -r:System.Data SqlClientWithExecAndOutVar.cs

- Run exe with connectionstring as parameter (for mono don't forget to add "Asynchronous Processing = true")
  mono SqlClientWithExecAndOutVar.exe "User ID=<SQL-User>;initial Catalog=<DB-Name>;Data Source=<DB-Server>;Password=<SQL-User-Password>;Asynchronous Processing = true"

Output from windows:

Using ExecuteScalarAsync
--> Hello
Using BeginExecuteReader .. EndExecuteReader
--> Hello

Output from mono:

Using ExecuteScalarAsync
--> Hello
Using BeginExecuteReader .. EndExecuteReader
Error 1
System.InvalidOperationException: No data available.
  at System.Data.SqlClient.SqlDataReader.EnsureDataAvailable () <0x406bf480 + 0x0005f> in <filename unknown>:0
  at System.Data.SqlClient.SqlDataReader.GetValue (Int32 i) <0x406bf310 + 0x0002f> in <filename unknown>:0
  at SqlClientWithExecAndOutVar.Program.Main (System.String[] args) <0x4066fd80 + 0x003e3> in <filename unknown>:0
Comment 1 jerrycheese 2016-10-09 22:42:13 UTC
Also happens to me,

SqlDataReader.HasRows returns true
SqlDataReader.FieldCount returns greater than 1
And i know for a fact that the first column in my table is an Int64 (and that there are entries in that table)

yet SqlDataReader.GetInt64(0) returns a:
System.InvalidOperationException: No data available.

Is anyone aware of a work around for easily reading multiple result sets and their data ?
Comment 2 Marek Safar 2018-02-22 22:23:29 UTC
Mono 5.10 has significantly improved System.Data implementation which should resolve this issue. If you can still reproduce it please reopen the issue.