Link to home
Start Free TrialLog in
Avatar of ofaniel
ofanielFlag for United States of America

asked on

C# Oracleclient Updating NULL CLOB

In the Oracle database, the column that I would like to update is designated as a CLOB. I am using C# and the Oracle Instant Client (System.Data.OracleClient) to interface.

The code shown below works perfectly if the CLOB column already has data inside it when I update. However, if prior to the update the column is NULL, it throws the error:

Null LOBs may not be modified. (I received this error before wrapping the .Erase and .SetLength in the .Null conditional).

OR

Invalid operation. The connection is closed.

To limit changes at the database level, I don't want to use a proc (although I know it's probably best!).

What am I missing? What needs done if the column is NULL previous to the update?

Thanks!
Chuck
private void UpdateCaseNotes(string SchemaReference, string DefectId, string Comments)
    {
      string connectionstring = ConfigurationSettings.AppSettings["DBConnStr"];
      string sql = "SELECT " + colCaseComment + " FROM " + SchemaReference + ".BUG WHERE BG_BUG_ID=" + DefectId + " FOR UPDATE";
      
      using (OracleConnection conn = new OracleConnection(connectionstring))
      {
        using (OracleCommand cmd = new OracleCommand(sql, conn))
        {
          conn.Open();
          using (OracleDataReader dr = cmd.ExecuteReader())
          {
            OracleTransaction tx = conn.BeginTransaction();
            
            cmd.Transaction = tx;
            dr.Read();
 
            byte[] clobByte = Encoding.Unicode.GetBytes(Comments);
            OracleLob clobColumn = dr.GetOracleLob(0);
            
            Logger.Write(conn.State.ToString() + " - Connection State - " + clobByte.Length, "Tracing");
 
            clobColumn.BeginBatch(OracleLobOpenMode.ReadWrite);
            
            if(clobColumn != OracleLob.Null)
            {
              clobColumn.Erase(0, clobColumn.Length);
              clobColumn.SetLength(0); 
            }
            
            clobColumn.Write(clobByte, 0, clobByte.Length);
            clobColumn.EndBatch();
            
            tx.Commit();
          }
          if (conn.State.ToString().Equals("Open"))
          {
            conn.Close();
          }
        }
      }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ofaniel
ofaniel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial