Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > ReportViewer and DataSet ออกรายงานกับการสร้าง DataSource ด้วย DataSet



 
Clound SSD Virtual Server

ReportViewer and DataSet ออกรายงานกับการสร้าง DataSource ด้วย DataSet

ReportViewer and DataSet ออกรายงาน Report กับการสร้าง DataSource ด้วย DataSet วิธีนี้เหมาะสำหรับผู้ที่ชอบเขียน Code Behide ด้วยการสร้าง DataSet หรือ DataTable ด้วย ADO.NET ผ่านการ Query ต่าง ๆ และได้ค่าเป็น DataSet หรือ DataTable กลับมา จากนั้นค่อยนำ DataSet หรือ DataTable ไปสร้าง DataSource ให้กับ ReportViewer และก็จะได้ Report ออกมาตามรูปแบบ Query ที่ต้องการ

Screenshot

ReportViewer and DataSet

ตัวอย่าง Report ที่แสดงบนเว็บ (ReportViewer สามารถออก Report ได้ทั้งบน Web (Asp.net) หรือ Windows Application หรือบน Application อื่น ๆ )

Icons

ReportViewer and DataSet

Icons ของ Control ชื่อว่า MicrosoftReportViewer ที่อยู่บน Visual Studio 2005 (.NET 2.0) และ Visual Studio 2008 (.NET 3.0,3.5)


ReportViewer and DataSet

Icons ของ Control ชื่อว่า ReportViewer ที่อยู่บน Visual Studio 2010 (.NET 4.0)

Basic ReportViewer Control


เริ่มต้นการสร้าง Report

ตัวอย่างนี้จะเป็นการออก Report รายชื่อฐานข้อมูลของลูกค้า โดยมีตารางชื่อว่า customer จัดเก็บบนฐานข้อมูล SQL Server จาก Screenshot ข้างบนจะเห็นว่ามีการเปิดรับ Parameters ที่เป็น CustomerID และนำค่าที่ได้ไปค้นหาจากฐานข้อมูลในรูปแบบของ DataSet และ DataTable จากนั้นค่อยสร้าง DataSource แสดงผลบน Report


Database ของ SQL Server
USE [mydatabase]
GO
/****** Object:  Table [dbo].[customer]    Script Date: 05/01/2012 16:44:29 ******/
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,
	[Used] [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]

GO
SET ANSI_PADDING OFF

INSERT INTO customer VALUES ('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000);
INSERT INTO customer VALUES ('C002', 'John  Smith', '[email protected]', 'EN', 2000000, 800000);
INSERT INTO customer VALUES ('C003', 'Jame Born', '[email protected]', 'US', 3000000, 600000);
INSERT INTO customer VALUES ('C004', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000);


นำ Query ไปรันเพื่อสร้าง Table และข้อมูล

ReportViewer and DataSet

โครงสร้างฐานข้อมูลที่ถูกจัดเก็บบนฐานข้อมูล SQL Server








ReportViewer and DataSet

สร้างโปรเจค ASP.NET Web Application หรือ Application อื่น ๆ เช่น Windows Form Application ซึ่งการใช้งานแทบจะไม่ต่างกันเลย

ReportViewer and DataSet

ไฟล์เริ่มต้นของ Project ตอนนี้ยังไม่มีไฟล์อะไรเพิ่มเติม นอกจากไฟล์ Default ที่มีมาให้กับการ New Project


การสร้าง DataSet

ReportViewer and DataSet

ทำการสร้าง DataSet ด้วยการ Add New Item และเลือก DataSet ตั้งชื่อเป็น myDataSet.xsd

ReportViewer and DataSet

สร้าง DataTable ชื่อว่า myDataTable ทีมีโครงสร้างประกอบด้วย

myDataTable
- CustomerID
- Name
- Email
- CountryCode
- Budget
-Used


บทความที่เกี่ยวข้องการกับสร้าง DataSet


หลังจากที่สร้าง DataSet เรียบร้อยแล้ว เราจะได้ชุดของ DataSet และ DataTable ขึ้นมา 1 ชุดซึ่งตอนที่เรียกใช้จะเรียกเป็น myDataSet_myDataTable

การสร้าง Report

ReportViewer and DataSet

กลับมาที่ Project เลือก Add New Item เลือก Report Wizard โดยกำหนดชื่อเป็น myReport.rdlc

ReportViewer and DataSet

หน้าจอแสดงข้อความต้อนรับการสร้าง Report แบบ Wizard

ReportViewer and DataSet

เลือก DataSource ที่ได้จากขั้นตอนการสร้าง DataSet ซึ่งชื่อว่า myDataSet และ myDataTable ตามลำดับ

ReportViewer and DataSet

เลือกรูปแบบ Report

ReportViewer and DataSet

ปรับแต่ง Table ที่ได้จาก DataTable

ReportViewer and DataSet

ปรับแต่ง Layout ของ Report

ReportViewer and DataSet

เลือก Themes หรือ Style หรือสีของ Report

ReportViewer and DataSet

คลิกที่ Finish เพื่อเสร็จสิ้นการสร้าง Report แบบ Wizard

ReportViewer and DataSet

ตัวนี้เป็นไฟล์ myReport.rdlc ที่ได้จากการสร้าง Wizard ซึ่งสามารถปรับแต่ง Layout หรือขยายเพิ่มอื่น ๆ ได้ตามต้องการ








กลับมายัง Web Form

ReportViewer and DataSet

กลับมาที่ Web Form สร้าง Design ด้วย Control ต่าง ๆ ดังรูป พร้อมกับลาก Control ของ ReportViewer หรือ MicrosoftReportViewer เข้ามาใน Web Form

เมื่อเสร็จแล้วให้กลับมาที่ปุ่ม Button บน Web Form ให้สร้าง Event ดังนี้

- VB.NET

Imports System.Data.SqlClient
Imports Microsoft.Reporting.WebForms

Partial Public Class _Default
    Inherits System.Web.UI.Page


    Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnShow.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, strSQL As String

        strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
        strSQL = "SELECT * FROM customer WHERE CountryCode LIKE '%" & Me.txtCountryCode.Text & "%'  "

        objConn.ConnectionString = strConnString
        With objCmd
            .Connection = objConn
            .CommandText = strSQL
            .CommandType = CommandType.Text
        End With
        dtAdapter.SelectCommand = objCmd

        dtAdapter.Fill(ds)
        dt = ds.Tables(0)

        dtAdapter = Nothing
        objConn.Close()
        objConn = Nothing

        ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("myDataSet_myDataTable", dt))
        ReportViewer1.LocalReport.ReportPath = "myReport.rdlc"
        ReportViewer1.LocalReport.Refresh()

    End Sub

