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
- Introduction
- Create typed dataset
- Create report and browse data table
- Fill data table in typed dataset
- Display Reports
- Common Issues
- 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.
- 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:
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:
- 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.
- 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.
- public ReportsDS Report_GetReportData()
- {
- SqlConnection sqlConn = new SqlConnection();
- sqlConn.ConnectionString = connectionString;
- ReportsDS ReportDS = new ReportsDS();
- string CompanyId = CachingHelper.ReadFromCache(Constants.CompanyId);
- try
- {
- DataSet ds = new DataSet();
- SqlCommand cmd = new SqlCommand();
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = "SELECT * FROM Company WHERE Id ='" + CompanyId + "'";
- cmd.Connection = sqlConn;
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- da.Fill(ReportDS, ReportDS.Company.TableName);
- } catch (Exception ex)
- {
- throw;
- } finally
- {
- if (sqlConn.State != ConnectionState.Closed) sqlConn.Close();
- sqlConn.Dispose();
- }
- return ReportDS;
- }
- 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.
- private void DisplayReport()
- {
- try
- {
- ReportsDs ReportDS = new ReportsDs();
- ReportDocument obj = new ReportDocument();
- ReportDS = Report_GetReportData();
- obj.FileName = “rptCompanyReport.rpt”;
- obj.SetDataSource((DataTable) ReportDS.Company);
- crystalReportViewer1.ReportSource = obj;
- crystalReportViewer1.Refresh();
- } catch (Exception ex)
- {
- Logging.CustomizedException(ref ex, false);
- }
- }
Report
- 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: