Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...