Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

How to: Assign a DataSet to a Crystal report document

In my current project we use type DataSets as a source for the reports we build using Crystal Reports for VS.Net. How this works can be found in the Crystal Reports knowledge base, and more specific in this document.

One of the challenges there was to assign DataSets to the reports once it was created. Based on documentation from Business Objects, the following generic code assigns a DataSet to the ReportDocument and it's subreports.

void AssignDataSet(ReportDocument oReport, DataSet dsData)
{
    DataSet dsNew = dsData.Copy();
 
    // Remove primary key info. CR9 does not appreciate this information!!!
    foreach (DataTable dataTable in dsNew.Tables)
    {
        foreach (DataColumn dataCol in dataTable.PrimaryKey)
        {
            dataCol.AutoIncrement = false;
        }
        dataTable.PrimaryKey = null;
    }
 
    // Now assign the dataset to all tables in the main report
    foreach (CrystalDecisions.CrystalReports.Engine.Table oTable in oReport.Database.Tables)
    {
        oTable.SetDataSource(dsNew);
    }
 
    // Now loop through all the sections and its objects to do the same for the subreports
    foreach (CrystalDecisions.CrystalReports.Engine.Section crSection in oReport.ReportDefinition.Sections)
    {
        // In each section we need to loop through all the reporting objects
        foreach (CrystalDecisions.CrystalReports.Engine.ReportObject crObject in crSection.ReportObjects)
        {
            if (crObject.Kind == ReportObjectKind.SubreportObject)
            {
                SubreportObject crSubReport = (SubreportObject)crObject;
                ReportDocument  crSubDoc = crSubReport.OpenSubreport(crSubReport.SubreportName);
 
                foreach (CrystalDecisions.CrystalReports.Engine.Table oTable in crSubDoc.Database.Tables)
                {
                    oTable.SetDataSource(dsNew);
                }
            }
        }
    }
}

Comments

Jan Schreuder said:

Jan,

Thanks for this example. I had struggled with an error that plagued mr for several hours. I was having a problem properly Set the data source using XML data sources in a generic sort of way. I was trying to set the data source using the data tables themselves to the table in the report. While this seemingly should work it kept throwing a Logon exception. Which it pretty much about as msileading of a message that could possibly exist. The solution apparently was to apply the data set to the report tables.

This methodology really helped me out.

Thanks

John Jordan
# May 20, 2005 4:00 AM

Dadasaheb said:

Hey...plz help me..

when i am trying to assign the dataset to report viewer it throws Error no table found.. when i opens dataset visualizer i can see table data...

my here is the code..

===========================================

da.Fill(ds, "Employee");

CrystalReport1 rpt = new CrystalReport1();

rpt.SetDataSource(ds.Tables["Employee"]);

crystalReportViewer1.ReportSource = rpt;

===============================

Thanx in Advance...

My email Id: dada.kind85@gmail.com

# January 5, 2011 12:21 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 5 and 4 and type the answer here: