cancel
Showing results for 
Search instead for 
Did you mean: 

Convert SqlDataReader to Datatable

Former Member
0 Kudos

Hello Forum,

how can I convert a SqlDataReader to a SAP DataTable?

SwissAddonFramework.B1Connector _B1 = SwissAddonFramework.B1Connector.GetB1Connector();

System.Data.SqlClient.SqlDataReader Reader = _B1.ExecuteQuery(sSQL);

DataTable MyTable = ?

Thanks

Mark

Accepted Solutions (1)

Accepted Solutions (1)

pedro_magueija
Active Contributor
0 Kudos

Hi Mark,

You'd have to go through each record and create row in the datatable (MyTable.Rows.Add()) then set the values (MyTable.SetValue(row, column, value)).

If you are populating the grid directly from the query it's probably best to just use the ExecuteQuery method.


Best regards,

Pedro Magueija


View Pedro Magueija's profile on LinkedIn

Former Member
0 Kudos

I want to put the query into a separat thread. Is there a possibility to make a query and get a Datatable back?

Currently I'm thinking of creating a Grid without a GUI in the task, let the Grid doing the query and get the DataTable from the Grid. I think this could work, but it is not a lean solution.

pedro_magueija
Active Contributor
0 Kudos

Hi Mark,

You can just add a datatable to a form and ask it to do the query then assign it to the grid.

However, I think the UI is single threaded so I'm not sure if while the datatable is doing the query the UI will block. Let me know how your experience goes.


Best regards,

Pedro Magueija


View Pedro Magueija's profile on LinkedIn

Former Member
0 Kudos

Hello Pedro,

I tried it with an invisible Grid, but the message was ExecuteQuery is allowed on a loaded grid only This happens even if I put the Grid on a form:


  Form f = Form.CreateNewForm("ajjrr", "gdhedrers");

  Grid UnboundGrid = Grid.CreateNew("sdfgkgrk");

  f.AddItem(UnboundGrid);

  UnboundGrid.ExecuteQuery((string)SQL); // Exception: ExecuteQuery is allowed on a loaded grid only


I will create a function that copies the data from a SqlReader to a datatable.

Thanks

Mark

pedro_magueija
Active Contributor
0 Kudos

Hi Mark,

That issue is not from the SDK, but from the framework you are using. Adding a standard DataTable to a form and calling ExecuteQuery is possible in the standard SDK, even if that DataTable isn't assigned to any grid.


Best regards,

Pedro Magueija


View Pedro Magueija's profile on LinkedIn

Former Member
0 Kudos

You are right, the Gui still freezes when I'm using a second hidden grid to get the data. And if I place the Grid on another hidden form and try to set a datatable obtained from there to the current form I get a System.Runtime.InteropServices.COMException (0xFFFFE1B3): Data Table - invalid Unique Id

My next try will be to query the data within a separat thread and convert it manually to a DataTable. I think that should work.

Answers (0)