How can I programmatically produce multiple copies of Crystal Reports (Details Section)?

18,130

I finally did it.

For those who would like to know how, or for a better StackOverFlow.com experience, I am glad to share the solution.

After reading this great article, I was inspired by the solution and was able to accomplish the desired result by

1-Replacing the FormulaFields with an ADO dataset containing the same number of columns I did that by creating a new DataSet in VS.2008 and named it adoDataSet. Then added all required column names to it (No linking to the actual data at this point as the data will be pulled dynamically later. This is just like a template for the data only). By default, all columns will be String typed, but that okay for my case.

3- In Crystal Reports, I used DataBase Expert to pull the mentioned dataset into the report and replaced the locations of the formula fields with the columns from the adoDataSet

4- Then in my code to populate the formulafields with data, I just called the same function that takes an SQL query and returns a normal OracleClient data set (so it might return multiple rows as desired)

5- This dataset however can not be used directly with Crystal Reports, so it must be first converted to the same type of the adoDataSet created earlier. so a simple TryCast did that for me.

Dim sql As String = ""
sql = " SELECT * from table1 where input_id = '" & INPUT_NUMBER & "'"

Dim ds As DataSet = getTable(sql)
Dim orpt As New CrystalReport3
CrystalReportViewer1.ReportSource = orpt
dim ds1 as New adoDataSet
ds1 = TryCast(ds,adoDataSet)   ' ds is based on OracleClient data set
                        ' while adoDataSet is the one CrystalReports likes to use
orpt.SetDataSource(ds1)

from there I was able to generate as much copies as I wanted because I own the data, and can cause the data to appear as many times as I like.

Share:
18,130
Ahmad
Author by

Ahmad

I am a Full Stack Software Engineer who has a passion for building solutions to solve existing problems/needs. I acquired a Udacity Nanodegrees as Front End Developer and iOS Developer. During the last 10 years of my career, I developed several web apps using ASP.Net that automates the task of technical support engineers quickly and securely. On my free time, I also enjoy building educational web app interactive games for kids to help them learn math and language skills. I'd love to combine my passion for writing software and providing solutions to continue building applications that are beneficial, productive, and helpful. I am experienced in the following fields: HTML5 / CSS3 / JavaScript ES6 ReactJS, Redux, Firebase Integration Bootstrap.js / D3.js / P5.js / jQuery C# / ASP.Net MVC iOS Development Microsoft SQL Server / Oracle PL/SQL / MySql / SQLite / Core Data PHP

Updated on June 04, 2022

Comments

  • Ahmad
    Ahmad over 1 year

    I have a web page that asks the user for an input number and based on the input, an SQL query is executed and the results is then fetched to fill a Crystal Reports page. Here is the code that searches the database and fills the report: (For simplicity, I removed a lot of query string that does not add value to the question, same goes for Crystal Reports fields)

    Dim sql As String = ""
    sql = " SELECT * from table1 where input_id = '" & INPUT_NUMBER & "'"
    
    Dim ds As DataSet = getTable(sql)
    Dim orpt As New CrystalReport3
    CrystalReportViewer1.ReportSource = orpt
    orpt.DataDefinition.FormulaFields("fldMaterialID").Text = """" & ds.Tables(0).Rows(0).Item("MATERIAL_ID").ToString & """"
    orpt.DataDefinition.FormulaFields("fldMaterialID2").Text = """" & ds.Tables(0).Rows(0).Item("MATERIAL_ID").ToString & """"
    orpt.DataDefinition.FormulaFields("fldBar").Text = """" & ds.Tables(0).Rows(0).Item("MATERIAL_ID_BAR_CODE").ToString & """"
    orpt.Refresh()
    

    I am trying to allow the user to view multiple copies of the report. I already tried:

     orpt.PrintToPrinter(2, False, 0, 1)
    

    But that did not work. I searched, but all the solutions I got were related to Crystal Reports with Saved Queries which is not an option for me.