End Class


- C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;

public partial class _Default : System.Web.UI.Page
{



	protected void btnShow_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;
		string strSQL = null;

		strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
		strSQL = "SELECT * FROM customer WHERE CountryCode LIKE '%" + this.txtCountryCode.Text + "%'  ";

		objConn.ConnectionString = strConnString;
		var _with1 = objCmd;
		_with1.Connection = objConn;
		_with1.CommandText = strSQL;
		_with1.CommandType = CommandType.Text;
		dtAdapter.SelectCommand = objCmd;

		dtAdapter.Fill(ds);
		dt = ds.Tables[0];

		dtAdapter = null;
		objConn.Close();
		objConn = null;

		ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("myDataSet_myDataTable", dt));
		ReportViewer1.LocalReport.ReportPath = "myReport.rdlc";
		ReportViewer1.LocalReport.Refresh();

	}

}


จาก Code เมื่อทำการ Query ได้ DataTable จะมีการสร้าง DataSource ให้กับ DataSet ของ Report ผ่าน new ReportDataSource("myDataSet_myDataTable", dt)

ทดสอบ Report

ReportViewer and DataSet

หน้าจอ Report ที่ได้จาก ReportViewer ที่ได้จากการสร้าง DataSet

สรุป
วิธีนี้จะให้ความสำคัญกับการสร้าง DataSource บน Code Behide ซึ่งจาก Code จะเห็นว่ามีการเขียน Query เพื่อ SELECT ข้อมูลจากตาราง customer และ WHERE ด้วย CountryCode = 'ค่าที่รับมา' และถ้าต้องการสร้าง DataSource จากแหล่งอื่น ๆ ก็ได้เช่นเดียวกัน เช่นการสร้าง DataTable หรือจะ JOIN Query จากหลาย ๆ ตาราง ก็เป็นเรื่องที่ทำได้ไม่ยาก และอาจจะง่ายกกว่าการใช้ผ่าน Wizard ซะอีก

บทความตัวอย่างการสร้าง DataSet ผ่านการ JOIN ตาราง และแสดงผลบน Crystal Report



ดาวน์โหลด Code ในบทความ
Download Code !!




บทความอื่น ๆ ที่เกี่ยวข้อง
Go to : ASP.NET ReportViewer - rsweb:ReportViewer
Go to : การสร้าง Report ด้วย Report Viewer และการส่งค่า Parameters (ReportViewer , MicrosoftReportViewer)
Go to : สุดยอดบทความ Crystal Report กับ .NET Framework (VB.NET , C#)
Go to : Walkthrough: Creating a ReportViewer Report


   
Share
Bookmark.   

  By : TC Admin
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2012-05-30
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่