Search code examples
c#.netpostgresqlnpgsqlenterprisedb

"An operation is already in progress" on DataAdaper.Fill


I am trying the Postgres Plus 9.5 with .Net 4.5, Npgsql 3.1.6 NuGet package. I have read what is here about this error, but I do not understand why I get it. Everything is disposed. Here the is code:

public override DataTable getActListData(int FunkNr)
{
  using (var cmd = new NpgsqlCommand())
  {
    cmd.CommandText = npgsqlCommand3.CommandText;
    cmd.Connection = this.npgsqlConnection;
    cmd.Parameters.Add(new Npgsql.NpgsqlParameter("ANWENDUNG", NpgsqlTypes.NpgsqlDbType.Numeric));
    cmd.Parameters.Add(new Npgsql.NpgsqlParameter("XFUNKNR", NpgsqlTypes.NpgsqlDbType.Numeric));


    using (var da = new NpgsqlDataAdapter(npgsqlCommand3))
    {
      var tab = new DataTable();
      da.SelectCommand.Parameters["ANWENDUNG"].Value = getAnwendung();
      da.SelectCommand.Parameters["XFUNKNR"].Value = FunkNr;
      da.Fill(tab);  // Here is the error on the 5th call
      return tab;
    }
  }
}

Does this problem comes from Npgsql or is from Postgres?

Some other questions:

I have read here, that lazy loading is impossible, but I didn't understand is is because of the Npgsql or from Postgres?

Is it possible in Postgres to open several cursors and read on demand in the same connection?

Edit: Changed the code:

  using (var npgsqlConnection = new NpgsqlConnection())
  {
    ConnectionString = string.Format(DataClientFactory.DataBaseConnectString, DB, User, PW);
    npgsqlConnection.ConnectionString = ConnectionString;
    npgsqlConnection.Open();
    ....
    the code above here
    ....
 }

The same error in the same call. The error:

System.InvalidOperationException occurred
  HResult=-2146233079
  Message=An operation is already in progress.
  Source=Npgsql
  StackTrace:
       bei Npgsql.NpgsqlConnector.StartUserAction(ConnectorState newState)
  InnerException: 

Solution

  • If the piece of code you posted runs concurrently on the same connection, then that's your problem - Npgsql connections aren't thread-safe, and it's not possible to have multiple readers opened at the same time (MARS).