HEEN
HEEN

Reputation: 4727

Operation is not valid due to the current state of the object when I submit data to DB in Oracle asp.net

I have a method where I want to execute a stored procedure and get the status and response from it. When executing my cmd.ExecuteNonQuery(), I am getting an error:

operation is not valid due to the current state of the object

This is the code:

public static string GET_IPL_STATUS_ON_NEID(string NEID)
{
        string strStatus = string.Empty; string strMessage = string.Empty;

        OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAppAuto"].ConnectionString);

        try
        {
            OracleCommand cmd = new OracleCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = ConfigurationManager.AppSettings["GET_IP_STATUS_PROC"].ToString();

            cmd.Parameters.Add(new OracleParameter { ParameterName = "P_A6_NEID", Value = NEID, OracleDbType = OracleDbType.Varchar2, Direction = ParameterDirection.Input });

            cmd.Parameters.Add(new OracleParameter
            {
                ParameterName = "STATUS",
                OracleDbType = OracleDbType.Varchar2,
                Size = 500,
                Direction = ParameterDirection.Output
            });

            cmd.Parameters.Add(new OracleParameter
            {
                ParameterName = "MESSAGE",
                OracleDbType = OracleDbType.Varchar2,
                Size = 500,
                Direction = ParameterDirection.Output
            });

            if (conn.State != ConnectionState.Open) conn.Open();
            
            cmd.ExecuteNonQuery();

            strStatus = cmd.Parameters["STATUS"].Value.ToString();
            strMessage = cmd.Parameters["MESSAGE"].Value.ToString();
        }
        catch (Exception ex)
        {   
            throw;
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
        }

        return strStatus;            
}

Upvotes: 0

Views: 235

Answers (1)

MT0
MT0

Reputation: 168470

As per the example in the OracleCommand documentation, you need to set the Connection property on the command to tell it which connection to use:

OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = ConfigurationManager.AppSettings["GET_IP_STATUS_PROC"].ToString();

Upvotes: 1

Related Questions