Display Crystal Report With Images Using Typed Dataset

Abstract

This document focuses on how to display crystal reports with images using typed dataset. While displaying reports instead of querying to database, we can use already filled data tables. This design is based on Crystal Reports for Visual Studio .NET.

H/W Platform: Windows XP
S/W Environment: MS SQL Server 2005, Crystal Reports for Visual Studio 2008

Table of Contents

  1. Introduction
  2. Create typed dataset
  3. Create report and browse data table
  4. Fill data table in typed dataset
  5. Display Reports
  6. Common Issues
  1. Introduction

    This BOK explains simple steps on how to create crystal reports using typed dataset. The instances in this document have been illustrated considering “Company” table which stores general data as well Logo images.

  2. Create typed dataset

    2.1. Company Master table schema.

    This is company master table schema which stores company information and company logo images.

    Database Table schema:

    Database Table schema

    2.2. Typed data set in .NET

    Follow these steps to add typed data set in your report project.

    Go to Project, Add, New Item, Data, then Data Set

    Once data set is added in your project then drag and drop database table from server explorer.

    In this example I have used Company table.

    Data Table:

    table

  3. Create report and browse data table

    3.1 Instead of using direct table from database we can use data table from typed data set
    3.2 Dataset is available under Project Data, then ADO.Net DataSets
    3.3 Select the data table from available data source and add to selected tables section.
    3.4 Once data table is added click “Ok” button to map the data table in crystal report.

    create

  4. Fill data table in typed dataset

    4.1 Fetch the data from database and fill it in the data table in dataset.
    4.2 This method returns the data set in display report method.
    1. public ReportsDS Report_GetReportData()  
    2. {  
    3.     SqlConnection sqlConn = new SqlConnection();  
    4.     sqlConn.ConnectionString = connectionString;  
    5.     ReportsDS ReportDS = new ReportsDS();  
    6.     string CompanyId = CachingHelper.ReadFromCache(Constants.CompanyId);  
    7.     try  
    8.     {  
    9.         DataSet ds = new DataSet();  
    10.         SqlCommand cmd = new SqlCommand();  
    11.         cmd.CommandType = CommandType.Text;  
    12.         cmd.CommandText = "SELECT * FROM Company WHERE Id ='" + CompanyId + "'";  
    13.         cmd.Connection = sqlConn;  
    14.         SqlDataAdapter da = new SqlDataAdapter(cmd);  
    15.         da.Fill(ReportDS, ReportDS.Company.TableName);  
    16.     } catch (Exception ex)   
    17.     {  
    18.         throw;  
    19.     } finally  
    20.     {  
    21.         if (sqlConn.State != ConnectionState.Closed) sqlConn.Close();  
    22.         sqlConn.Dispose();  
    23.     }  
    24.     return ReportDS;  
  5. Display Reports

    5.1 Create instance of Report Document and set DataSource as data table
    5.2 Set ReportSource as Report Document in crystal report viewer
    5.3 Call DisplayReport() method in events like button click.
    1. private void DisplayReport()  
    2. {  
    3.     try  
    4.     {  
    5.         ReportsDs ReportDS = new ReportsDs();  
    6.         ReportDocument obj = new ReportDocument();  
    7.         ReportDS = Report_GetReportData();  
    8.         obj.FileName = “rptCompanyReport.rpt”;  
    9.         obj.SetDataSource((DataTable) ReportDS.Company);  
    10.         crystalReportViewer1.ReportSource = obj;  
    11.         crystalReportViewer1.Refresh();  
    12.     } catch (Exception ex)   
    13.     {  
    14.         Logging.CustomizedException(ref ex, false);  
    15.     }  
    16. }  
    Report

    report

  6. Common Issues

    6.1 Issue of null fields – If image field is null, report will not display image.
    6.2 Set proper image size to avoid blur effects.
Read more articles on Crystal Reports:

Up Next
    Ebook Download
    View all
    Learn
    View all