Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OracleCommand.Executenonquery throws 'System.InvalidOperationException'

The following function throws the System.InvalidOperationException:

internal void executeNonQuery(string connectionString, OracleCommand cmd)
    {
        using (OracleConnection conn = new OracleConnection(connectionString))
        {
            using (cmd)
            {
                conn.Open();
                cmd.ExecuteNonQuery(); //here is the error
                conn.Close();
            }
        }
    }

The additional information is:

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

I try to insert a row into a table. Is there another way to do this or to fix this error?

EDIT: I build the query in the binaryManager class with the following methods:

internal object[] binaryInsert(string tblName, string tblQuery, int conStrgID, int cq)
    {
        object[] retValues = new object[3];
        Stream myStream = null ;
        OracleConnection con = null;
        string conString = qm.getConnectionString("ConnectionStringToMyDB"); //is correct

        byte[] data = GetBytes(tblQuery);

        String sql = "INSERT INTO MYTABLES VALUES (NULL, '" + tblName + "', ':tblQueryBlob', " + conStrgID + ", " + cq + ")";


        OracleCommand cmd = new OracleCommand();
        cmd.CommandText = sql;  // Set the sql-command
        cmd.Connection = con;   //con is an OracleConnection

        OracleParameter param = cmd.Parameters.Add("tblQueryBlob", OracleDbType.Blob); //Add the parameter for the blobcolumn
        param.Direction = ParameterDirection.Input;

        param.Value = data;     //Asign the Byte Array to the parameter

        //command containts the parameter :tblQueryBlob with its value
        retValues[0] = cmd;
        retValues[1] = conString;

        return retValues;
    }

private byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }

I call the binaryInsert method from another class with the following code:

BinaryManager bm = new bBinaryManager();
string sqlQuery = "large string with 5100 characters";
object[] binaryValues = bm.binaryInsert("TextTbl", sqlQuery, 1, 1);
string conString = binaryValues[1].ToString();
OracleCommand cmd = (OracleCommand)binaryValues[0];

QueryManager qm = new QueryManager();
qm.executeNonQuery(conString, cmd);

EDIT2 (ExecuteReader):

int csID = 1;
using (OracleConnection conn = new OracleConnection(conString))
                {
                    using (OracleCommand myCommand = new OracleCommand(query, conn))
                    {
                        conn.Open();
                        myCommand.Connection = conn; // FIX!
                        using(OracleDataReader read = myCommand.ExecuteReader())
                        {
                            if (read.HasRows)
                            {
                                while (read.Read())
                                {
                                    if (read[0] == System.DBNull.Value)
                                    {
                                        TableObject tableDBNull = new TableObject(csID, Convert.ToString(read[1]), (byte[])read[2], Convert.ToInt32(read[3]), Convert.ToInt32(read[4]));
                                        allTables.Add(tableDBNull);
                                        csID++;
                                    }
                                    else
                                    {
                                        TableObject tableDBNNull = new TableObject(Convert.ToInt32(read[0]), Convert.ToString(read[1]), (byte[])read[2], Convert.ToInt32(read[3]), Convert.ToInt32(read[4]));
                                        allTables.Add(tableDBNNull);
                                    }
                                }
                            }
                        }
                        conn.Close();
                    }

The error happens in the if block: if (read[0] == System.DBNull.Value)

like image 853
Hack4Life Avatar asked Feb 10 '26 19:02

Hack4Life


1 Answers

The problem is not the Query (query errors raises exception from Oracle namespace).

In your case the command is not related to any connection.
You can solve it in some ways, i.e.

internal void executeNonQuery(string connectionString, OracleCommand cmd)
{
    using (OracleConnection conn = new OracleConnection(connectionString))
    {
        using (cmd)
        {
            conn.Open();
            cmd.Connection = conn; // FIX!
            cmd.ExecuteNonQuery(); //here is the error
            conn.Close();
        }
    }
}

Usually I prefere another way to do it

OracleConnection con = new OracleConnection(connectionString);
// Creating command from the connection
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = ...........
cmd.ExecuteNonQuery();
like image 173
bubi Avatar answered Feb 12 '26 15:02

bubi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!