Skip to Content
0
Former Member
May 31, 2005 at 12:59 AM

DIAPI: Getting NullReferenceException trying to connect

245 Views

Here's a very simple VB.NET program to connect to an SBO company. The console displays all the company names just fine but trying to connect produces a NullReferenceException in Windows (all the details are in the comments of the code). I have 6.70.188 (patch 21) and I've double checked that my SDK matches (by trying to re-install it. Is there a better way?)

Module Module1

    Sub Main()

        Dim sboCompany As SAPbobsCOM.Company
        Dim connectVal As Integer
        Dim companies As SAPbobsCOM.Recordset
        Dim errCode As Integer
        Dim errMsg As String

        sboCompany = New SAPbobsCOM.Company
        sboCompany.Server = "(local)"
        sboCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL
        sboCompany.language = SAPbobsCOM.BoSuppLangs.ln_English
        sboCompany.UseTrusted = False
        sboCompany.DbUserName = "sa"
        sboCompany.DbPassword = "your_password_here"

        companies = sboCompany.GetCompanyList()
        sboCompany.GetLastError(errCode, errMsg)
        If errCode Then
            MsgBox(errMsg)
            Exit Sub
        End If

        companies.MoveFirst()
        Do Until companies.EoF = True
            Console.WriteLine(companies.Fields.Item(0).Value & "," & vbTab _
                            & companies.Fields.Item(1).Value)
            companies.MoveNext()
        Loop

        ' Console displays:

        'DBSC,   Direct Broadcast Satellite Center
        'SBOBS_Support(, Support)
        'SBODemo_US,     OEC Computers
        'Toklat, Toklat Originals

        sboCompany.CompanyDB = "DBSC"
        sboCompany.UserName = "manager"
        sboCompany.Password = "manager"

        connectVal = sboCompany.Connect

        ' Windows error message:

        'An unhandled exception of type 'System.NullReferenceException' occurred in ConsoleApplication2.exe
        'Additional information: Object reference not set to an instance of an object.

        If connectVal <> 0 Then
            sboCompany.GetLastError(errCode, errMsg)
            MsgBox(errMsg)
        Else
            MsgBox("Connected")
        End If

    End Sub

End Module