Beginners guide: How connect to NAV from a .net project, using C/Front
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
Microsoft Dynamics UK
Microsoft Customer Service and Support (CSS) EMEA
These postings are provided "AS IS" with no warranties and confer no rights. You assume all risk for your use.
Comments
Anonymous
April 18, 2008
This post describes how you can automate importing and exporting of Microsoft Dynamics NAV objects withAnonymous
May 23, 2008
Einleitung: Dieser Beitrag soll einen ersten Einblick in die Programmierung von C/Front zeigen. Wir zeigenAnonymous
July 31, 2008
Hi!!, I'm new in Microsoft Dynamics NAV, the C/Front its the only one Object Model to automate NAV?Anonymous
August 14, 2012
Hi Lars, This is an excellent example of how to use CFront.Net and has worked well in my test desktop application. However when I try to use cfront in the same manner in my web services solution I get an error telling me to make sure the cfront and cfrontsql dlls are in the right location? I've copied them to the .Net framework folders and the IIS folder inside system32. I've also copied them into the folders that make up my web services site but its made no difference, even though eveerything is being developed/hosted on the same machine. Any ideas?Anonymous
July 11, 2014
Hi, I am getting the same error as KarlAnonymous
July 31, 2014
I had the same issue as guys above. I have solved that by switching Platform target from Any CPU to x86 in Project Properties/Build/ in my Visual Studio. Hope it helps Ondrej.Anonymous
June 15, 2015
But can we access navision stand alone database of .FDB format without adding any reference?