Connecting Data

Data to a report can be connected in various ways. The easiest way is to store connection settings in the report template. You can also connect the data from the code, this can be done when the report is loaded in the GetReport action.

HomeController.cs

...
public ActionResult GetReport()
{
    DataSet ds = new DataSet();
    ds.ReadXml(Server.MapPath("~/Content/Data/Demo.xml"));
    
    StiReport report = new StiReport();
    report.Load(Server.MapPath("~/Content/TwoSimpleLists.mrt"));
    report.Dictionary.Databases.Clear();
    report.RegData("Demo", ds);
    
    return StiMvcViewerFx.GetReportResult(report);
}
...

SQL data sources

The connection parameters to the SQL data source, as well as to any other ones, can be stored in the report template. If you want to set the connection parameters from the code before rendering the report (for example, for security reasons or depending on the authorized user), you can use the example below.

HomeController.cs

...
public ActionResult GetReport()
{
    OracleConnection connection = new OracleConnection("Data Source=Oracle8i;Integrated Security=yes");
    connection.Open();
    OracleDataAdapter adapter = new OracleDataAdapter();
    adapter.SelectCommand = new OracleCommand("SELECT * FROM Products", connection);
     
    DataSet dataSet = new DataSet("productsDataSet");
    adapter.Fill(dataSet, "Products");
     
    StiReport report = new StiReport();
    report.Load(Server.MapPath("~/Content/SqlSampleReport.mrt"));
    report.RegData("Products", dataSet);
    
    return StiMvcViewerFx.GetReportResult(report);
}
...

Information

For SQL data sources of other types, the connection is created similarly, and an adapter corresponding to the type of the data source is connected. For example, for the MS SQL data source, you should connect SqlDataAdapter, for OLE DB - OleDbDataAdapter is required. Also, you should specify a connection string that matches the connection type.

The table below shows the connection string templates for different types of data sources.

Data Source Connection String Template
MS SQL Integrated Security=False; Data Source=myServerAddress;Initial Catalog=myDataBase; User ID=myUsername; Password=myPassword;
MySQL Server=myServerAddress; Database=myDataBase;UserId=myUsername; Pwd=myPassword;
ODBC Driver={SQL Server}; Server=myServerAddress;Database=myDataBase; Uid=myUsername; Pwd=myPassword;
OLE DB Provider=SQLOLEDB.1; Integrated Security=SSPI;Persist Security Info=False; Initial Catalog=myDataBase;Data Source=myServerAddress
Oracle Data Source=TORCL;User Id=myUsername;Password=myPassword;
MS Access Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Password=pass;Data Source=C:\myAccessFile.accdb;
PostgreSQL Server=myServerAddress; Port=5432; Database=myDataBase;User Id=myUsername; Password=myPassword;
Firebird User=SYSDBA; Password=masterkey; Database=SampleDatabase.fdb;DataSource=myServerAddress; Port=3050; Dialect=3; Charset=NONE;Role=; Connection lifetime=15; Pooling=true; MinPoolSize=0;MaxPoolSize=50; Packet Size=8192; ServerType=0;
SQL CE Data Source=c:\MyData.sdf; Persist Security Info=False;
SQLite Data Source=c:\mydb.db; Version=3;
DB2 Server=myAddress:myPortNumber;Database=myDataBase;UID=myUsername;PWD=myPassword;Max Pool Size=100;Min Pool Size=10;
Infomix Database=myDataBase;Host=192.168.10.10;Server=db_engine_tcp;Service=1492;Protocol=onsoctcp;UID=myUsername;Password=myPassword;
Sybase Data Source=myASEserver;Port=5000;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
Teradata Data Source=myServerAddress;User ID=myUsername;Password=myPassword;
VistaDB Data Source=D:\folder\myVistaDatabaseFile.vdb4;Open Mode=ExclusiveReadWrite;
Universal(dotConnect) Provider=Oracle;direct=true;data source=192.168.0.1;port=1521;sid=sid;user=user;password=pass
MongoDB mongodb://<user>:<password>@localhost/test
OData http://services.odata.org/v3/odata/OData.svc/

Information

The table shows the most commonly used templates for the connection string. You can view various connection string options at the specia; website.

Data from XML, JSON, Excel files

Connecting to XML and JSON data sources can be stored in the report template. If you want to specify data files from the code, you can use the example below.

HomeController.cs

...
public ActionResult GetReport()
{
    DataSet data = new DataSet();
    data.ReadXml(Server.MapPath("~/Content/Data/Demo.xml"));
    
    StiReport report = new StiReport();
    report.Load(Server.MapPath("~/Content/SimpleList.mrt"));
    report.RegData(data);
    
    return StiMvcViewerFx.GetReportResult(report);
}
...

HomeController.cs

...
public ActionResult GetReport()
{
    DataSet data = StiJsonToDataSetConverterV2.GetDataSetFromFile(Server.MapPath("~/Content/Data/Demo.json"));
    
    StiReport report = new StiReport();
    report.Load(Server.MapPath("~/Content/SimpleList.mrt"));
    report.RegData(data);
    
    return StiMvcViewerFx.GetReportResult(report);
}
...

Information

The viewer has the possibility of obtaining data from an Excel file. To do this, you can use the following method.

DataSetdataSet =StiExcelConnector.Get().GetDataSet(newStiExcelOptions(array,this.FirstRowIsHeader));