Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SAP BAPI .NET

Status
Not open for further replies.

stanley1610

Programmer
Mar 22, 2004
42
0
0
HK
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?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top