'How to pass custom sql command to fastreport.net through application using c#?

I start using fastreport.net to generate report in c# since Crystal reports is not compatible with .net4. Its so simple but it is so complicated also. I try to pass an sql command to my report through my applicate but it doesnt work.Can anyone help me with that ? Here is the Code :

        Report rpt = new Report();
        rpt.Load("H:\\MyReport.frx");
        rpt.SetParameterValue("Parameter", "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Confictionary");
        FastReport.Data.TableDataSource data = rpt.GetDataSource("Contact") as FastReport.Data.TableDataSource;
        data.SelectCommand = "SELECT * from contact";
        rpt.Prepare();
        rpt.ShowPrepared();

It throws an exception in line 5 : "Object reference not set to an instance of an object." Please sb help me.
tnx a lot



Solution 1:[1]

I think SQL command should be in report template set, not dynamically, unless this would be used only for the first time for creating report file.

i use this:

           FReport.SetParameterValue("SQL_rysys", ConnectionString);

                  if (FReport.Dictionary.Connections.Count > 0)
                  {
                      FReport.Dictionary.Connections[0].ConnectionString = ConnectionString;
                      FReport.Dictionary.Connections[0].ConnectionStringExpression = "[SQL_rysys]";
                      FReport.Dictionary.Connections[0].CommandTimeout = 60;
                  }

This code assigns value for Parameter "SQL_rysys" (SQL_connection in english), i pass SQL connection string. Also if there is any connections in datasource dictionary-i assign connection string to the first one.

So my reports always gets the correct connection string, BUT the SQL query is already included IN REPORT. Cause there is no other way of creating report in designer without using query to get data, and that query is saved in XML report file (frx).

I was asking in fastreport support how to assign query, but their support is AWFUL and noobs works there.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tommix