AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=677&pId=-1
Recommended Crystal Reports Resources for .NET Developers
page
by Eric Landes
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 23539/ 64

Interactive Enterprise Reporting Object Model

Introduction

Welcome to my article describing the resources I use when developing Crystal Reports for ASP.NET environments.  These resources include some of the code snippets, websites, direct links, and other bits and pieces that I turn to when creating a Crystal Reports web solution.

This article should be helpful for folks just starting to do Crystal Reports for .NET development as well as for experienced .NET developers.  I am assuming you use Visual Studio .NET for your development, utilizing the version of Crystal Reports included with Visual Studio .NET.

Business Objects’ Interactive Enterprise Reporting Object Model

Business Objects recently unveiled an interactive Enterprise Reporting Object Model section of their support website.  It can be found at http://support.businessobjects.com/global/interactive/xi/om/default.html.  While located in the Crystal Reports XI area of the website, and based on the XI object model, it is backwards-compatible for older versions of Crystal Reports.  So it should be useful for ASP.NET developers using any version of Crystal Reports.

The interactive start helps you as a beginning developer decide which approaches to take with your application.  Utilizing a flowchart, you are presented with options on what type of application is being developed.  As you select different options, a representation of the object model and references display on the right-hand side of the page.  This interactive part is most helpful when first getting used to the object model.

As you become familiar with the object model, it becomes easier to go directly into the documentation to which this model points.  For instance, if you are creating a .NET Windows Forms application you select the CR.NET (WinForm) box under number 2.  Clicking on that will display an information box on the right.  There’s also a graphic you can click on that will display the API information.  From there you will see a graphic representation of the objects.

There are also examples of the object functions as well as links to specific documentation.  For example, click here for an explanation of the ReportDocument class and links to specific documentation.  It’s a great way to discover the Crystal Reports documentation available from the Business Objects web site that is specific to your application.

Code Snippets

Code Snippets (Logging Onto the Database and Exporting Data)

A tool I like to use for various programming tasks is code snippets.  For example, I keep code snippets available that programmatically log on to a Report Documents tables collection for a Crystal Reports document object.  You can find this code in other articles.  I use these examples as snippets because I like the ability to drag and drop the code I need from my Toolbox into my class or code.  Another option would be to create a class to do these generic Crystal Reports tasks.  I do have a series of articles that address this, called “Automagically Display Crystal Parameters: Parts I, II, and III”.  For occasions when the class approach will not work, I have used code snippets to add this code to different applications.

In case you haven’t utilized code snippets, here’s a quick rundown.  You create a new section in the Toolbox of Visual Studio .NET 2003 for your different code snippets by right–clicking in the Toolbox, choosing Add Tab, and keying in My Snippets as the tab name.  With a code page or class open, you highlight the section of code you want to save as a snippet, and drag that section of code into the My Snippets area of the Toolbox (see Figure 1).

Figure 1

 

 

Code Listing 1 (Login Snippet)

C# code

private void ApplyCRLogin(CrystalDecisions.CrystalReports.Engine.ReportDocument  oRpt )
{
 CrystalDecisions.CrystalReports.Engine.Database oCRDb = oRpt.Database; 
 CrystalDecisions.CrystalReports.Engine.Tables oCRTables = oCRDb.Tables; 
 CrystalDecisions.Shared.TableLogOnInfo oCRTableLogonInfo; 
 CrystalDecisions.Shared.ConnectionInfo oCRConnectionInfo = new CrystalDecisions.Shared.ConnectionInfo(); 
 oCRConnectionInfo.DatabaseName = "Northwind"; 
 oCRConnectionInfo.ServerName = "(local)"; 
 oCRConnectionInfo.UserID = "NorthwindUser"; 
 oCRConnectionInfo.Password = "NorthwindUser"; 
 foreach (CrystalDecisions.CrystalReports.Engine.Table oCRTable in oCRTables) 
 { 
  oCRTableLogonInfo = oCRTable.LogOnInfo; 
  oCRTableLogonInfo.ConnectionInfo = oCRConnectionInfo; 
  oCRTable.ApplyLogOnInfo(oCRTableLogonInfo); 
 }
}

VB.NET code

Public Sub ApplyInfo(ByRef _oRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument)
 Dim oCRDb As CrystalDecisions.CrystalReports.Engine.Database = _oRpt.Database
 Dim oCRTables As CrystalDecisions.CrystalReports.Engine.Tables = oCRDb.Tables
 Dim oCRTable As CrystalDecisions.CrystalReports.Engine.Table
 Dim oCRTableLogonInfo As CrystalDecisions.Shared.TableLogOnInfo
 Dim oCRConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo()
 oCRConnectionInfo.DatabaseName = "Northwind" 
 oCRConnectionInfo.ServerName = "(local)" 
 oCRConnectionInfo.UserID = "NorthwindUser" 
 oCRConnectionInfo.Password = "NorthwindUser" 
 For Each oCRTable In oCRTables
  oCRTableLogonInfo = oCRTable.LogOnInfo
  oCRTableLogonInfo.ConnectionInfo = oCRConnectionInfo
  oCRTable.ApplyLogOnInfo(oCRTableLogonInfo)
 Next
