stanley1610
Programmer
I am going to write a C# program to connect SAP.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using SAPLogonCtrl;
using SAPBAPIControlLib;
namespace SapExplorer
{
public partial class frmSapExplorer : Form
{
SAPLogonControlClass sapLogon;
SAPBAPIControlClass sapBapi;
SAPLogonCtrl.Connection sapConnection;
public frmSapExplorer()
{
InitializeComponent();
sapLogon = new SAPLogonControlClass();
sapBapi = new SAPBAPIControlClass();
}
private bool logonSAP()
{
sapLogon.ApplicationServer = ConfigurationSettings.AppSettings["sapserver"];
sapLogon.Client = ConfigurationSettings.AppSettings["sapclient"];
sapLogon.System = ConfigurationSettings.AppSettings["sapsystem"];
sapLogon.Language = ConfigurationSettings.AppSettings["saplang"];
sapLogon.User = txtUsername.Text;
sapLogon.Password = txtPassword.Text;
sapConnection = (SAPLogonCtrl.Connection)sapLogon.NewConnection();
return sapConnection.Logon(0, true);
}
private void logoffSAP()
{
sapConnection.Logoff();
}
private void btnLogon_Click(object sender, EventArgs e)
{
if (logonSAP())
{
MessageBox.Show("Logon OK");
toolStripStatusLabel1.Text = "SAP is connected";
}
else
{
MessageBox.Show("Logon NG");
logoffSAP();
toolStripStatusLabel1.Text = "SAP is disconnected";
}
}
private void btnLogoff_Click(object sender, EventArgs e)
{
logoffSAP();
MessageBox.Show("Logoff SAP");
toolStripStatusLabel1.Text = "SAP is disconnected";
}
private void btnCallBapi_Click(object sender, EventArgs e)
{
sapBapi.Connection = sapConnection;
Object bapiService = sapBapi.GetSAPObject("BapiService", "", "", "", "", "", "", "", "", "", "");
MessageBox.Show(bapiService.ToString());
}
}
}
When I run sapBapi.Connection = sapConnection;
it prompted System.AccessViolationException.
What should I do?
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using SAPLogonCtrl;
using SAPBAPIControlLib;
namespace SapExplorer
{
public partial class frmSapExplorer : Form
{
SAPLogonControlClass sapLogon;
SAPBAPIControlClass sapBapi;
SAPLogonCtrl.Connection sapConnection;
public frmSapExplorer()
{
InitializeComponent();
sapLogon = new SAPLogonControlClass();
sapBapi = new SAPBAPIControlClass();
}
private bool logonSAP()
{
sapLogon.ApplicationServer = ConfigurationSettings.AppSettings["sapserver"];
sapLogon.Client = ConfigurationSettings.AppSettings["sapclient"];
sapLogon.System = ConfigurationSettings.AppSettings["sapsystem"];
sapLogon.Language = ConfigurationSettings.AppSettings["saplang"];
sapLogon.User = txtUsername.Text;
sapLogon.Password = txtPassword.Text;
sapConnection = (SAPLogonCtrl.Connection)sapLogon.NewConnection();
return sapConnection.Logon(0, true);
}
private void logoffSAP()
{
sapConnection.Logoff();
}
private void btnLogon_Click(object sender, EventArgs e)
{
if (logonSAP())
{
MessageBox.Show("Logon OK");
toolStripStatusLabel1.Text = "SAP is connected";
}
else
{
MessageBox.Show("Logon NG");
logoffSAP();
toolStripStatusLabel1.Text = "SAP is disconnected";
}
}
private void btnLogoff_Click(object sender, EventArgs e)
{
logoffSAP();
MessageBox.Show("Logoff SAP");
toolStripStatusLabel1.Text = "SAP is disconnected";
}
private void btnCallBapi_Click(object sender, EventArgs e)
{
sapBapi.Connection = sapConnection;
Object bapiService = sapBapi.GetSAPObject("BapiService", "", "", "", "", "", "", "", "", "", "");
MessageBox.Show(bapiService.ToString());
}
}
}
When I run sapBapi.Connection = sapConnection;
it prompted System.AccessViolationException.
What should I do?