USE [mydatabase]
GO
/****** Object: Table [dbo].[customer] Script Date: 03/13/2012 13:42:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[customer](
[CustomerID] [varchar](4) NOT NULL,
[Name] [varchar](50) NULL,
[Email] [varchar](50) NULL,
[CountryCode] [varchar](2) NULL,
[Budget] [float] NULL,
CONSTRAINT [PK_customer] PRIMARY KEY CLUSTERED
(
[CustomerID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO customer VALUES ('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000);
INSERT INTO customer VALUES ('C002', 'John Smith', '[email protected]', 'EN', 2000000);
INSERT INTO customer VALUES ('C003', 'Jame Born', '[email protected]', 'US', 3000000);
INSERT INTO customer VALUES ('C004', 'Chalee Angel', '[email protected]', 'US', 4000000);
GO
SET ANSI_PADDING OFF
country
USE [mydatabase]
GO
/****** Object: Table [dbo].[country] Script Date: 03/13/2012 13:42:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[country](
[CountryCode] [varchar](2) NOT NULL,
[CountryName] [varchar](50) NULL,
CONSTRAINT [PK_country] PRIMARY KEY CLUSTERED
(
[CountryCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO country VALUES ('TH', 'Thailand');
INSERT INTO country VALUES ('EN', 'English');
INSERT INTO country VALUES ('US', 'United States');
GO
SET ANSI_PADDING OFF
audit
USE [mydatabase]
GO
/****** Object: Table [dbo].[audit] Script Date: 03/13/2012 13:45:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[audit](
[AuditID] [varchar](4) NOT NULL,
[CustomerID] [varchar](4) NULL,
[Date] [datetime] NULL,
[Used] [float] NULL,
CONSTRAINT [PK_audit] PRIMARY KEY CLUSTERED
(
[AuditID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO audit VALUES (1, 'C001', '2011-08-01', 100000);
INSERT INTO audit VALUES (2, 'C001', '2011-08-05', 200000);
INSERT INTO audit VALUES (3, 'C001', '2011-08-10', 300000);
INSERT INTO audit VALUES (4, 'C002', '2011-08-02', 400000);
INSERT INTO audit VALUES (5, 'C002', '2011-08-07', 100000);
INSERT INTO audit VALUES (6, 'C002', '2011-08-15', 300000);
INSERT INTO audit VALUES (7, 'C003', '2011-08-20', 400000);
INSERT INTO audit VALUES (8, 'C003', '2011-08-25', 200000);
INSERT INTO audit VALUES (9, 'C004', '2011-07-04', 100000);
INSERT INTO audit VALUES (10, 'C005', '2011-07-04', 200000);
GO
SET ANSI_PADDING OFF
กลับมาที่ Web Form ให้สร้าง Control ของ Textbox และ Button พร้อมทั้งลาก Control ชื่อ CrystalReportViewer เหมือนในภาพ จากนั้นให้สร้าง Event ของ Button Load Report ใส่ code ดังต่อไปนี้
Code VB.NET
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnShowReport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnShowReport.Click
Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim dtAdapter As New SqlDataAdapter
Dim ds As New DataSet
Dim dt As DataTable
Dim strConnString As String
Dim strSQL As New StringBuilder
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
strSQL.Append(" SELECT a.CustomerID,a.Name,a.Email,a.Budget, ")
strSQL.Append(" b.CountryName,c.Date,c.Used ")
strSQL.Append(" FROM customer a ")
strSQL.Append(" LEFT JOIN country b ON a.CountryCode = b.CountryCode ")
strSQL.Append(" LEFT JOIN audit c ON a.CustomerID = c.CustomerID ")
strSQL.Append(" WHERE a.CustomerID = '" & Me.txtCustomerID.Text & "' ")
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL.ToString()
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds, "myDataReport")
dt = ds.Tables(0)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Dim rpt As New ReportDocument()
rpt.Load(Server.MapPath("CrystalReport1.rpt"))
rpt.SetDataSource(dt)
Me.CrystalReportViewer1.ReportSource = rpt
End Sub
End Class
Code C#
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using CrystalDecisions.CrystalReports.Engine;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
}
protected void btnShowReport_Click(object sender, EventArgs e)
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataAdapter dtAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = null;
string strConnString = null;
StringBuilder strSQL = new StringBuilder();
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
strSQL.Append(" SELECT a.CustomerID,a.Name,a.Email,a.Budget, ");
strSQL.Append(" b.CountryName,c.Date,c.Used ");
strSQL.Append(" FROM customer a ");
strSQL.Append(" LEFT JOIN country b ON a.CountryCode = b.CountryCode ");
strSQL.Append(" LEFT JOIN audit c ON a.CustomerID = c.CustomerID ");
strSQL.Append(" WHERE a.CustomerID = '" + this.txtCustomerID.Text + "' ");
objConn.ConnectionString = strConnString;
var _with1 = objCmd;
_with1.Connection = objConn;
_with1.CommandText = strSQL.ToString();
_with1.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds, "myDataReport");
dt = ds.Tables[0];
dtAdapter = null;
objConn.Close();
objConn = null;
ReportDocument rpt = new ReportDocument();
rpt.Load(Server.MapPath("CrystalReport1.rpt"));
rpt.SetDataSource(dt);
this.CrystalReportViewer1.ReportSource = rpt;
}
}
หลังจากวาง Code เรียบร้อยแล้ว ลองกดที่ Run เพื่อดูผลลัพธ์ของ Report