Day by day I am moving closer to DotNet programming in Dynamics NAV. More and more of the things I like to do are more easily solved with DotNet than with native C/AL code.

In most cases I can use the standard DotNet types but in some cases I need to build a small DotNet Class to solve the problem.

An example of this landed on my desk yesterday. A colleague of mine needed to be able to print text to the local label printer. Perhaps this can be solved with a simple report but in this case something more was needed. I asked the Internet – how do I print a text file with c# code. Got some answers and selected the one I liked the most.

Normally I rewrite the c# code with DotNet objects in C/AL but in this case I could not. The reason was that I needed the DotNet objects to be executed on the client side, and using DotNet to print requires an event handler. DotNet events are not supported on the client side so I needed to create a class.

I create a c# Class Project in Visual Studio and can use the code I found with only a few modifications.

[code lang=”csharp”] public class NAVTextFilePrinter
{
public void PrintText(string printerName, string fontName, float fontSize, string[] linesToPrint)
{
Font printFont = new Font(fontName, fontSize);
PrintDocument docToPrint = new PrintDocument();
docToPrint.DocumentName = "NAV Text File";
docToPrint.PrinterSettings.PrinterName = printerName;
docToPrint.PrintPage += (s, ev) =>
{
int count = 0;
float yPos = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;

foreach (string line in linesToPrint)
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
count++;
}
ev.HasMorePages = false;
};
docToPrint.Print();
}
}
[/code]

The class is compiled with DotNet 4.5 and added to the Client Add-ins folder. If you are using NAV version 2015 or newer just add it to the Server Add-ins folder.

Using this in NAV is easy

PrintNAVText

Microsoft.Dynamics.Nav.Client.TextFilePrinter can be downloaded from here.

I like to use DotNet when I am handling text – in general. The DotNet object System.String has a lot of functions that can be useful. Just use

String := String.Copy(NAVString);

and your NAV String can now be handled with all the functionality available with DotNet.  Good example is the Renumbering Tool I created a few months ago.

Another example here – where I need to create all the directory tree leading to the file I need to copy.

CreateDirectoryTree

One of my favorite is the Global Variable Store Codeunit, where I use the DotNet Dictionary to store variables globally. This can be used to minimize the footprint of your code changes. For example, if you need to pass a new variable to a function and you don’t want to change the function – just store the variable before you call the function and retrieve it inside the function. It is even possible to pass a whole record this way.

Downloadable Global Variable Store Codeunit

 

5 thoughts on “Just use DotNet

  1. Just found a problem with the Dictionary.TryGetValue function. I can’t use the NAV Variant value in that function because it needs to be nullable. Using System.Object instead and changing the code to

    GetValue(ValueName : Code[20];VAR Value : Variant) ValueFound : Boolean
    Initialize;
    IF Dictionary.TryGetValue(ValueName,NullableValue) THEN BEGIN
    Value := NullableValue;
    EXIT(TRUE);
    END ELSE
    EXIT(FALSE)

  2. Hello,
    If you don’t mind can you please explain what is the deferent between automation and DotNet data type ? Is there any way to create our own automation file and call it from NAV ?

    1. Automation is a file based on the COM model. You can use automation in the windows client only and only running on the client side. COM is 32bit only so you will need the 32bit version of the windows client from NAV 2016 going forward.

      1. Thank you so much for your comment. I have leaned new thing from you

      2. Dear Gunner,, I am using Automation datatype variable in my code. In the server it is working fine, when user access through client it is giving me an error. Do I have to register .dll in client pc too ?

        If I use dotnet variable ,is it required to have .dll in the client pc ?

Leave a Reply

%d bloggers like this: