cancel
Showing results for 
Search instead for 
Did you mean: 

.NET function ToADODataTable does not work

Former Member
0 Kudos

Hi,

We need help on using SAPTable.ToADODataTable and

SAP.Table.FromADODataTable methods

DataTables are used extensively in our application, and we need to get the data or structure (as in the example below) from an SAPTable, modify it, and pass it back to

SAP.

In SAP .NET Connector 1.0 manual it is stated that for

FromADODataTable method to work the schema of DataTable should be identical. Yet even with the simple C# example below, which only adds a row to the DataTable without modifying any other parameters, FromADODataTable method is not able to convert the resulting DataTable to a SAPTable.

Please advise what the recommended method of getting data from DataTable into a SAPTable is.

Thank you in advance.

Source code:

// This contains all the needed table structure and no rows

BAPILOCRANGETable tblValue = new BAPILOCRANGETable();

DataTable tblTmp = tblValue.ToADODataTable();

DataRow rw = tblTmp.NewRow();

rw["Sign"] = "I";

rw["Option"] = "CP";

rw["Low"] = "*";

tblTmp.Rows.Add( rw );

// Crashes on the next line, error is:

// "Object type cannot be converted to target type"

tblValue.FromADODataTable( tblTmp );

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi, I think If you change you code like this, it will go well

BAPILOCRANGETable tblValue = new BAPILOCRANGETable();

DataTable tblTmp = tblValue.ToADODataTable();

DataRow rw = tblTmp.NewRow();

rw["Sign"] = "I";

rw["Option"] = "CP";

rw["Low"] = "*";

rw["High"] = ""; // pay attention to this line!

tblTmp.Rows.Add( rw );

tblValue.FromADODataTable( tblTmp );

I find before call the FromADODataTable, all of the fields should be no-null, if not, it will throw out the error you mentioned.

reiner_hille-doering
Active Contributor
0 Kudos

Let me also add some information to the discussion:

Whenever you want to do data manipulations, better directly do in on the SAP Table instead of converting to and from DataTable. Each of the operation is a full data copy. Also the DataTable as a generic container is much less efficient than the type SAP Tables. I don't see a significat feature that you cannot do with SAP Table or that is more complicated with it.

Anyway, if you need to use DataTable, note that they have two kinds of "null" values: Null and DBNull. Having an uninitialized field causes the entry to be DBNull and not Null. This special value cannot be easily converted to anything - that's the reason for the error message.

Former Member
0 Kudos

Hi All,

I'm in a situation where i'm adding new rows to my SAPTable using the SAPstructure, then displaying in a datagrid what i've added to the SAPTable. It all appears to work but I only end up with 1 record from 3 in my datagrid. Anyone got any ideas?

Code shown below:

string commandtext = "select * from SAPUKLIB.BAPIE1MARA";

iDB2Command cmd = new iDB2Command(commandtext);

cmd.Connection = conn;

iDB2DataAdapter adapter = new iDB2DataAdapter(cmd);

DataTable client_table = new DataTable();

adapter.Fill(client_table);

foreach (DataRow da in client_table.Rows)

{

clientdatastruct.Allowed_Wt = Convert.ToDecimal(da["BMR042"]);

clientdata.CreateNewRow();

clientdata.Add(clientdatastruct);

}

dataGrid1.DataSource = clientdata;

dataGrid1.Refresh();

Message was edited by:

Jim Kiriazis

Answers (4)

Answers (4)

Former Member
0 Kudos

Do you create the tables using the wizard or do you just declare them in the code? It may just be in my head but it seems like it is easier to manipulate the table if I drop it in using the wizard.

Former Member
0 Kudos

> Do you create the tables using the wizard or do you

> just declare them in the code? It may just be in my

> head but it seems like it is easier to manipulate the

> table if I drop it in using the wizard.

What advantages does the wizard give?

The issue is that I am creating and manipulating tables dynamically, using Activator.CreateInstance for a given Type. So the wizard would not help me anyway, yet it would be good to know why it could be useful in general case.

Former Member
0 Kudos

I looked at the code the wizard puts in the form and it may give you something to look at. Today is not a really good day for me to put together any sample code. So I hope this helps. I'll see if I can't generate a table from code later today or early tomorrow, If I have time I'll post the results

It creates a new instance of the table in the web form generated section of the code.

Me.ZActualsTableSAD = New SAPProxyAdmin.ZPLANNINGDATATable()

It then initializes the tables

CType(Me.ZActualsTableSAD, System.ComponentModel.ISupportInitialize).BeginInit()

CType(Me.BdcmsgcollTable1, System.ComponentModel.ISupportInitialize).BeginInit()

CType(Me.T100Table2, System.ComponentModel.ISupportInitialize).BeginInit()

It sets properties on the data table

Me.ZActualsTableSAD.AllowEdit = True

Me.ZActualsTableSAD.AllowNew = True

Me.ZActualsTableSAD.AllowRemove = True

Me.ZActualsTableSAD.SupportsChangeNotification = True

It then ends the initialization

CType(Me.ZActualsTableSAD, System.ComponentModel.ISupportInitialize).EndInit()

CType(Me.BdcmsgcollTable1, System.ComponentModel.ISupportInitialize).EndInit()

CType(Me.T100Table2, System.ComponentModel.ISupportInitialize).EndInit()

I'll dynamically create a table and I'll get back with you on the results.

Former Member
0 Kudos

I have been able to get the ToADODataTable and fromADODataTable working, and can say I have experienced no issues.

Hear is some vb code...

dim exptable as datatable

dim er as datarow

exptable = ZplanexpenseTable1.ToADODataTable

I then manipulate it like any other ADO table

er = exptable.NewRow

er(0) = reader3.getvalue(1)

.....

exptable.Rows.Add(er)

then when I am done building the table

ZplanexpenseTable1.FromADODataTable(exptable)

I have had no issues and I have had this code in a production environment for a long time.

Former Member
0 Kudos

Todd, thanks for your reply. Your code looks identical to ours (posted by Robin), yet yours works and ours doesn't. This means that we are probably facing some configuration issue which we will need to research.

Yet thank you for assuring me that it SHOULD work. This is good to know.

Regards,

Anton Maslo

Former Member
0 Kudos

Hi, there,

Have you also tried to add a SAPStructure instance directly to your SAPTable?

It may look like:

BAPILOCRANGETable tblValue = new BAPILOCRANGETable();

BAPILOCRANGE tmpStructure = new BAPILOCRANGE();

tmpStructure.Sign = "I";

tmpStructure.Option = "CP";

tmpStructure.Low = "*";

tblValue.Add(tmpStructure);

It may not be a elegant solution, but I think it works

Hope it helps...

Regards

Former Member
0 Kudos

> Have you also tried to add a SAPStructure instance

> directly to your SAPTable?

This helped, thank you very much!

Yet does this mean that FromADODataTable method officially does not work? Very strange to hear that.

Regards,

Anton Maslo

Message was edited by: Anton Maslo

Former Member
0 Kudos

Hi,

please check that the field-types in "tblValue" are the same as in "rw". Use explicit type-casting for DataRow values.

Or use datasets and specify the fields in the dataset-shema exact like the field-types used in your SAP-structure.

Something like

sqlDataAdapter.Fill(dataset.viewName);

sapTabeleStructure.FromADODataTable(dataset.viewName);

Regards

Gerhard

Message was edited by: Gerhard Rausch