Skip to Content
author's profile photo Former Member
Former Member

Company.connect error

The following code snippet is being used to connect to the company:

Dim connectstr As String

Dim oCompany As SAPbobsCOM.Company

Dim lngConnect As Long

oCompany = New SAPbobsCOM.Company

oCompany.Server = SBO_Appln.Company.ServerName

oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL

oCompany.CompanyDB = SBO_Appln.Company.DatabaseName

oCompany.UserName = SBO_Appln.Company.UserName

connectstr = SBO_Appln.Company.GetConnectionContext(oCompany.GetContextCookie)

Try

lngConnect = oCompany.Connect

Catch ex As Exception

MsgBox(ex.Message)

End Try

The statement "oCompany.connect" is generating the exception "Object Reference not set to an instance of an object" though a new instance of oCompany is created.

Add a comment
10|10000 characters needed characters exceeded

Related questions

3 Answers

  • Best Answer
    Posted on Oct 19, 2005 at 01:40 PM

    Hmmm a strange way to do a single signon. Look at the single signon sample or follow this code:

    //UI Conncetion
    SboGuiApi SboGui = new SboGuiApiClass(); 
    Application _SboApplication; 
    string conStr = Environment.GetCommandLineArgs()[1];
    SboGui.Connect(conStr);
    _SboApplication = SboGui.GetApplication(-1);
    
    //DI (Single signon)
    SAPbobsCOM.Company _SboCompany = new SAPbobsCOM.CompanyClass(); 
    string cookie = SboCompany.GetContextCookie();
    string conStr = _SboApplication.Company.GetConnectionContext(cookie);
    SboCompany.SetSboLoginContext(conStr);
    if(!SboCompany.Connected) {
      if(SboCompany.Connect() != 0) {
        //Error
      }
      else {
        //Connection OK
      }
    }
    

    If its not a Single signon conncetion you are after use the followeing function

    private static SAPbobsCOM.Company _SboCompany = new SAPbobsCOM.CompanyClass(); 
    
    /// <summary>
    /// Create a DI Connection
    /// </summary>
    /// <param name="Server">Name of the server</param>
    /// <param name="Username">The SBO Username</param>
    /// <param name="Password">The SBO Password</param>
    /// <param name="DatabaseName">The Databasename</param>
    /// <param name="DbUsername">The Username for the MSSQL (Not used if UseTrusted = true)</param>
    /// <param name="DbPassword">The Password for the MSSQL (Not used if UseTrusted = true)</param>
    /// <param name="LicenseServer">Name of the license server</param>
    /// <param name="UseTrusted">If connection should use a trusted connection to Connect to MSSQL</param>
    /// <param name="Language">The connection language</param>
    public static void Connect(string Server, string Username, string Password, string DatabaseName, string DbUsername, string DbPassword, string LicenseServer,bool UseTrusted, BoSuppLangs Language) {
      SboCompany.Server = Server;
      SboCompany.UserName = Username;
      SboCompany.Password = Password;
      SboCompany.CompanyDB = DatabaseName;
      SboCompany.DbUserName = DbUsername;
      SboCompany.DbPassword = DbPassword;
      SboCompany.LicenseServer = LicenseServer;
      SboCompany.UseTrusted = UseTrusted;
      SboCompany.language=Language;
      SboCompany.DbServerType=BoDataServerTypes.dst_MSSQL;
      if(!SboCompany.Connected) {
        if(SboCompany.Connect() != 0) {
    	//Error
        }
      }
    }
    

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 19, 2005 at 01:35 PM

    Hi Rashmi,

    You should pass your connection string after the connect method like

    lngConnect = oCompany.Connect(connectstr)

    Hope it helps,

    Adele

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 19, 2005 at 01:42 PM

    MY single logon connect part is

    Dim sContextCookie As String

    Dim sConnContext As String

    Try

    oCmp = New SAPbobsCOM.Company

    sContextCookie = oCmp.GetContextCookie

    sConnContext = oApp.Company.GetConnectionContext(sContextCookie)

    oCmp.SetSboLoginContext(sConnContext)

    oCmp.Connect()

    Return True

    Catch ex As System.Exception

    MsgBox(ex.Message + vbCrLf + ex.StackTrace, MsgBoxStyle.Critical, "SBO BAPI ERROR")

    Return False

    End Try

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.