0

Asp.Net 4, C#, Oracle 11g

Hi, I'm trying to save the content of an html file to an Oracle CLOB column. The html file is uploaded to the server through an asp:Upload button. It is working fine most of the time, the problem is that sometimes the stream at the FileContent property of the button, has an odd number of bytes, and the Write method of the clob column throws an exception, stating that it requires an even number of bytes

How can I solve this problem??? Is there anything I can do to make my html files have an even number of bytes??? Html files are encoded as UTF8, and changing the encoding do modify the number of bytes, but they are still an even number

Thanks in advance

Edit: For now, I'm just increasing the size of the buffer by 1 in the case of the stream length being an odd number, then write the stream, in its own length, to the buffer, thus defaulting the last byte of the buffer. Please advice of any potential errors in doing it this way:

var buffer = new byte[(stream.Length % 2 > 0? stream.Length + 1: stream.Length)];
stream.Read(buffer, 0, (int)stream.Length);
clob.Write(buffer, 0, buffer.Length);

Thanks again

Edit: the previous solution didn't work. The new approach consists of passing the stream to string, add a space at the end of the string, and then convert to stream again. It's working fine until now. Sorry I can't post the code... it's just that I couldn't figured out how to overcome this policy of 4 spaces for code in StackOverflow

1 Answer 1

3

I'm assuming that you are using the System.Data.OracleClient classes (as opposed to Oracle's ODP.NET).

The OracleLob class has no method for writing a string, which I would expect for handling CLOBs. Instead, the documentation says:

The .NET Framework Data Provider for Oracle handles all CLOB and NCLOB data as Unicode. Therefore, when accessing CLOB and NCLOB data types, you are always dealing with the number of bytes, where each character is 2 bytes. For example, if a string of text containing three characters is saved as an NCLOB on an Oracle server where the character set is 4 bytes per character, and you perform a Write operation, you specify the length of the string as 6 bytes, although it is stored as 12 bytes on the server.

In this context, Unicode means the UTF-16 encoding which requires 2 bytes for most characters and 4 or 6 bytes for characters in the supplementary planes.

So if you have a string, you have to convert it to UTF-16 first:

byte[] utf16Bytes = Encoding.Unicode.GetBytes(str);
clob.Write(utf16Bytes, 0, utf16Bytes.Lenght);

Or you can use a StreamWriter to achieve the same:

OracleLob clob = ...
using (StreamWriter writer = new StreamWriter(clob, Encoding.Unicode))
{
    writer.Write(str);
}

If your data is in a UTF-8 encoded byte array, then you have to convert it to UTF-16:

byte[] utf8Data = ...
byte[] utf16Data = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, utf8Data);
clob.Write(utf16Data, 0, utf16Data.Length);
3
  • Hi @Codo yes, I'm using OracleClient. For now, the solution is to convert the stream to string and then append a whitespace at the end, to make it even. It's working fine so far and they're testing it... I'll try your solution if it gives troubles. Thanks Jan 8, 2013 at 21:38
  • Have you ever used Toad or SQL Developer or a similar tool to make sure the data has arrived in the database in a proper form?
    – Codo
    Jan 8, 2013 at 21:43
  • Hi @Codo I use SQL developer to run a script with "dbms_lob.substr" so I can see the content of the files. The content arrives just as the App is sending it Jan 8, 2013 at 21:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.