top of page
Writer's pictureTabrez Ajaz

Blob Data Type: How to perform read and write operation on Blob fields in Dynamics 365 Business Cent

Updated: Feb 21, 2022

Welcome Dynamics 365 BC Lovers,


Blob Data Type: Blob is a complex data type, it is also known as Binary Large Object and its maximum size is 2 GB. It can be used to store memos (text), pictures (bitmaps), or user-defined types. And the most interesting part of Blob fields is you can’t able to view text that is stored in BLOBs from the development environment, which means if you want to read data from and write data to a BLOB field, you need to use the InStreams and OutStreams objects.


Below methods are available on an instance of the Blob data type whether it is used as field or variable:


Method name

Description

CreateInStream(InStream [, TextEncoding])

Creates an InStream object for a binary large object (BLOB). This enables you to read data from the BLOB.

CreateOutStream(OutStream [, TextEncoding])

Creates an OutStream object for a binary large object (BLOB). This enables you to write data to the BLOB.

Export(String)

Exports a binary large object (BLOB) to a file.

HasValue()

Determines whether a binary large object (BLOB) has a value.

Import(String)

Imports a binary large object (BLOB) from a file.

Length()

Returns the number of bytes in the binary large object (BLOB).


Let’s understand Blob data type with the help of an example, in which I will create 1 table and 1 page, the table will contains a field of Blob data type, in which we will store/retrieve text data with the help of page.


Table Schema Snippet:

table 80003 BlobDemo
{
 DataClassification = ToBeClassified;
 fields
 {
    field(1; "Entry No."; Integer)
    {
        DataClassification = ToBeClassified;
        AutoIncrement = true;
    }
    field(2; "Blob Text"; Blob)
    {
        DataClassification = ToBeClassified;
    }
 }

 keys
 {
    key(Key1; "Entry No.")
    {
        Clustered = true;
    }
 }
}
 

Page Snippet:

page 80003 BlobDemo
{
 Caption = 'BLOB - Demo';
 PageType = List;
 ApplicationArea = All;
 UsageCategory = Administration;
 SourceTable = BlobDemo;
 layout
 {
    area(Content)
    {
        repeater(GroupName)
        {
            field("Entry No."; Rec."Entry No.")
            {
                ApplicationArea = All;
            }
            field(BlobVarText; BlobVarText)
            {
                ApplicationArea = All;
                MultiLine = true;

                trigger OnValidate()
                var
                    outStr: OutStream;
                begin
                    Rec."Blob Text".CreateOutStream(outStr);
                    outStr.WriteText(BlobVarText);
                end;
            }
        }
    }
 }

 trigger OnNewRecord(BelowxRec: Boolean)
 begin
    BlobVarText := '';
 end;

 trigger OnAfterGetRecord()
 var
    inStr: InStream;
 begin
     BlobVarText := '';
    // Read values and Store BlobVarText
    Rec.CalcFields("Blob Text");
    if Rec."Blob Text".HasValue then begin
        Rec."Blob Text".CreateInStream(inStr);
        inStr.ReadText(BlobVarText);
    end
    else
        BlobVarText := 'No value on the Blob field';
 end;

 var
    BlobVarText: Text;    // Text type variable - used to read or write text data to Blob Field
}
  

Result:


I hope you understand how to store or view text data store in Blob data type or field.


Stay Tuned!

2,258 views2 comments

2 Comments


Tabrez Ajaz
Tabrez Ajaz
Dec 26, 2022

Read this module form Microsoft Learn and you will get a lot. https://learn.microsoft.com/en-us/training/modules/work-with-xmlports/ You can use the sample code:

------------------------------------ local procedure ImportCitiesUsingXMLport()

var

TempBlob: Codeunit "Temp Blob";

InStr: InStream;

OutStr: OutStream;

FileName: Text;

XmlExportCities: XmlPort CitiesXmlPort;

FileMng: Codeunit "File Management";

TextRead: Text;

TextData: BigText;

begin

if (File.UploadIntoStream('Select your file', '', FileMng.GetToFilterText('', FileName), FileName, InStr)) then begin

InStr.Read(TextRead);

Message(TextRead);

TextData.AddText(TextRead);

TempBlob.CreateOutStream(OutStr);

TextData.Write(OutStr);

TempBlob.CreateInStream(InStr);

Xmlport.Import(Xmlport::ImportCitiesXmlPort, InStr);

end;

end;

Like

can we use same method for importing data through xml port

Like
bottom of page