Search code examples
c#gridviewsapb1sap-business-one-di-api

Display All BP info in a Gridview


I managed to connect to SAP B1 database using C# and SAP DI API version 9.00 and display ONE BP info.

What I want to do is display all BPs or one BP info in a GridView.

What I couldn't figure out is displaying all BPs/one BP info in a Gridview. In other words how to bind to a Gridview?

Thank you!

This is my code to connect which works perfect:

SAPbobsCOM.Company vCmp = new SAPbobsCOM.Company();
int lRetCode, lErrCode;
string sErrMsg;
vCmp.Server = "SAPR01";
vCmp.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2008;
vCmp.CompanyDB = "company";
vCmp.UserName = "user";
vCmp.Password = "*****";
vCmp.language = SAPbobsCOM.BoSuppLangs.ln_English;
lRetCode = vCmp.Connect();

if (lRetCode == 0)
{
    label1.Text = "Hello World!";
}
else
{ 
    label1.Text = "Connection Failed!"; 
}

lErrCode = vCmp.GetLastErrorCode(); sErrMsg = vCmp.GetLastErrorDescription();
vCmp.GetLastError(out lErrCode, out sErrMsg);  

and this is my code to display BP's Name using a label:

SAPbobsCOM.BusinessPartners BP;
BP = vCmp.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oBusinessPartners);
string  cardName;

if (BP.GetByKey("C70000"))
{
    cardName = BP.CardName;
    label1.Text = cardName;
}

Solution

  • After hours of searching and trying a lot of code, I managed to display BP's info. using this code:

    SAPbobsCOM.SBObob obj = vCmp.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoBridge);
            SAPbobsCOM.Recordset rs = vCmp.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
            rs = obj.GetBPList(SAPbobsCOM.BoCardTypes.cCustomer);
            rs.MoveFirst();
    
            int count = rs.RecordCount;
            grdBP.Rows.Clear();
            if (count > 0)
            {
                grdBP.Columns.Add("cardCode", "BP Code");
                grdBP.Columns.Add("cardName", "BP Name");
                grdBP.Columns.Add("cardType", "BP Type");
                grdBP.Rows.Add(count);
                int gridCounter = 0;
                while (!rs.EoF)
                {
                    DataGridViewRow row = grdBP.Rows[gridCounter];
                    row.Cells[0].Value = rs.Fields.Item("cardCode").Value;
                    row.Cells[1].Value = rs.Fields.Item("cardName").Value;
                    row.Cells[2].Value = rs.Fields.Item("cardType").Value;
                    gridCounter++;
                    rs.MoveNext();
                }
            }
    

    i had to create my columns and rows manually to be able to use Recordset
    I used C# Windows Form.