Imports CrystalDecisions.CrystalReports.Engine
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim dtAdapter As New SqlDataAdapter
Dim ds As New DataSet
Dim strConnString, strSQL As String
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
'*** for Main Report (Start) ***'
Dim dtMain As DataTable
strSQL = "SELECT * FROM customer ORDER BY CustomerID ASC "
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
dtMain = ds.Tables(0)
'*** for Main Report (End) ***'
'*** for Audit Sub Report (Start) ***'
Dim dtAudit As DataTable
strSQL = "SELECT * FROM audit ORDER BY AuditID ASC "
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
dtAudit = ds.Tables(0)
'*** for Audit Sub Report (End) ***'
'*** for Logo Sub Report (Start) ***'
Dim dtLogo As New DataTable("logo")
Dim dr As DataRow
dtLogo.Columns.Add(New DataColumn("Logo", GetType(System.Byte())))
dtLogo.Columns.Add(New DataColumn("Company", GetType(String)))
Dim fiStream As New FileStream(Server.MapPath("Images/thaicreate.jpg"), FileMode.Open)
Dim binReader As New BinaryReader(fiStream)
Dim pic() As Byte = {}
pic = binReader.ReadBytes(fiStream.Length)
dr = dtLogo.NewRow
dr("Logo") = pic
dr("Company") = "ThaiCreate.Com Co,. LTD"
dtLogo.Rows.Add(dr)
fiStream.Close()
binReader.Close()
'*** for Logo Sub Report (End) ***'
Dim rpt As New ReportDocument()
rpt.Load(Server.MapPath("myCrystalReport.rpt"))
'** for Main Report **'
rpt.SetDataSource(dtMain)
'** for Logo Sub Report **'
rpt.Subreports("Logo_Sub_Report").Database.Tables(0).SetDataSource(dtLogo)
'** for Audit Sub Report **'
rpt.Subreports("Audit_Sub_Report").Database.Tables(0).SetDataSource(dtAudit)
Me.CrystalReportViewer1.ReportSource = rpt
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
End Sub
End Class
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataAdapter dtAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
string strConnString = null;
string strSQL = null;
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
//*** for Main Report (Start) ***'
DataTable dtMain = null;
strSQL = "SELECT * FROM customer ORDER BY CustomerID ASC ";
objConn.ConnectionString = strConnString;
var _with1 = objCmd;
_with1.Connection = objConn;
_with1.CommandText = strSQL;
_with1.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
dtMain = ds.Tables[0];
//*** for Main Report (End) ***'
//*** for Audit Sub Report (Start) ***'
DataTable dtAudit = null;
strSQL = "SELECT * FROM audit ORDER BY AuditID ASC ";
objConn.ConnectionString = strConnString;
var _with2 = objCmd;
_with2.Connection = objConn;
_with2.CommandText = strSQL;
_with2.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
dtAudit = ds.Tables[0];
//*** for Audit Sub Report (End) ***'
//*** for Logo Sub Report (Start) ***'
DataTable dtLogo = new DataTable("logo");
DataRow dr = null;
dtLogo.Columns.Add(new DataColumn("Logo", typeof(System.Byte[])));
dtLogo.Columns.Add(new DataColumn("Company", typeof(string)));
FileStream fiStream = new FileStream(Server.MapPath("Images/thaicreate.jpg"), FileMode.Open);
BinaryReader binReader = new BinaryReader(fiStream);
byte[] pic = {};
pic = binReader.ReadBytes((int)fiStream.Length);
dr = dtLogo.NewRow();
dr["Logo"] = pic;
dr["Company"] = "ThaiCreate.Com Co,. LTD";
dtLogo.Rows.Add(dr);
fiStream.Close();
binReader.Close();
//*** for Logo Sub Report (End) ***'
ReportDocument rpt = new ReportDocument();
rpt.Load(Server.MapPath("myCrystalReport.rpt"));
//** for Main Report **'
rpt.SetDataSource(dtMain);
//** for Logo Sub Report **'
rpt.Subreports["Logo_Sub_Report"].Database.Tables[0].SetDataSource(dtLogo);
//** for Audit Sub Report **'
rpt.Subreports["Audit_Sub_Report"].Database.Tables[0].SetDataSource(dtAudit);
this.CrystalReportViewer1.ReportSource = rpt;
dtAdapter = null;
objConn.Close();
objConn = null;
}
}
คำอธิบาย
'** for Main Report **'
rpt.SetDataSource(dtMain)
'** for Logo Sub Report **'
rpt.Subreports("Logo_Sub_Report").Database.Tables(0).SetDataSource(dtLogo)
'** for Audit Sub Report **'
rpt.Subreports("Audit_Sub_Report").Database.Tables(0).SetDataSource(dtAudit)
เป็นการ Map ตัว Data DataSource กับ Report และ Sub Report