cancel
Showing results for 
Search instead for 
Did you mean: 

DIAPI: Getting NullReferenceException trying to connect

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Try resetting the company object after you have displayed them

i.e.

sboCompany = Nothing

sboCompany = New SAPbobsCOM.Company

sboCompany.Server = "(local)"

insert the above snippet just before

your code line

sboCompany.CompanyDB = "DBSC"

seemed to work when I tried it

Former Member
0 Kudos

Thanks for the reply.

No, that moved the problem down below the listing of the companies. I only included the company listing to prove that the server can be queried. I'm really just trying to connect.

BTW, this all works fine in VB6, and the other VB.NET samples work fine too. Why this simple code would fail is baffling.

Former Member
0 Kudos

what happens when you use

sboCompany.UseTrusted = True

also if you are using vb6 you may need to use the 'set' command

i.e.

set sboCompany = New SAPbobsCOM.Company

(Not required in .net)

Former Member
0 Kudos

Nothing, exactly the same.

(Using VB.NET)

Former Member
0 Kudos

dont suppose you could zip up your project to see if it does the same thing on my workstation.

My email is gsavery@vialogic.com.au

Former Member
0 Kudos

Never mind, I'm an idiot.

The SAPbobs DLL was the wrong version, dispite what I said. SAP Support showed me how to double check it.

For the record, you do it like this:

Find SAPbobsCOM67.dll, usually in:

C:\Program Files\SAP Manage\SAP Business One DI API\DI API 67\Object

Right-click, go to Properties

Click on Version tab

You can see the base version (e.g. 6.7.188.0)

Click on "Special Build Description"

You should see "21" or whatever your patch level is.

Thanks to those who looked into it for me.