Beginners guide: How connect to NAV from a .net project, using C/Front

Beginners guide: How connect to NAV from a .net project, using C/Front

  • Comments 0

Introduction: 

This post describes the simplest possible way to get started with creating a .net project in Microsoft Visual Studio, and use C/Front in c# code to connecct to a Microsoft Dynamics NAV database. The example here is based on the sample from the product CD, which will install to this folder: C:\Program Files\Microsoft Dynamics NAV\SDK\CFRONT\DotNetSampleApplication. More details can be seen from that sample, and I will also make further blog posts to describe further use of C/Front. For this post, all we want to do is to make a connection.

The example here was made in Visual Studio 2008. You can do the same in Visual Studio 2005. The NAV database in this example is running on SQL Server. To make it connect to a Native NAV database you nee to adjust NetType and DriverType.

Time to complete: Approximately 20 minutes.


Example:

C/Front is part of SDK (Software Developers Kit), which also includes the Communication Components. If you haven't installed it already, then install SDK from the NAV product CD. Then follow the steps below:

1)  Open Microsoft visual Studio 2005/2008

2)  Create a new project (File -> New -> Project). As project type, select "Visual c#", then select "Windows Forms Application". Type in a name and click OK.

3)  Include C/Front to your project by rightclicking on "References" on the right hand side, under "Solution Explorer", and select "Add Reference". Go to the Browse-tab, and select Microsoft.Navision.CFront.CFrontDotNet.dll. As default this is installed in this folder: C:\Program Files\Microsoft Dynamics NAV\SDK\CFRONT\.

4)  Now, add a button to your Form (View -> Toolbox, then double-click a button to add it). Also add a TextBox so we have a place to display data from C/Front. Double-click on the button to get to the code.

5)  At the top of the code-window where Visual Studio has already added a number of "using"-commands, also add a line for C/Front: using Microsoft.Navision.CFront;

6)  All the preparations done, we are now ready to program with C/Front. Go back down the the function "private void button1_Click", and copy in the code below. Just adjust values NavisionPath, ServerName and CompanyName to match your system:

// Declare specific C/Front variables

NavisionNetType NetType;

NavisionDriverType DriverType;

NavisionValue Data;

// Set Net Type and Driver Type (SQL or native)

DriverType = NavisionDriverType.Sql;

NetType = NavisionNetType.SqlDefault;

CFrontDotNet.DriverType = DriverType;

int CT; // Currentt Table. Keep name short, since we need to type it in a lot

int CR; // CurrentRecord. Keep name short, as above

CFrontDotNet.NavisionPath = "C:\\Clients\\W15SP1RTM\\";

CFrontDotNet.Instance.ConnectServerAndOpenDatabase("Lohndorf1", NetType, "W1500SP1", 2000, true, true, "", "");

CFrontDotNet.Instance.OpenCompany("CRONUS International Ltd.");

CT = CFrontDotNet.Instance.OpenTable(18);

CR = CFrontDotNet.Instance.AllocRecord(CT);

CFrontDotNet.Instance.FindFirstRecord(CT, CR);

Data = CFrontDotNet.Instance.GetFieldData(CT, CR, 2);

NavisionText NAVText;

NAVText = Data;

textBox1.Text = NAVText.ToString();

// Important: Close down Navision when finished.

CFrontDotNet.Instance.FreeRecord(CR);

CFrontDotNet.Instance.CloseTable(CT);

CFrontDotNet.Instance.CloseCompany();

CFrontDotNet.Instance.CloseDatabase();

CFrontDotNet.Instance.DisconnectServer();

CFrontDotNet.Instance.Dispose();

 

7)  Save the project. As default it will save to this folder: C:\Users\[UserName]\Documents\Visual Studio 2008\Projects\CFrontSampleVS2008\[ProjectName]

8)  Finally, copy the files CFront.dll and CFrontSQL.dll from the C/Front folder (default C:\Program Files\Microsoft Dynamics NAV\SDK\CFRONT) into the project sub-folder bin\Debug


Now try to run it. Build the project first (Buld -> Build [ProjectName]), and check that it builds without errors. Then run it (Debug -> Start Debugging - F5). If succesful, it will display the customer name of the first customer on the form.

Comments:

Always set the NavisionPath and point it to a NAV client folder of the same version as C/Front. This is where C/Front will locate essential files like dlls, flf and stx files. If you don't set this path, you are likely to get error 2 in module 1 ("The system cannot find the file specified.").

C/Front requires granule 1,800 C/FRONT. It connects just like a normal client, so it will also consume a normal user session while connected.

 


Lars Lohndorf-Larsen (Lohndorf )

Microsoft Dynamics UK

Leave a Comment
  • Please add 2 and 2 and type the answer here:
  • Post