Save SAP RFC structure to MSSQL table? - asp.net

Recently i am working on a project that required to fetch the data from SAP table to MICROSOFT SQL table ,I have completed the connection part and i also pulled the SAP structure values into ASP.NET.
My question is how to save the structure into Sql table ?
here is my code ;
RfcDestination mydestination = RfcDestinationManager.GetDestination("rfcecp");
RfcRepository myrepository = mydestination.Repository;
IRfcFunction EHSIMSFM = myrepository.CreateFunction("ZEHSIMS");
EHSIMSFM.Invoke(mydestination);
IRfcTable positable = EHSIMSFM.GetTable("POSITIONTAB1");
if (positable.RowCount > 0)"THIS IS THE SAP STRUCTURE WITH ROW COUNT '300'."
{
posid.Text = "working";
}
else
{
posid.Text = "notworking";
}
}
catch (Exception ej)
{
posid.Text = ej.Message;
}

You can gather all the information from a returning IRfcTable via NCo 3 SAP Connector.
Here is the way to traverse each row (IRfcStructure) from the IRfcTable object..
foreach (IRfcStructure row in rfcTable)
{
for (int element = 0; element < rfcTable.ElementCount; element++)
{
RfcElementMetadata metadata = rfcTable.GetElementMetadata(element);
// row.GetString(metadata.Name); // get value from row's field
}
// You have your data from a row here..
}
Example: Here is the code which converts IRfcTable into DataTable.
public static DataTable CreateDataTable(IRfcTable rfcTable)
{
var dataTable = new DataTable();
for (int element = 0; element < rfcTable.ElementCount; element++)
{
RfcElementMetadata metadata = rfcTable.GetElementMetadata(element);
dataTable.Columns.Add(metadata.Name);
}
foreach (IRfcStructure row in rfcTable)
{
DataRow newRow = dataTable.NewRow();
for (int element = 0; element < rfcTable.ElementCount; element++)
{
RfcElementMetadata metadata = rfcTable.GetElementMetadata(element);
newRow[metadata.Name] = row.GetString(metadata.Name);
}
dataTable.Rows.Add(newRow);
}
return dataTable;
}
Similarly, you can add your data into MSSQL DB.

Related

Blackberry app very slow why using Sqlite Queries in loop

I am working on bb app , app is complete but its very slow while selecting data from sqlite ,I am not able to find any solution to boost up the performance, please see code below
for(int a=0;a<200;a++){
retrieveCode(selectQuery);
}
where
public String retrieveCode(String query)
{
String s = "";
Vector v = new Vector();
try
{
g.vec_Result = new Vector();
// Read in all records from the Category table
Statement statement = _db.createStatement(query);//"SELECT * FROM samples"
statement.prepare();
Cursor cursor = statement.getCursor();
Row row;
// Iterate through the result set. For each row, create a new
// Category object and add it to the hash table.
while(cursor.next())
{
row = cursor.getRow();
s = row.getString(0);
v.addElement(vmd);
}
statement.close();
cursor.close();
}
catch(DatabaseException dbe)
{
g.errorDialog(dbe.toString());
}
catch(DataTypeException dte)
{
g.errorDialog(dte.toString());
}
s = v.firstElement().toString();
return s;
}
I am developing for OS5.0 , please help

how to set datarow to String array?

I have a datatable on which I am applying Select() method. After that I want to get the result into an array of String. I have tried the following code but it is not working.
Please let me know the exact solutions for this.
DataTable dtget = HttpContext.Current.Cache["Hello"] as DataTable;
string sqlFilter = "Securityid = '" + CommonClass.Encryptdata(txtSecurity.Text) + "'";
DataRow[] dr = dtget.Select(sqlFilter);
if (dr.Length > 0)
{
String[] Con;
for (int i = 0; i <= dr.Length; i++)
{
dr[i] = dr[i].ToString();
}
}
You have to create the List<T>.
Something like this:
List<String[]> list=new List<String[]>();
if (dr.Length > 0)
{
for (int i = 0; i < dr.Length; i++)
{
string []ar=new string[dtget.Columns.Count];
for(int j=0;j<dtget.Columns.Count;j++)
{
ar[j]=dr[i][j].ToString();
}
list.Add(ar);
}
}
You've declared the array, but not instantiated it. and you're also not writing anything to the actual array, you're trying to write straight back to the datatable.
String[] Con= new String[dtget.Columns.Count];
for (int i = 0; i <= dr.Length; i++)
{
Con[i] = dr[i].ToString();
}
The problem is that you are assigning values to the datarrow in stead of to your string array.
Change
dr[i] = dr[i].ToString();
to
con[i] = dr[i].ToString();
also you are missing the new statement for Con:
Con = new String[dr.Table.Columns.Count];
notice that there is no such thing as dr.Length, you have to check for the column count:
dr.Table.Columns.Count
lastly, you should probably move the string array definition up so it's available outside the IF.
Depending on your needs, what you could do is use the Datarow's ItemArray property. It is an object array in stead of a string array, but before using it's values you can do a "ToString()" and you are all set.
string a = dr.ItemArray[i].ToString();

How convert stream excel file to datatable C#?

I use Epplus to reading xlsx files from stream.
It has a bug , it cant read some columns in my workbook.How can read xlsx files from stream to datatable without epplus ?
my older code:
public static DataSet ReadExcelFile(Stream stream)
{
try
{
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader =
ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
DataSet result = excelReader.AsDataSet();
return result;
}
catch (Exception x)
{
throw x;
}
}
I didnt report it, but i tried so much combinations.If there are empty columns in worksheet ,epplus reader cant read correctly column values.
"It has a bug , it cant read some columns in my workbook"
Can you describe the bug, have you reported it or is it already known, what version are you using?
Here's a simple approach to load an excel file into a DataTable with EPPlus.
public static DataTable getDataTableFromExcel(string path)
{
using (var pck = new OfficeOpenXml.ExcelPackage())
{
using (var stream = File.OpenRead(path))
{
pck.Load(stream);
}
var ws = pck.Workbook.Worksheets.First();
DataTable tbl = new DataTable();
bool hasHeader = true; // adjust it accordingly( i've mentioned that this is a simple approach)
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
{
tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
}
var startRow = hasHeader ? 2 : 1;
for (var rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
var row = tbl.NewRow();
foreach (var cell in wsRow)
{
row[cell.Start.Column - 1] = cell.Text;
}
tbl.Rows.Add(row);
}
return tbl;
}
}
This is way past, however it could still help someone.
Apparently some columns in my worksheet were merged, so for example, if columns A and B are merged it only recognizes column A as the one with the value, and so it returns column B as empty, when i call on that particular cell's value(B). To get past this, make sure you know which cells are merged and then grab only the first one and regard the rest of the merged cells as null

Presentation of data from Mondrian OLAP engine + Olap4j

I'm doing a little bit of planning of an application that uses Mondrian OLAP engine with Olap4j and should present/display data to user. I understand all the back-end stuff, but I'm not sure how should I display the data in the view layer.
For example olap4j has a formatter that prints the SELECT nicely into the console.
How is the data that I get from olap4j displayed in view layer ? I just went through the olap4j API, and there doesn't seem to be anything for getting the result in a form that can be somehow further processed and displayed. Is this process part of the Pentaho solution ? So that otherwise it is really not easy to present data just from Mondrian OLAP engine and olap4j ?
EDIT: I'm used to traditionally get some data from a database into my DTO and display it in view layer. But how do I create DTOs for such a complicated result set ?
You can create your own view layer it's just a little bit tricky.
OlapStatement.executeOlapQuery() returns a CellSet, you will have to work with that. Also read the specifications, it's a good source of information.
Here is an example, that creates List<List<MyCell>> (not the best representation but it's easy to undarstand how it works). This creates a table similar to http://www.olap4j.org/api/index.html?org/olap4j/Position.html (without the "Gender" and "Product" labels).
private final static int COLUMNS = 0; //see Cellset javadoc
private final static int ROWS= 1; //see Cellset javadoc
/**
* Outer list: rows, inner list: elements in a row
*/
private List<List<MyCell>> getListFromCellSet(CellSet cellSet) {
List<List<MyCell>> toReturn= new ArrayList<List<MyCell>>();
//Column header
//See http://www.olap4j.org/api/index.html?org/olap4j/Position.html on how Position works, it helps a lot
//Every position will be a column in the header
for (Position pos : cellSet.getAxes().get(COLUMNS).getPositions()) {
for (int i = 0; i < pos.getMembers().size(); i++) {
if (toReturn.size() <= i) {
toReturn.add(i, new ArrayList<MyCell>());
}
Member m = pos.getMembers().get(i);
MyCell myCell = new MyCell(m); //use m.getCaption() for display
toReturn.get(i).add(myCell );
}
}
//Put empty elements to the beginning of the list, so there will be place for the rows header
if (cellSet.getAxes().get(ROWS).getPositions().size() > 0) {
for (int count=0; count < cellSet.getAxes().get(1).getPositions().get(0).getMembers().size(); count++) {
for (int i = 0; i < toReturn.size(); i++) {
toReturn.get(i).add(0, new MyCell());
}
}
}
//Content + row header
for(int i = 0; i < cellSet.getAxes().get(ROWS).getPositionCount(); i++) {
List<MyCell> row = new ArrayList<MyCell>();
//Header
for (org.olap4j.metadata.Member m : cellSet.getAxes().get(ROWS).getPositions().get(i).getMembers()) {
row.add(new MyCell(m));
}
//Content
for (int j = 0; j < cellSet.getAxes().get(COLUMNS).getPositionCount(); j++) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(j); //coordinte
list.add(i); //coordinte
row.add(new MyCell(cellSet.getCell(list))); //use cell.getFormattedValue() for display
}
toReturn.add(row);
}
return toReturn;
}
Create the MyCell class with these constructors:
public class MyCell {
...
public MyCell(){...}
public MyCell(Member m){...}
public MyCell(Cell c){...}
}
Don't forget to display the filters, use Cellset.getFilterAxis() for that.
You can also check the Rectangular formatter on SourceForge, but it's a bit longer.

