Hi
I have below code but i am getting above error on CreateConnection();
C#
static class Program
{
public static SAPbouiCOM.Application oApplication = null;
public static SAPbobsCOM.Company oCompany = null;
[STAThread]
static void Main(string[] args)
{
try
{
CreateConnection();
Menu oMenuItems = null;
oMenuItems = new Menu();
// Start Message Loop
System.Windows.Forms.Application.Run();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}Thanks

Amit MohantyRank #112 · 20K reputationPosted Apr 19, 2024, 2:10 AM EDT
This error occurs when trying to access an instance member from a static context. I think CreateConnection() is likely an instance method, and you're trying to call it from a static context in your Main method. If possible, make the CreateConnection() method to be static if it doesn't rely on instance-specific data. For example:
static void CreateConnection() { // Your connection logic here }Otherwise you'll need to create an instance of the class it belongs to and then call the method on that instance. For example:
static void Main(string[] args) { try { Program program = new Program(); program.CreateConnection(); Menu oMenuItems = new Menu(); System.Windows.Forms.Application.Run(); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } } public void CreateConnection() { // Your connection logic here }