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!

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Set connection immediately after OracleCommand initialization as above and It will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.