Filling Unbound Fields on a Crystal Report in ASP.NET

I  have  a Crystal Report created with unbound fields.  I want to be able to use ASP.NET to update the crystal report in my code with data from a sql database.

I have never worked with crystal reports before.

I need to know how to:
1 - Access the report in my code.
2 - Update the unbound fields
3 - Save the report as a pdf

I am so frustrated!  Can somebody please please help me?!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

My suugestion is to get your data from DB using ASP.NET code and then pass the data into the crysytal report as reprot parameters.

For exporting to PDF, see

protenAuthor Commented:
I can get the data no prob in my ASP.NET code.

I have the fields that I need to use in my report, now what?

How do I access the report in my code and how do I set the parameters to fill the data?
Add a parameter field in the crystal report and then you can drag the parameter field onto your crystal report.

To assign value to the parameter, you can refer

Hope that helps

Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

protenAuthor Commented:
I think I need to fill the fields dynamically in the background.

I'll explain my scenario:

I am creating a packing slip/s.  When a user enters say a customer number and some other information on the front end, I will validate the info and then populate one or more of the template I have in my report.  I will be getting the data from 3 different tables and one will be repetitive.

Address Info is one record on the address table
Customer Id info is one record on the cust table
Order Info is multiple records on the order table (I display these as a list)

I will create one packing slip per customer but there may be a few customers.  

The end result is a report with 1 or more pages.  Each page containing the information for one customers order that may contain many items.

The way I figured I'd do it is create an screen that has input fields for the key information.  Then in the code-behind I can get the data from the db and manipulate it and in my code set each field to an unbound field in the crystal report that will be generated in my code.  I already have the crystal report template created so I want to "read" it and fill in the "blanks" in my program.
protenAuthor Commented:
I guess my question is that I already have the template created and set out in a .rpt file, how do I read it in my program and fill the fields?
It seems to me you are finding a way to assign a value to a field inside the crystal report from outside.  I doubt if it is possible!?

I hope I understand your scenario correctly.  You generate the packing slip probabaly based on an order which contains customer Id and other items ordered.  Then I will pass just the order Id to the crystal report and then draw the necessary data from DB by the code (actually a DB query) inside crystal report.

Alternatively, you can compose a dataset in ASP.NET which contains all the necessary data of your packing slip and then pass the whole dataset to the crystal report as datasource.

protenAuthor Commented:
I dont think the first option will work because the top portion of the form is regular table data (one record per form) but included on the form is a detail table of up to 10 items per form that is one record per line from one datatable.

The second option makes more sense.  I can create a dataset and name each of the detail lines a different field name.  

I still dont know how to open the file with the template in my code and set the dataset as the datasource for the crystal report.  Also, will I be able to create multiple pages of the same template (i.e. 5 packing slips for different customers)?
The first option is feasible when you use grouping in crystal report.

You have to select all related tables in the crystal report and then use the orderId parameter as the selection criteria.
Group the record by the orderId field and then put the customer name, address stuff in the group header section and put the order lines (item code, qty, etc) in the details section.  (Actually you can choose not using grouping and simply put the cusotmer data in the report header section but I prefer grouping because it allows you to print multiple slips at a time)

For using dataset in CR, please refer to these articales

For having multiple copies of a report, you can create a crystal report and then add the template as multiple subreports.

protenAuthor Commented:
Hope you had a nice weekend.

Okay, I have figured out how to
open the report using rpt.load()
access and set the fields using cr.DataDefinition.FormulaFields("FldName")
and save it to pdf using cr.ExportToDisk()

So now I can create a report with one page.  Now I need to create multiples of the same page in the same document.

Subreport sounds like the right idea.  How do I create and add those to a report?  I want to have one or many of the same page but with different data on each, saved to one pdf file.

Also, I really dont need to save the pdf file to disc, I just need to open it in a pdf viewer so the user can print.  Is there a way to just open the file without saving it?  

protenAuthor Commented:
Another thing,

I did not understood your explanation of grouping.  How does it work and how do I use it?  Can you give me an example?
Sorry for the late reply because I was on vacation last week.

Grouping is a basic function in all reporting tools (including Crystal report) and you can find many tutorials on that.  Here is one I found from internet.

In your case, you can link all your tables (address, customer, order) and pass to the report and then group by OrderID.  Then put data such as customer name, order number, order date, etc in the group header.  The item ordered and qty can be put in the details section.

Hope that helps
protenAuthor Commented:
I finally got it to work, I did the following:

I created 2 crystal reports.
1 - Detail report linking to the datatable containing the detail records for the packing slip.
2 - A main report linking to the datatable with the header information.  I included the detail report as a subreport.

- Grouped the main report by order number so that there would be one page per order.
- Did not use the group headers at all, in fact suppressed them from displaying.  
- Placed the header info in the page header.
- Checked the "New Page After" option for the group footer in the section expert window so that  a new page was generated for each order
- Created a subreport link linking the order number of the detail items to the main page order number so only the details for that order would be generated on that page.

Accessing the report in my code:
- Added the crystal reports references.
- Included the following imports in my code
  Imports CrystalDecisions.CrystalReports.Engine
  Imports CrystalDecisions.Shared
- Open doc in the code:
   Dim cr As New ReportDocument
   cr.Load(Server.MapPath(FileRptName), OpenReportMethod.OpenReportByTempCopy)
- Set data for the report:
   Dim crSub As ReportDocument
   crSub = cr.OpenSubreport(FileSubRptName)
- Open report without saving it:
    Dim crDFDO As New DiskFileDestinationOptions
    crDFDO.DiskFileName = Server.MapPath(FileTempSaveName)
    With cr.ExportOptions
           .DestinationOptions = crDFDO
           .ExportDestinationType = ExportDestinationType.DiskFile
           .ExportFormatType = ExportFormatType.PortableDocFormat
     End With
     Response.ContentType = "application/pdf"
PAQ / No Refund
ee ai construct, community support moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.