cancel
Showing results for 
Search instead for 
Did you mean: 

Company.connect error

Former Member
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

rasmuswulff_jensen
Active Contributor
0 Kudos

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
    }
  }
}

Answers (2)

Answers (2)

AdKerremans
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Hi Rashmi,

You should pass your connection string after the connect method like

lngConnect = oCompany.Connect(connectstr)

Hope it helps,

Adele