Page 1 of 1

Switch SQL Connection to DataTable on preview event

Posted: Wed Oct 12, 2016 9:49 pm
by sbd_jeff
Hi Stimulsoft!
I have what I think is a unique situation.
We have a client application that can talk both directly to a database using ADO, but it can also talk to a webservice application which hosts the business logic including the ability to render Stimulsoft reports server side.
In the case of a webservice, the client application is not able to connect direct to SQL via ADO.

I've implemented the report editor in our client application, and it works great in both cases.
The one problem I have is when someone clicks the Preview tab in the report editor and their client is connected via a webservice.
The editor tries to connect via ADO and fails.

I tried to intercept the behavior via the StiDesigner.PreviewingReport event and extract the SQL from the report.
I then ran the SQL server side which returned a DataTable.
I thought I might be able to "hot-swap" the connection from SQL to use a DataTable instead simply for the purposes of preview, but I was not able to get that approach to work.

In a nutshell, the problem I am trying to solve is when using the designer is there a way to short-circuit the preview action so the engine returns a precompiled dataset?
If not, is there a better approach I should take?

Just to see if it would work I tried this in the PreviewingReport event:

Code: Select all

var control = sender as Stimulsoft.Report.Design.StiDesignerControl;
// get the query
var query = control.Report.DataSources.Items[0] as Stimulsoft.Report.Dictionary.StiSqlSource;
// execute query on server and get DT
var recs = WebServiceApp.SQLCommandDataTable.Execute(query.SqlCommand);
var rpt = control.Report as Stimulsoft.Report.StiReport;
// set up data source             
rpt.Dictionary.Databases.Clear();
rpt.Dictionary.DataSources.Clear();
rpt.RegData(recs);
rpt.Dictionary.Synchronize();
The designer still tried to connect using SQL.

Thanks!

Re: Switch SQL Connection to DataTable on preview event

Posted: Thu Oct 13, 2016 7:15 pm
by Alex K.
Hello,

Please try to use the following code:

Code: Select all

var report = new StiReport();
report.Load();
StiOptions.Engine.GlobalEvents.PreviewingReportInDesigner += GlobalEvents_PreviewingReportInDesigner;
report.Design();
...
private void GlobalEvents_PreviewingReportInDesigner(object sender, EventArgs e)
{
    var rep = ((StiDesignerControl)sender).Report as StiReport;

    var dataSet = new DataSet();
    // fill dataset

    rep.Dictionary.Databases.Clear();
    rep.Dictionary.DataSources.Clear();
    rep.RegData(dataSet);
    rep.Dictionary.Synchronize();

    rep.Compile();
}
Thank you.