NAV 5.0 SP1 and CFRONT vb.Net Example (VS 2008 SP1)

wreese@guardian.comwreese@guardian.com Member Posts: 2
Hello everyone,

I've been searching for a good cfront example to simply connect to our NAV database -- alas, none to be found. Anyway, I converted one of the C# examples and finally got this to work. Of course, you'll need to include the Microsoft.Navision.CFront.CFrontDotNet wrapper from the SDK folder as a reference to VS 2008 and follow the readme guidelines for placing the cfront*.dlls. You'll also need to change the server name, etc. to suite your needs.

All this example does is display the name of a customer in a message box, but it will get you started. It uses windows authentication while I'm testing on my laptop (you can tweak the parameters to ConnectServerAndOpenDatabase()). If anyone finds more vb.net examples out there (not vb6) or a better vb.net way to perform a simple connect like below, please let me know....

Hope this helps someone and by the way, appreciate all the posts out there -- mibuso is a great resource!

See ya,

Bill
Public Class frmCFront

  Const SERVER_NAME As String = "PC001799\SQLDEV"
  Const DATABASE_NAME As String = "GAPI5"
  Const COMPANY_NAME As String = "Guardian Automotive Products"

  Const TBL_Customer As Integer = 18


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim CFR As Microsoft.Navision.CFront.CFrontDotNet
  Dim iTable As Integer, iRec As Integer
  Dim navtxtName As Microsoft.Navision.CFront.NavisionText

  Microsoft.Navision.CFront.CFrontDotNet.DriverType = _
    Microsoft.Navision.CFront.NavisionDriverType.Sql

  CFR = Microsoft.Navision.CFront.CFrontDotNet.Instance

  Try
    CFR.ConnectServerAndOpenDatabase(SERVER_NAME, _
                                     Microsoft.Navision.CFront.NavisionNetType.SqlDefault, _
                                     DATABASE_NAME, 10000, False, True, "", "")


    CFR.OpenCompany(COMPANY_NAME)
    iTable = CFR.OpenTable(TBL_Customer)
    iRec = CFR.AllocRecord(iTable)

    ' Set filter on a "Customer"."No."
    CFR.SetFilter(iTable, CFR.FieldNo(iTable, "No."), "=2504")

    ' Find the first record 
    CFR.FindFirstRecord(iTable, iRec)

    ' Obtain "Customer"."Name"
    navtxtName = CFR.GetFieldData(iTable, iRec, CFR.FieldNo(iTable, "Name"))

    MsgBox(navtxtName.ToString)

    CFR.FreeRecord(iRec)

    CFR.CloseTable(iTable)

    CFR.CloseCompany()

    CFR.CloseDatabase()

    CFR.DisconnectServer()

  Catch ex As Exception
    MsgBox(ex.Message)
  Finally
    CFR.Dispose()
    CFR = Nothing
  End Try

End Sub

End Class

Bill Reese
Guardian Aftermarket IT Manager
Sign In or Register to comment.