The. NET program tutorial that uses datasets to display images in Crystal Reports

Source: Internet
Author: User
Tags constructor
Program | tutorials | data | Crystal Report | SHOW Catalog

Describe
File list
Steps
Form1.cs
VB.net edition


Describe

This C #. NET Windows program demonstrates how to create a dataset, add images to a dataset, and pass a dataset to a subreport at run time.


File list

-Bin\debug\canada.jpg
-Bin\debug\germany.jpg
-Bin\debug\japan.jpg
-Bin\debug\usa.jpg
-App.ico
-AssemblyInfo.cs
-CrystalReport1.cs
-Crystalreport1.rpt
-Dynamicimage.csproj
-DynamicImage.csproj.user
-DynamicImage.sln
-Form1.cs
-Form1.resx
-Readme.txt
-Steps.txt


Steps

* Start a new project/create dataset and its schema

-New Project
-Go to Form1.cs code
-Imports System.data/system.io
-Create the function "Createdata" to create the dataset:

DataSet Createdata ()
{
DataSet data = new DataSet ();
Data. Tables.add ("Images");
Data. Tables[0]. Columns.Add ("Country", System.Type.GetType ("System.String"));
Data. Tables[0]. Columns.Add ("img", System.Type.GetType ("system.byte[]"));
Data. WriteXmlSchema (Directory.GetCurrentDirectory () + "\\DynamicImage.xsd");
}

-Create the function "CreateReport" to invoke Createdata to create a dataset pattern:

void CreateReport ()
{
Createdata ();
}

-Call CreateReport in the constructor

Public Form1 ()
{
//
Required for Windows Form Designer support
//
InitializeComponent ();

//
Todo:add any constructor the code after InitializeComponent call
//
CreateReport ();
}

-Constructs and executes the program/will create the dynamicimage.xsd in the Bin\Debug folder.


* Design Report

-Project-> Add New Item
-Select the Crystal and click Open
-Select "as Blank Report" and click "OK"
-Right-click any space, select database-> Add/Remove Database
-Expand ODBC (RDO), select Xtreme Sample Database, and click Finish.
-Expand Table, double-click Customer
-click "OK"
-Drag the Customer Name and last year's Sales to the details
-Right-click any space, insert-> subreport
-Place the subreport next to Sales of last year ' s
-Select "Create subreport" and name the subreport "Flags", and click Report Expert
-Extend "More data sources", select Ado.net (XML)
-Locate the dynamicimage.xsd and click Finish
-Double-click Images
-Click Next, double-click IMG, click Finish
-Click the Links tab
-Double-click Country, and clicking OK
-Resizing the subreport
-Double-click on the subreport to open the subreport
-Delete Report header B and report footer B
-Right-click-> to close the subreport


* Go back to code and write Crystal code

-Drag and drop the Crystalreportviewer control onto the form Form1
-Select CRYSTALREPORTVIEWER1,F4 (properties)
-Change Dock property, fill
-View Code
-Comment out WriteXmlSchema (because a dataset schema file is required only when designing a report)
-Assemble the dataset in the Createdata function and return it

void Addimagerow (DataTable tbl, string name, string filename)
{
FileStream fs = new FileStream (filename, filemode.open);
BinaryReader br = new BinaryReader (FS);
DataRow Row;
row = tbl. NewRow ();
Row[0] = name;
ROW[1] = br. Readbytes ((int) Br. Basestream.length);
Tbl. Rows.Add (row);
br = NULL;
FS = null;
}

DataSet Createdata ()
{
DataSet data = new DataSet ();
Data. Tables.add ("Images");
Data. Tables[0]. Columns.Add ("Country", System.Type.GetType ("System.String"));
Data. Tables[0]. Columns.Add ("img", System.Type.GetType ("system.byte[]"));
Data. WriteXmlSchema (Directory.GetCurrentDirectory () + "\\DynamicImage.xsd");
Addimagerow (data. Tables[0], "USA", directory.getcurrentdirectory () + "\\USA.jpg");
Addimagerow (data. Tables[0], "Canada", directory.getcurrentdirectory () + "\\Canada.jpg");
Addimagerow (data. Tables[0], "Germany", directory.getcurrentdirectory () + "\\Germany.jpg");
Addimagerow (data. Tables[0], "Japan", directory.getcurrentdirectory () + "\\Japan.jpg");
return (data);
}


-Create a report document, pass a dataset to a subreport, and bind the report to the Crystal Report Viewer:

void CreateReport ()
{
CrystalReport1 cr = new CrystalReport1 ();
Cr. Opensubreport ("Flags"). Setdatasource (Createdata ());
Crystalreportviewer1.reportsource = CR;
}



Form1.cs

Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.IO;

Namespace Dynamicimage
{
<summary>
Summary description for Form1.
</summary>
public class Form1:System.Windows.Forms.Form
{
Private CrystalDecisions.Windows.Forms.CrystalReportViewer CrystalReportViewer1;
<summary>
Required designer variable.
</summary>
Private System.ComponentModel.Container components = null;

Process: Addimagerow
Read the image file and add it to the table in the dataset
//
[In] TBL datasheet
Country Country Name
filename of filename image
//
void Addimagerow (DataTable tbl, string name, string filename)
{
FileStream fs = new FileStream (filename, filemode.open); Create a file stream
BinaryReader br = new BinaryReader (FS); Creating a binary Reader
DataRow Row;

Create a new row of data
row = tbl. NewRow ();

Set Country field and image field
Row[0] = name;
ROW[1] = br. Readbytes ((int) Br. Basestream.length);

Adding rows of data to a table
Tbl. Rows.Add (row);

Clear
br = NULL;
FS = null;
}

Function: Createdata
Create a dataset that contains a table with two fields: Country (String), and IMG (blob/byte[])
Add four records to a table
//
DataSet Createdata ()
{
DataSet data = new DataSet ();

Add table ' Images ' to a dataset
Data. Tables.add ("Images");

Add two fields
Data. Tables[0]. Columns.Add ("Country", System.Type.GetType ("System.String"));
Data. Tables[0]. Columns.Add ("img", System.Type.GetType ("system.byte[]"));

Create a dataset pattern (which is used to design a report)
The schema file is no longer required after the report is created
Data. WriteXmlSchema (Directory.GetCurrentDirectory () + "\\DynamicImage.xsd");

Add four lines
Addimagerow (data. Tables[0], "USA", directory.getcurrentdirectory () + "\\USA.jpg");
Addimagerow (data. Tables[0], "Canada", directory.getcurrentdirectory () + "\\Canada.jpg");
Addimagerow (data. Tables[0], "Germany", directory.getcurrentdirectory () + "\\Germany.jpg");
Addimagerow (data. Tables[0], "Japan", directory.getcurrentdirectory () + "\\Japan.jpg");

return (data);
}

Process: CreateReport
Create a report and pass a dataset
//
void CreateReport ()
{
Create a report
CrystalReport1 cr = new CrystalReport1 ();

Passing a dataset (created by calling function Createdata) to the subreport "Flags"
Cr. Opensubreport ("Flags"). Setdatasource (Createdata ());

To pass a report document to the viewer
Crystalreportviewer1.reportsource = CR;
}

Public Form1 ()
{
//
Required for Windows Form Designer support
//
InitializeComponent ();

//
Todo:add any constructor the code after InitializeComponent call
//
CreateReport ();
}

The following section omits ...



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.