Wrting byte[] into a memo with FieldPutBytes

This forum is meant for questions and discussions about the X# language and tools
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Wrting byte[] into a memo with FieldPutBytes

Post by NickFriend »

Hi All,

I need to write a utility to extract data from an Excel file and write it into a DBF (CDX) with memo fields. One text cell needs to be translated into MS-Word .doc format and stored as a byte array in a memo field.

I have a converter that gives me a byte[] with the required .doc format, but when I try to write the array into the memo field I get this error on the FieldPutBytes line.

Code: Select all

                
XSharp.Core.Functions.SetAnsi(false);
XSharp.Core.Functions.SetExclusive(false);
XSharp.Core.Functions.SetDateFormat("MM/DD/YY");
XSharp.Core.Functions.SetEpoch(1990);
XSharp.Core.Functions.SetDeleted(true);

QW10DocumentConverter converter = new QW10DocumentConverter();

DbServer mwriter = new DbServer(@"D:LiveQWPartsM.DBF", false, false, "DBFCDX");

if (mwriter.Used)
{
     foreach (VMQW10PartsMItem item in itemlist)
     {
          mwriter.Append();
          byte[] bytespec = converter.ConvertStringTextToWordDoc(item.Spec);
          mwriter.FieldPutBytes("SDESCR", bytespec);
     }
     mwriter.Commit();
     mwriter.Close();
}
I get a "No RDD Exception found in the runtime state" error. The full error is attached. I'm not opening a CDX file, just the pre-existing DBF/FPT.

I'm sure it's a silly error by me, but can someone give me a hint as to what's wrong.

TIA

Nick
Attachments
XSharpError.jpg
XSharpError.jpg (44.39 KiB) Viewed 716 times
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Wrting byte[] into a memo with FieldPutBytes

Post by ic2 »

Hello Nick,

I have a ready & working X# Exce->DBF program which I'll send you now by mail

Dick
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Wrting byte[] into a memo with FieldPutBytes

Post by Chris »

Hi Nick,

Actually this looks like a bug in the FieldPutBytes() functionality in the X# RDD. We'll look into this, thanks for your report!
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Wrting byte[] into a memo with FieldPutBytes

Post by NickFriend »

Ok, thanks Chris.

I did try to do this using FieldPut and the AdjustBinaryData class that you came up with way back, but I found that it was corrupting the format of the saved documents...

Something like this.... any idea why this isn't working? It's just that I need to try and get this working for Monday somehow :S

Code: Select all

int codepage = XSharp.RuntimeState.DosCodePage;
AdjustBinaryData.Initialize(Encoding.GetEncoding(codepage));
....
byte[] bytespec = converter.ConvertStringTextToWordDoc(item.Spec);
mwriter.FIELDPUT("SDESCR", AdjustBinaryData.BeforeSaveBytes(bytespec));
....

public static class AdjustBinaryData
{
    private static Encoding enc = Encoding.Default;

    public static void Initialize(Encoding encoding)
    {
        enc = encoding;
    }

    public static string BeforeSaveBytes(byte[] input)
    {
        return enc.GetString(input);
    }

    public static byte[] AfterReadToBytes(string input)
    {
        return enc.GetBytes(input);
    }
}
Nick
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Wrting byte[] into a memo with FieldPutBytes

Post by Chris »

Hi Nick,

Not sure why it fails, would need to see a complete sample. But why aren't you just changing the data to "readable" string and back, in order to save them to dbf fields? You can just use Convert.ToBase64String() to convert into text, then convert it back with Convert.FromBase64String() after you read it from the dbf, and this way it will always work, no matter the contents or environmental settings.

.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Wrting byte[] into a memo with FieldPutBytes

Post by NickFriend »

Thanks Chris, I'll try that.

Nick
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Wrting byte[] into a memo with FieldPutBytes

Post by NickFriend »

Actually that's not going to work I think... the dbf has to be read by an old program that expects to find an MS .doc format text as a byte array, so I need to write to the memo field in the same way.

Thanks anyway

Nick
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Wrting byte[] into a memo with FieldPutBytes

Post by Chris »

Hi Nick,

OK, I was afraid that this could be the case. But still, the trick with AdjustBinaryData should still work, I just made a quick test here and it seems to be working. If you can prepare a runable sample showing the problem, I will look into it..

.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Wrting byte[] into a memo with FieldPutBytes

Post by NickFriend »

AdjustBinaryData basically works, but it's just screwing up a bit of the formatting. I suspect that some formatting character is being changed, so maybe it's simply that the Encoding is wrong.

To be honest I know very little about this, so I'm stabbing in the dark a bit. The data comes out of Excel into a C# program as a UTF8 MemoryStream. The encoding for AdjustBinaryData is set using Encoding.GetEncoding(XSharp.RuntimeState.DosCodePage). Do I maybe need to use a different codepage when saving??

Nick
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Wrting byte[] into a memo with FieldPutBytes

Post by Chris »

Hi Nick,

Maybe the dbf is using a different codepage? In any case, I think it would be better to try to narrow down the problem. First of all, create a byte array of 256 length and save in it all bytes 0..255. Try to save it and load it from the dbf, does it work correctly? If yes, then I guess the problem is not in the dbf read/write part of the code. If there's an error in retrieving the array back, at which bytes does this happen?
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply