2

It's the first time I HAVE to work with Oracle, and as we all hate to work with foreign stuff while you are working with a specific model some years, although this is our job and we have to make it.

Now i have installed the Oracle 11 g and copied and referenced the Oracle.DataAccess.dll created a method where opens a connection and tries to retrieve some objects from a view that has been created on the server.

Method:

public BindingList<HeaderReceiver> GetHeaderReceivers()
{
    try
    {
        using (OracleConnection db = new OracleConnection(BaseDataAccess.ConnString))
        {
            string cmdText = "select * from p_customer t";
            BindingList<HeaderReceiver> headerReceivers = new BindingList<HeaderReceiver>();

            OracleCommand cmd = new OracleCommand(cmdText) { CommandType = CommandType.Text };
            db.Open();

            OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); <--- Error Occurs HERE!!!

            while (reader.Read())
                headerReceivers.Add(HeaderReceiver.GetInstance(reader));

            CustBranchRepository rep = new CustBranchRepository();
            headerReceivers.ForEach(p => p.DetailsBranch = rep.GetDetailReceivers(p.Id));

            reader.Close();
            db.Close();
            return headerReceivers;
        }
    }
    catch (Exception ex)
    {
        ExporterLogger.Log(ex);
        return null;
    }
}

Now when ExecuteReader() commits I get this InvalidOperationException.

Operation is not valid due to the current state of the object.

The StackTrace:

   at Oracle.DataAccess.Client.OracleCommand.ExecuteReader(Boolean requery, Boolean fillRequest, CommandBehavior behavior)
   at Oracle.DataAccess.Client.OracleCommand.ExecuteReader(CommandBehavior behavior)
   at Exporter.Boss.DataAccess.CustomerRepository.GetHeaderReceivers() in ...Exporter\Exporter.Boss.DataAccess\CustomerRepository.cs:line 25

Any thoughts and help...

Thank you!

George Taskos
  • 8,324
  • 18
  • 82
  • 147

1 Answers1

10

It looks like you're missing the connection on the command object...

db.Open();
OracleCommand cmd = new OracleCommand(cmdText) { CommandType = CommandType.Text };
cmd.Connection = db;
Will
  • 773
  • 1
  • 7
  • 24
  • 1
    Found this post. I had exactly this error when calling ExecuteDataReader() on my Command object. Turns out I did set the connection on the Command object, but I did this after calling ExecuteDataReader(). When I switched the two lines it worked perfectly fine. So, this should be marked as the right answer imo. – Jeroen Oct 02 '13 at 14:51
  • Set connection immediately after OracleCommand initialization as above and It will work. – Lahiru Gamage Aug 25 '19 at 18:30