on 02-14-2011 4:57 AM
Hi all,
Is it possible to programmatically create a command object into an existing report? (Visual basic .net)
I would like to do so as a filtering mechanism.
The report is based on a database query.
I would like to create a command object and inner-join it to the query, and use the SQL of the command object to do the filtering.
I want to use a command as the filtering because the SQL comes from an application that has it's own filtering mechanism that builds up SQL. It may include database fields that are not necessarily part of the original report table (query)
Is it possible? Any ideas?
Many thanks,
Rod
Rod,
I'm not 100% sure what you're trying to do... But I have an idea.
There is a very cool thing about Commands and their use of parameters. What CR does, it uses a simple parameter/value swap. This makes it very easy to write dynamic SQL without actually having to write dynamic SQL.
For example:
Say you have a report and you want to give the option of having FieldABC = 123 OR FieldXYZ = 789
You can set up your parameter like this:
Value1: FieldABC = 123
Description1: Filter on FieldABC
Value2: FieldXYZ = 789
Description2: Filter on FieldXYZ
You can then write you command like this...
SELECT * FROM TableName WHERE {?ParameterName}
So now when the the report runs and the user selects "Filter on FieldXYZ" as their parameter selection, the SQL that gets sent back to the database will look like this...
SELECT * FROM TableName WHERE FieldXYZ = 789
HTH,
Jason
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Hoa and Jason,
Thanks for your replies. I'll look into both of these.
The reason why I'm looking into dynamically (progammatically) creating a command object is so that I can use it as a report filter.
The reports I'm making are for an application. The application has it's own search screen that is being used as a filter. I'm looking for a way to pass the SQL from this search screen through to the report. In the past, this search screen would transform the search parameters into Crystal Reports "RecordSelectionFormula" code and set these just before displaying the report. The problem with this approach is that the data source for the report needs to contain all of the fields that the search screen can generate filters for. This means having to use a datasource for the report that is overkill for the actual fields that are displayed on the report. In other words, you need to have the underlying query return (or at least expose) many database fields that are not actually used in the report - they are only there so the "RecordSelectionFormula" can work.
Why the command?
If I can dynamically create a command object and set its SQL, I can inner join it to the main report datasource and it will act as a filter. The main datasource contains all of the display fields, and the command is simply a filter mechanism.
At this stage I'm looking into hard-coding into every report an additional datasource that is based of an XML schema, with just one field - the primary key for the datasource. I can inner-join this at design time, and then programmatically asisgn it a dataset with the filtering data I require.
If anyone has a better way, I love to discuss it. I could be making a lot of extra work for myself here?
Thanks,
Rod
Edited by: Rod Weir on Feb 14, 2011 10:40 PM
Rod,
Kind of seems like you are taking a round about approach to this... And your approach may cause you some performance problems if your main query has any complexity at all.
The problem is that as soon as you add a Command to the mix, CR stops pushing any of the processing back to the server. So while the Command would limit the records shown on the report it will not limit the number of records being pulled from the database. Every record from every table will be brought in and all of the processing will happen client side.
If your application is capable of generating the necessary SQL to create a workable where clause it should also be able to generate an entire SQL statement. Just hard code the bulk of the SQL (the part you currently have) and make the rest dynamic as per your current plan.
From there you can simply pass the entire SQL statement as a single string type parameter.
Basically the Command itself would simply be 1 single parameter. And the value passed to the parameter would be an entire SQL statement.
You'll just want to make sure the field list is identical no matter what the user selects in the application... Which doesn't sound like an issue based on your last post.
HTH,
Jason
Hi Jason,
Thanks for this info. I didn't realize the client/server processing ramifications of commands. That's a definite show-stopper for my plan.
The datasource that the reports are based off are basically unfiltered database views - they return everything! From what you're saying, adding a command will return the entire query client side, and then do the inner join - is this right?
By basing a report off of a dynamic command as you suggest (the parameter being an entire SQL statement supplied by the application), this means that at design time you have no record set to build and design the report from.
How do you build and design a report if the data required for it (field definitions etc) is only supplied at runtime?
Thanks,
Rod
Hey Rod,
From what you're saying, adding a command will return the entire query client side, and then do the inner join - is this right?
Yes that is correct.
How do you build and design a report if the data required for it (field definitions etc) is only supplied at runtime?
When you click OK to create the command, you will be prompted to enter a parameter value before the Command is accepted. You would simply paste in a sample SQL query at that time. From there CR will test the SQL and pull in the results. That will give you the necessary data to build the report.
Jason
You don't necessarily want to
hard-code the SQL into the command at design time
as in add the actual query to the Command. Just add the 1 string parameter. You would actually add the SQL by manually pasting the query into the prompt.
For example:
1) Start a new blank report based off of the SQL Server AdventureWorks database.
2) Add Command and create a new parameter called SQL
3) Double click it over to the Command window so that the command itself looks like this
{?SQL}
Nothing else...
4) Click OK and it will prompt you for a value for {?SQL}... type or paste in *SELECT * FROM Sales.SalesOrderHeader*.
The SQL will be accepted and you will see all of the rows from the SalesOrderHeader table available for use on the report.
Jason
Edited by: Jason Long on Feb 14, 2011 4:33 PM
Getting this....
-
Crystal Reports
-
Failed to retrieve data from the database.
Details: ADO Error Code: 0x80040e10
Source: Microsoft OLE DB Provider for SQL Server
Description: No value given for one or more required parameters.
-
OK
-
Using CR 2008 Developer.
Seems like this method might have its quirks...
Maybe you can insert a sub-report, and use sql command in that report which won't join with the main report.
Thx, Hao
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
76 | |
10 | |
8 | |
8 | |
6 | |
6 | |
6 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.