2 Replies Latest reply: Sep 10, 2012 8:19 PM by 960538 RSS

    InvalidOperationException throws when execute OracleCommand.ExecuteNonQuery

    960538
      Hi,

      I have encountered an problem when I execute this code, which delete a record from Oracle DB. However, it doesn't happen when execute other SQL statements like "Select ", "Update". I am not sure whether this problem will happen on Microsoft SQL Server database. Please test this code below, and deal with this InvalidOperationException problem. Thank you in advance.

      private void btnDelete_Click(object sender, EventArgs e)
      {
      string oradb = "Data Source=localhost;User Id=system;Password=admin;";
      OracleConnection conn = new OracleConnection(oradb);
      int recordAffected;
      // Delete an existing record
      try
      {
      conn.Open();
      OracleCommand cmd = new OracleCommand();
      cmd.CommandText = "DELETE FROM MEMBERS23T WHERE ID=:pID";
      cmd.Parameters.Clear();
      cmd.Parameters.Add(new OracleParameter("pID", tbSearch.Text));
      cmd.CommandType = CommandType.Text;
      recordAffected = cmd.ExecuteNonQuery(); // Here throws the InvalidOperationException.
      if (recordAffected == 1)
      {
      MessageBox.Show("Record successfully deleted:" + recordAffected);
      }
      else
      {
      MessageBox.Show("エラーが発生しました:" + recordAffected);
      }
      conn.Close();
      conn.Dispose();
      conn = null;
      }
      catch (OracleException oe)
      {
      MessageBox.Show(oe.Message);
      conn = null;
      }
      }