on 04-16-2013 6:25 PM
Hello,
I want to know How can I get a table from SAP, using .Net connector from VB.NET or C# and using this table with .net controls like ComboBox, Gridview or Datatable. to show the table content in a combo box o gridview, is this possible?
with .NET Connector we can get a structure, but a structure only has one row:
string GetData = Data.GetStructure("WA_PAPEL").GetString("Country");
But with a table?
The Function should have either a "Tables" argument or a table type output. An example BAPI to look at is BAPI_MATERIAL_GETLIST, which has many table arguments which you can access. On the .NET side, simple call "GetTable" to get an IRfcTable object.
//Set function
IRfcFunction BapiMaterialGetList =
SapRfcRepository.CreateFunction("BAPI_MATERIAL_GETLIST");
//Get reference to table object
IRfcTable So_MATNR = BapiMaterialGetList.GetTable("MATNRSELECTION");
After that you can convert it to a DataTable to set your control DataSource to.
Convert to datatable function:
public static DataTable GetDataTableFromRFCTable(IRfcTable functionRfcTable)
{
DataTable data = new DataTable();
//Create data table.
for (int i = 0; i <= functionRfcTable.ElementCount - 1; i++) {
RfcElementMetadata metadata = functionRfcTable.GetElementMetadata(i);
data.Columns.Add(metadata.Name);
}
//Transfer rows from rfcTable to .Net table.
foreach (IRfcStructure row in functionRfcTable) {
DataRow rowAdd = data.NewRow();
for (int j = 0; j <= functionRfcTable.ElementCount - 1; j++) {
RfcElementMetadata metadata = functionRfcTable.GetElementMetadata(j);
rowAdd(metadata.Name) = row.GetString(metadata.Name);
}
data.Rows.Add(rowAdd);
}
return data;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello, thanks for your fast answer, but I have some issues, when I try to run this code, visual studio shows some errors:
Could you please help me with this issue?
My complete code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SAP.Middleware.Connector;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
RfcDestination _ecc;
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
try
{
_ecc = RfcDestinationManager.GetDestination("ECDCLNT140");
if (_ecc == null)
{
RfcDestinationManager.RegisterDestinationConfiguration(new ECCDestinationConfig());
_ecc = RfcDestinationManager.GetDestination("ECDCLNT140");
}
}
catch (Exception a)
{
Response.Write(a);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//Set function
IRfcFunction BapiMaterialGetList = SapRfcRepository.CreateFunction("BAPI_MATERIAL_GETLIST");
//Get reference to table object
IRfcTable So_MATNR = BapiMaterialGetList.GetTable("MATNRSELECTION");
}
public static DataTable GetDataTableFromRFCTable(IRfcTable functionRfcTable)
{
DataTable data = new DataTable();
//Create data table.
for (int i = 0; i <= functionRfcTable.ElementCount - 1; i++)
{
RfcElementMetadata metadata = functionRfcTable.GetElementMetadata(i);
data.Columns.Add(metadata.Name);
}
//Transfer rows from rfcTable to .Net table.
foreach (IRfcStructure row in functionRfcTable)
{
DataRow rowAdd = data.NewRow();
for (int j = 0; j <= functionRfcTable.ElementCount - 1; j++)
{
RfcElementMetadata metadata = functionRfcTable.GetElementMetadata(j);
rowAdd(metadata.Name) = row.GetString(metadata.Name);
}
data.Rows.Add(rowAdd);
}
return data;
}
}
Sorry! I work mostly in VB.
Thanks!
abusing of your hospitality, I've one issue, but I don't know why c# show me this error:
I have worked with VB.NET to get an structure and it's very easy to solve with this code in my load page:
Try
_ecc = RfcDestinationManager.TryGetDestination("ECDCLNT140")
If (_ecc Is Nothing) Then
RfcDestinationManager.RegisterDestinationConfiguration(New ECCDestinationConfig())
_ecc = RfcDestinationManager.GetDestination("ECDCLNT140")
End If
' GetCompanyName()
'TextRolloNum.Text = ""
TextRolloNum.Focus()
Catch ex As Exception
End Try
But in My C# I have this:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
try
{
_ecc = RfcDestinationManager.GetDestination("ECDCLNT140");
if (_ecc == null)
{
RfcDestinationManager.RegisterDestinationConfiguration(new ECCDestinationConfig());
_ecc = RfcDestinationManager.GetDestination("ECDCLNT140");
}
}
catch (Exception a)
{
Response.Write(a);
}
}
Do you know what I'm doing wrong?
Do you have an example with vb.net that does work well?
Thanks.
User | Count |
---|---|
84 | |
24 | |
12 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.