Create HTML table out of object array in Javascript

I am calling a web Method from javascript. The web method returns an array of customers from the northwind database. The example I am working from is here: Calling Web Services with ASP.NET AJAX
I dont know how to write this javascript method: CreateCustomersTable
This would create the html table to display the data being returned. Any help would be appreciated.
My javascript
function GetCustomerByCountry() {
var country = $get("txtCountry").value;
AjaxWebService.GetCustomersByCountry(country, OnWSRequestComplete, OnWSRequestFailed);
}
function OnWSRequestComplete(results) {
if (results != null) {
CreateCustomersTable(results);
//GetMap(results);
}
}
function CreateCustomersTable(result) {
alert(result);
if (document.all) //Filter for IE DOM since other browsers are limited
{
// How do I do this?
}
}
else {
$get("divOutput").innerHTML = "RSS only available in IE5+"; }
}
My web Method
[WebMethod]
public Customer[] GetCustomersByCountry(string country)
{
NorthwindDALTableAdapters.CustomersTableAdapter adap =
new NorthwindDALTableAdapters.CustomersTableAdapter();
NorthwindDAL.CustomersDataTable dt = adap.GetCustomersByCountry(country);
if (dt.Rows.Count <= 0)
{
return null;
}
Customer[] customers = new Customer[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
NorthwindDAL.CustomersRow row = (NorthwindDAL.CustomersRow)dt.Rows[i];
customers[i] = new Customer();
customers[i].CustomerId = row.CustomerID;
customers[i].Name = row.ContactName;
}
return customers;
}
Try to look what is the result variable value in debug mode. If the structure seems the structure that i'm imagining, something like this could work:
function CreateCustomersTable(result) {
var str = '<table>';
str += '<tr><th>Id</th><th>Name</th></tr>';
for ( var i=0; i< result.length; i++){
str += '<tr><td>' + result[i].CustomerId + '</td><td>' + result[i].Name + '</td></tr>';
}
str += '</table>';
return str;
}
And then You can do somethig like this:
var existingDiv = document.getElementById('Id of an existing Div');
existingDiv.innerHTML = CreateCustomersTable(result);
I wish this help you.
Something like this, assuming you have JSON returned in the "result" value. The "container" is a div with id of "container". I'm cloning nodes to save memory, but also if you wanted to assign some base classes to the "base" elements.
var table = document.createElement('table');
var baseRow = document.createElement('tr');
var baseCell = document.createElement('td');
var container = document.getElementById('container');
for(var i = 0; i < results.length; i++){
//Create a new row
var myRow = baseRow.cloneNode(false);
//Create a new cell, you could loop this for multiple cells
var myCell = baseCell.cloneNode(false);
myCell.innerHTML = result.value;
//Append new cell
myRow.appendChild(myCell);
//Append new row
table.appendChild(myRow);
}
container.appendChild(table);
You should pass the array as JSON or XML instead of just the toString() value of it (unless that offcourse is returns either JSON oR XML). Note that JSOn is better for javascript since it is a javascript native format.
Also the person who told you that browser other then IE can not do DOM manipulation should propably have done horrible things to him/her.
If your format is JSON you can just for-loop them and create the elements and print them. (once you figured out what format your service returns we can help you better.)

Resources