Link to home
Start Free TrialLog in
Avatar of proten
proten

asked on

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?!
Avatar of Edwin_C
Edwin_C
Flag of Hong Kong image

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 http://www.c-sharpcorner.com/Code/2004/May/ExportCrystalReportInASPNET.asp

Edwin
Avatar of proten
proten

ASKER

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
http://support.businessobjects.com/library/kbase/articles/c2010247.asp

Hope that helps
Edwin


Avatar of proten

ASKER

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 asp.net 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.

i.e.
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 asp.net 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.
Avatar of proten

ASKER

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.

Avatar of proten

ASKER

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 asp.net 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
http://www.codeproject.com/aspnet/crystal_report.asp
http://www.codeproject.com/Purgatory/Crystal_Report.asp

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


Avatar of proten

ASKER

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?  

Thanks.
Avatar of proten

ASKER

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.  http://www.crystaldevelopersjournal.com/artman/publish/article_118.shtml

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
Edwin
Avatar of proten

ASKER

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 vb.net 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
   cr.Database.Tables(0).SetDataSource(ds.Tables("HeaderTable"))
   crSub = cr.OpenSubreport(FileSubRptName)
   crSub.Database.Tables(0).SetDataSource(ds.Tables("DetailTable"))
- 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
     cr.Export()
     Response.ClearContent()
     Response.ClearHeaders()
     Response.ContentType = "application/pdf"
     Response.WriteFile(Server.MapPath(FileTempSaveName))
     Response.Flush()
     Response.Close()
     System.IO.File.Delete(Server.MapPath(FileTempSaveName))
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial