原文链接:http://www.aspsnippets.com/Green/Articles/ASPNet-Report-Viewer-control-Tutorial-with-example.aspx

Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here
 
1. Add Typed DataSet to the ASP.Net Website
Since I am using disconnected Crystal Reports we will make use of Typed DataSet to populate the Crystal Reports with data from database.

 
 
2. Adding DataTable to the Typed DataSet
Our next step would be to add a DataTable to the Type DataSet.

 
 
3. Adding Columns or fields to DataTable
In the DataTable we need to specify the column names that we want to display in the Crystal Report.
 
Note: The Column Names of the DataTable must exactly match with the actual Database Table column names.

By default all the columns are of String Data Type but you can also change the data type as per your need.
 
4. Adding the RDLC Report
Using the Add New Item option in Visual Studio you need to add new RDLC Report. I am making use of Report Wizard so that it make easier to configure the Report.

 
5. Choose the DataSet
Now we need to choose the DataSet that will act as the DataSource for the RDLC Report. Thus we need to select the Customers DataSet that we have created earlier.

 
6. Choose the Fields to be displayed in the RDLC Report
Next we need to choose the fields we need to display, we need to simply drag and drop each fields into the Values Box as shown in the screenshot below

 
7. Choose the Layout
The next dialog will ask us to choose the layout, we can simply skip it as of now as this is a simple Report with no calculations involved.

 
8. Choose the Style
Finally we need to choose the style, i.e. color and theme of the Report.

 
Once you press Finish button on the above step, the Report is ready and is displayed in the Visual Studio as shown below

 
9. Adding Report Viewer to the page
In order to display the Report we will need to add ReportViewer control to the page from the Toolbox. The ReportViewer controls requires ScriptManager on the page.

 
Once you add the ReportViewer control to the page, your page must look as below
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="600">
    </rsweb:ReportViewer>
    </form>
</body>
</html>
 
 
10. Populating the RDLC Report from Database
Below is the code to populate the RDLC Report from database. The first statement notifies the ReportViewer control that the Report is of type Local Report.
Then the path of the Report is supplied to the ReportViewer, after that the Customers DataSet is populated with records from the Customers Table is set as ReportSource to the Report.
C#
Namespaces
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;
 
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ReportViewer1.ProcessingMode = ProcessingMode.Local;
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
        Customers dsCustomers = GetData("select top 20 * from customers");
        ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(datasource);
    }
}
 
private Customers GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
 
            sda.SelectCommand = cmd;
            using (Customers dsCustomers = new Customers())
            {
                sda.Fill(dsCustomers, "DataTable1");
                return dsCustomers;
            }
        }
    }
}
 
VB.Net
Namespaces
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports Microsoft.Reporting.WebForms
 
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ReportViewer1.ProcessingMode = ProcessingMode.Local
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
        Dim dsCustomers As Customers = GetData("select top 20 * from customers")
        Dim datasource As New ReportDataSource("Customers", dsCustomers.Tables(0))
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(datasource)
    End If
End Sub
 
Private Function GetData(query As String) As Customers
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As New SqlCommand(query)
    Using con As New SqlConnection(conString)
        Using sda As New SqlDataAdapter()
            cmd.Connection = con
 
            sda.SelectCommand = cmd
            Using dsCustomers As New Customers()
                sda.Fill(dsCustomers, "DataTable1")
                Return dsCustomers
            End Using
        End Using
    End Using
End Function
 

 
 
Demo
 
Downloads
 
 

