Solved

Filling Unbound Fields on a Crystal Report in ASP.NET

Posted on 2006-07-05
15
1,516 Views
Last Modified: 2010-05-18
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?!
0
Comment
Question by:proten
  • 7
  • 5
15 Comments
 
LVL 16

Expert Comment

by:Edwin_C
ID: 17047654
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
0
 
LVL 5

Author Comment

by:proten
ID: 17047775
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?
0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 17048237
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


0
 
LVL 5

Author Comment

by:proten
ID: 17050532
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.
0
 
LVL 5

Author Comment

by:proten
ID: 17050550
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?
0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 17051342
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.

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Author Comment

by:proten
ID: 17052526
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)?
0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 17055475
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.


0
 
LVL 5

Author Comment

by:proten
ID: 17074194
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.
0
 
LVL 5

Author Comment

by:proten
ID: 17075763
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?
0
 
LVL 16

Expert Comment

by:Edwin_C
ID: 17151019
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
0
 
LVL 5

Author Comment

by:proten
ID: 17154011
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))
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 17356285
PAQ / No Refund
ee ai construct, community support moderator
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now