Page 1 of 1

How to bind datasource to report

Posted: Wed May 28, 2014 8:19 pm
by jamesmuir
Hello, I am trying to bind a datasource to the report at runtime and cannot seem to get it to work. I have set the connection in the my report template and have verified that I can connect and view data in the designer. However, when I try to add the datasource via code I get no data. Inpsecting the datasource columns gives "Enumeration yielded no results". The query that I am using is the exact same query that is defined in the datasource in the designer. This is the code that I am using

Code: Select all

var report = new StiReport();
            report.Load(reportPath);

            StiConfig.Services.Add(new StiSqlCeAdapterService());
            StiConfig.Services.Add(new StiSqlCeDatabase());
         
            StiSqlCeSource DSDynamic = new StiSqlCeSource("SQLCE", "Users", "Users", "SELECT * FROM [Users]", true, false);
            report.Dictionary.DataSources["Users"] = DSDynamic;

            foreach (StiDataColumn col in DSDynamic)
            {
                //This does not get hit
                DSDynamic.Columns.Add(col.Name, col.Type);
            }

            report.Dictionary.Synchronize();
            report.Compile();
            report.Render();
            
            StiPdfExportService pdfExport = new StiPdfExportService();
            pdfExport.ExportPdf(report, outputPath);
Can someone point out what I am doing incorrectly here? Thank you.

Project code can be found here https://github.com/jamesamuir/reportdatatest.git

Re: How to bind datasource to report

Posted: Thu May 29, 2014 7:41 am
by Alex K.
Hello,

Please the following code:

Code: Select all

StiReport report = new StiReport();

string newConnectionString = @"Data Source=ALEKSEYPC\SE2012;Initial Catalog=Northwind;Integrated Security=True";
report.Dictionary.Databases.Clear();

report.Dictionary.Databases.Add(new Stimulsoft.Report.Dictionary.StiSqlDatabase("Connection", newConnectionString));

StiSqlSource Categories = new StiSqlSource("Connection", "Categories", "Categories", "SELECT * FROM Categories", true, false);
report.Dictionary.DataSources.Add(Categories);

using (SqlConnection connection = new SqlConnection(newConnectionString))
{
    SqlDataAdapter dataCategories = new SqlDataAdapter("select * from Categories", connection);
    DataTable dataTableCategories = new DataTable();
    dataCategories.Fill(dataTableCategories);

    foreach (DataColumn col in dataTableCategories.Columns)
    {
        Categories.Columns.Add(col.ColumnName, col.DataType);
    }
}
Thank you.