[转] Asp.net Report Viewer 简单实例的更多相关文章

  1. Asp.Net读取服务器EXE文件的方法!(超简单实例)

    Asp.Net读取服务器EXE文件的方法!(超简单实例) Process process = new Process(); process.StartInfo.FileName = "d:\ ...

  2. 简单实例一步一步帮你搞清楚MVC3中的路由以及区域

    我们都知道MVC 3 程序的所有请求都是先经过路由解析然后分配到特定的Controller 以及 Action 中的,为什么这些知识讲完了Controller Action Model 后再讲呢?这个 ...

  3. resteasy简单实例

    1.建一个maven web项目 新建一个maven项目,next,第一个框不要勾选 选择maven-archetype-webapp,建一个web项目 键入项目组织id与项目id 一般此时搭建的只是 ...

  4. SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论 SignalR 简单示例 通过三个DEMO学会SignalR的三种实现方式 SignalR推送框架两个项目永久连接通讯使用 SignalR 集线器简单实例2 用SignalR创建实时永久长连接异步网络应用程序

    SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论   异常汇总:http://www ...

  5. 关于操作 ASP.NET Web API的实例

    WCF的野心造成了它的庞大复杂,HTTP的单纯造就了它的简单优美.为了实现分布式Web应用,我们不得不将两者凑合在一起 —— WCF服务以HTTP绑定宿主于IIS. 于是有了让人晕头转向的配置.让人郁 ...

  6. Web Services调用存储过程简单实例

    转:http://www.cnblogs.com/jasenkin/archive/2010/03/02/1676634.html Web Services 主要利用 HTTP 和 SOAP 协议使商 ...

  7. Hibernate(二)__简单实例入门

    首先我们进一步理解什么是对象关系映射模型? 它将对数据库中数据的处理转化为对对象的处理.如下图所示: 入门简单实例: hiberante 可以用在 j2se 项目,也可以用在 j2ee (web项目中 ...

  8. 最新 Eclipse IDE下的Spring框架配置及简单实例

    前段时间开始着手学习Spring框架,又是买书又是看视频找教程的,可是鲜有介绍如何配置Spring+Eclipse的方法,现在将我的成功经验分享给大家. 本文的一些源代码来源于码农教程:http:// ...

  9. 修改js confirm alert 提示框文字的简单实例

    修改js confirm alert 提示框文字的简单实例: <!DOCTYPE html> <html> <head lang="en"> & ...

随机推荐

  1. CSS透明属性详解代码

    透明往往能产生不错的网页视觉效果,先奉上兼容主流浏览器的CSS透明代码: 代码如下: .transparent_class { filter:alpha(opacity=50); -moz-opaci ...

  2. 【UVA】【10828】随机程序

    数学期望/高斯消元/马尔可夫过程 刘汝佳老师白书上的例题- -b 本体不满足拓扑关系,但马尔可夫过程是可以高斯消元解的…… 用「高斯·约当消元」更方便! //UVA 10828 #include< ...

  3. 【BZOJ】【2844】albus就是要第一个出场

    高斯消元解XOR方程组 srO  ZYF  Orz 膜拜ZYF…… http://www.cnblogs.com/zyfzyf/p/4232100.html /******************** ...

  4. KASS分布式文件系统(Kass File System)

    KASS分布式文件系统(Kass File System),简称KFS,是开始公司自主研发的分布式文件存储服务平台.KFS系统架构及功能服务类似Hadoop/GFS/DFS,它通过HTTP-WEB为上 ...

  5. java基础知识回顾之---java String final类之intern方法

    public class StringObjectDemo { /** * @param args */ public static void main(String[] args) { String ...

  6. 简单易懂的现代魔法——Play Framework攻略4

    接前文:简单易懂的现代魔法——Play Framework攻略3 1.The Object 时隔2个多月,Play Framework系列又更新了,本次的主题是:利用Play Framework实现R ...

  7. DB2 中将date类型的转换成timestamp

    方法1: TIMESTAMP(Char(date)||'00.00.00') SELECT T2.RECORDNO,T2.DANGERTIME,T2.BIZORIGIN,T3.COMMONNAME A ...

  8. TopCoder 603 div1 & div2

    div2 250pts MiddleCode 题意:s串长度为奇数时,将中间字符取掉并添加到t末尾:长度为偶数时,将中间两个较小的字符取掉并添加到末尾. 分析:直接做,学习了一下substr(s, p ...

  9. VIM Taglist + ctags

    Windows下 进入http://ctags.sourceforge.net/ 下载ctags 把ctags58.zip解压,随便放个地方,我放到了Home\Vim\vim72下,在ctags58文 ...

  10. 基于Struts2框架实现登录案例 之 使用Struts2标签库简化表单+继承ActionSupport完成输入交验

    一,使用Struts2标签库简化表单 在文章[基于Struts2框架实现登录案例]的基础上,通过使用Struts标签库可以简化登录页面login2.jsp <%@ page language=& ...