How do I get the index of each record displayed on crystal reports? - vb.net

I'm developing winform application using vb.net. I use crystal report in my application. Now i need to store the index of each record that is displaying in current report into database. Then i create another report which displays the index of all the records. My plan is to read the page number of each record and save them on database. I have a group field in the report. I don't know how to scan one by one record and get appropriate page number. I tried the following code to get the field value but did not work.
msgbox(rpt.DataDefinition.FormulaFields("name").Text)
It displayed the formula of that field.
I don't know in which order the records fetched using following.
msgbox(rpt.rows(0).item(0))
Please Help me out....

The way your question reads, it sounds like you're trying to generate an "index" for the order records appear in a report and store that index in a database for use in another report. I am not clear at all what you're trying to do with the page number.
In any case, I need to make something clear: the definition of the report (which you're accessing in your code sample) only represents where data will go when the report is formatted - it is not the actual data itself.
At the most basic level, Crystal Reports takes data out of a database and formats it nicely for you on pages. Rather than trying to take data out of a database, put it into a report, then try to read that report to put it back in a database to make another report, why not just do all your data manipulation at the database level itself before going to a report in the first place?
If you really must have that first report, the easiest option you'll have for getting at the formatted data is to export it to excel and access it programmatically through an Excel API - Crystal Reports doesn't have an API for getting at your formatted data (including things like generated page numbers, whatever you're trying to do with them).

You are trying to break few basic rules. For example a report should never change the data. The right way is to handle data processing in a stored procedure or command and to call the report in a separate process. In such way you will be able to control the data change and data visualization separately.
P.S. You probably mean "Identifier" not "Index"

Related

SSRS Query paging

I am creating a report from a very large table. I created a very simple report that simply has one table in it. The table does seem to allow paging but every page load takes so long that it seems as if it was pulling the entire table and then paging locally.
I thought I might have to add the paging to the query myself but this doesn't work because the page variables can only be used in the header or footer
="SELECT * FROM MyTable OFFSET " & =Globals!PageNumber & " * 20 LIMIT 20"
I haven't used SSRS before so I'm not sure if there is something really obvious that I am missing.
Any help with how to do this properly would be appreciated.
If you are using any pagination related expressions, SSRS must retrieve all of the data before it display the first page.
For example, if you are trying to do "Page 1 of X" in the footer, SSRS must figure out how many pages exists (by retrieving and pre-rendering all pages) before it can display page 1.
Also, if you don't need all of the columns, don't use Select *. That will force SSRS to load every column into its cache even if they are not all used. You can cut down on the data retrieval execution time.
Finally, if you have access to the SSRS reporting database, you can take a look at the execution statistics for your report to see where the performance problem is happening. https://msdn.microsoft.com/en-us/library/ms159110.aspx
The Global PageNumber in SSRS is generated after the report is rendered. That's why you can't use it in the query.
It's unclear exactly what you mean by paging. SSRS automatically creates pages for the report once the table goes over the page size. If you wanted to just query a small chunk of the table and then click a button to get the next chunk, there's nothing like that readily available.
However, you could use the ROW_NUMBER function in your query and filter that to a particular range in the report. But you have to specify the value of the parameters/filters before you run the report because it uses those to process the dataset and then goes on to render the report.

VB.NET Active Reports

I have a VB active report which has many different attributes. The report contains attributes area, balance, id and status and is currently grouped my area. What I need to do now is remove all lines from the report where the balance is equal to 0 and the status equal to deactive. The information for the active report is from my database. Thus I think the best way would be to only select records where the balance is not 0 and status not equal to deactive. Is there a way I can query my database and have the active report be based off the query results? Is there an easy way to do this? Thanks for any help.
Yes, the best way to do this is to change the query so that only the minimum records you need are actually coming into the report. The Modify Data Sources at Run Time topic from the documentation shows how to modify that SQL statement in the code dynamically at runtime.
If you can hard-code the SQL query for the report you should probably just modify the SQL at design time inside the designer. This Bind Reports to a Data Source topic shows you how to do that.
You can also programatically control the visibility of fields/textboxes based on the data using the Format event of the section containing those controls (most likely Detail_Format), but it sounds to me like modifying the SQL query is your best bet.

Create a report that could be one page or two, depending on what field was modified

In an alternate application, the user has the ability to update their address and phone number. When these are changed, three fields will update: Old Value, New Value, and Field Changed. If the Field Changed was the address, I need to create two report pages - one with the old address and one with the new. However, if the Field Changed was the phone number, I only need to create one report page for the current address.
My initial plan was to do a Union that would have one record with the Old Value and another with the New Value. This should work when only the Address has changed. However, it won't whenever the Phone Number has changed. I assume I need to do some sort of case statement, but I'm not really sure if this is the right approach. Sorry if the data is a little confusing (I didn't design the data structure. This was provided by our professor's assistant). If you need more information, I'll try to provide it.
I'm not looking for exact SQL, but I am wondering if I'm approaching this the correct way.
What do you mean by a 1 or 2 page report? Are you outputting to a CSV, PDF, XLSX or something eles?
If you need to do this through "pure" sql I would recommend a stored procedure that is given a value stating whether it's the address or phone number that is being updated. It can then do the update and you can simply do an if statement which determines which report to run and return.
I'd recommend handling it programatically if possible. Have your code run the sql update and then call the appropriate function within your code to get the report you need. You can then easily re-use the code for that report in other ways.

Substitute one table for another at runtime

I have 9 tables with state information in them. They all have the same field names. I have a Crystal Report that is based on one of them. I want the user to be able to select a state and change the Crystal Report to use that table instead of the one it was based on.I mean when user select text in combo box and then click on "OK" then report show (using only one rpt for all the tables of same fields).
How do I do that in VB.Net?
Could you base the Report on a Stored Procedure and pass in a parameter so the Stored Procedure knows which table you want the data from?
I remember trying to get a Crystal Report (v2005) to switch database source from the one it was designed against and that wasn't easy - every table had the connection details in it if I recall correctly! (maybe changing tables is easier though)
It's not easier but if the tables have the same structure you should be able to do it. But you should create a new Document for it and call SetDataSource for your new table. After this you can set the document as ReportSource to your viewer.

Dynamic SSRS report

I had a problem in creating the Dynamic report in SSRS. My problem is:
In a table I have stored SQL scripts with the column SQLScripts. If you execute these SQL scripts you get different number of columns for each script.
My problem is, I have one report with buttons of these scripts, for example test1, test2...like that. If you press test1 button this should take the test one SQL script and should display the report with appropiate columns in that sqlscripts.
I can't create individual reports for each test report, they are plenty. Are there any options for me to solve this problem...
The only way I've been able to get this to work sofar is:
Each report has 2 datasets.
ReportData
DataHeaders
The "DataHeaders" need to have the proper name of the datafields in "ReportData". Be careful since SSRS replaces blanks and special characters with "_"
Now, create a table (or matrix) and drag the DataHeaders as the Columns of your report. (This should be a grouped column). If you run it at this point, you'll see all your columns without any data. Now comes the magic:
Create another report that takes a "DataField" parameter. Create another table or matrix within this report and set it's dataset property to be "ReportData". In the DATA cell for the table, set it to the expression =Fields(Parameters!DataField.Value).Value
Now go back to your first report. Right click and insert a subreport. Right click on the subreport and select "Subreport Properties". Under general, select the second report you created to be used as the subreport. Under parameters, select the DataField parameter and set its value to something like =Fields!DataField.Value
In my case I did some formatting in this expression to fix the above mentioned issue with spaces and special characters, since my stored procedure was initially used in ASP.NET and this was just a proof of concept.
Also in my experience the performance isn't great. In fact it was kinda slow, though I haven't had a chance to switch it to use a shared dataset, which I suspect would help a bit. Please let me know if you find a better solution.
I have not found a way to do this completely dynamically. Here is a similar question with some possible solutions:
How do i represent an unknown number of columns in SSRS?
You basically need to create a 'master dataset' from the other Datasets that are based on your multitude of SQL scripts first.The master dataset should contain the data to be presented in it's most simplistic form, i.e. in a simple list format.
Finally, go to the toolbar in SSRS and drag a 'Matrix' into the report. A Matrix table acts similar to a pivot table in Excel or a CrossTab query in Access that will display whatever's in the Dataset.