End Sub

 

Code Listing 2 (ExportData Snippet)

C# code

public void ExportData(ref CrystalDecisions.CrystalReports.Engine.ReportDocument oRpt) 
{ 
 FileStream fs; 
 long FileSize; 
 CrystalDecisions.Shared.DiskFileDestinationOptions oDest = 
     new CrystalDecisions.Shared.DiskFileDestinationOptions(); 
 string ExportFileName =
     Server.MapPath("/") + ConfigurationSettings.AppSettings["ExportDir"] + Session.SessionID + ".pdf"; 
 try 
 { 
  oRpt.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile; 
  oRpt.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat; 
  oDest.DiskFileName = ExportFileName; 
  oRpt.ExportOptions.DestinationOptions = oDest; 
  oRpt.Export(); 
  Response.Clear(); 
  Response.Buffer = true; 
  Response.AddHeader("Content-Type", "application/pdf"); 
  fs = new FileStream(ExportFileName, FileMode.Open); 
  FileSize = fs.Length; 
  byte[] bBuffer = new byte[System.Convert.ToInt32(FileSize)]; 
  fs.Read(bBuffer, 0, System.Convert.ToInt32(FileSize)); 
  fs.Close(); 
  Response.BinaryWrite(bBuffer); 
  Response.Flush(); 
  Response.Close(); 
 } 
 catch (Exception e) 
 { 
 } 
}

VB.NET code

Public Sub ExportData(ByRef oRpt As Object)
 Dim fs As FileStream
 Dim FileSize As Long
 Dim oDest As New CrystalDecisions.Shared.DiskFileDestinationOptions
 Dim ExportFileName As String =
     Server.MapPath("/") & ConfigurationSettings.AppSettings("ExportDir") & Session.SessionID & ".pdf"
 Try
  oRpt.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile
  oRpt.ExportOptions.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat
  oDest.DiskFileName = ExportFileName
  oRpt.ExportOptions.DestinationOptions = oDest
  oRpt.Export()
  Response.Clear()
  Response.Buffer = True
  Response.AddHeader("Content-Type", "application/pdf")
  fs = New FileStream(ExportFileName, FileMode.Open)
  FileSize = fs.Length
  Dim bBuffer(CInt(FileSize)) As Byte
  fs.Read(bBuffer, 0, CInt(FileSize))
  fs.Close()
  Response.BinaryWrite(bBuffer)
  Response.Flush()
  Response.Close()
 Catch e As Exception
 End Try
End Sub

 

After you’ve added the above snippets to your Toolbox, simply drag the needed snippet from your Toolbox and drop it onto an open code-behind page or class.

More Resources

Business Objects Walkthroughs

There are standard walkthroughs available on the Business Objects DevZone website.  I mainly used these when beginning with Crystal Reports.  I still refer to these samples occasionally when I need to create a report that utilizes a technique I haven’t used for a while.  While these samples are in VB.NET, it should not be difficult to translate them into C#.

For instance, when I want to remember how to set parameters, I check the vbnet_web_discreteparams sample application.  To view the code in that sample, simply download the walkthrough file.  Next, double-click the file and it will unzip the files to a subdirectory under C:\Crystal.  In that directory (C:\Crystal\CRNET), you will find the sample applications in compressed files.  Now, unzip the vbnet_web_discreteparams.zip file (accept the defaults and it will extract to C:\Crystal\CRNET\vbnet_web_discreteparams).  Open the .vb files to view the code, or open the project file in Visual Studio .NET.

Honorable Mentions

Another resource I use when I’m stuck with a particularly tough question is the Crystal Reports forum at http://www.asp.net.  I’ve found a lot of help from folks like Brian Bischof and Richard Dudley who hang out and answer questions on that forum.  It’s a great place for receiving help with both beginner and advanced questions.

For a collection of other lists, check out the Community Links tab at the Crystal Reports Alliance.  As the editor of that site, I like to think that this is a comprehensive list of communities.  This includes mailing lists, newsgroups, and forums related to Crystal Reports and .NET.

Here are a few blogs that have some good links on Crystal Reports: Dr. Crystal, Richard Dudley, and Julie Lerman’s “Don’t Be Iffy”.

Summary

That’s a listing of the resources I utilize when I’m developing a web application involving Crystal Reports.  I hope this provides some helpful links and code for others who need to create a reporting solution that incorporates Crystal Reports.  Keep on Codin’.

 



©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-08-24 11:29:49 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search