Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

RDLC Report Example in Asp.net with Dataset or Datatable in Visual Studio 2010

Dec 16, 2014
Introduction:

Here I will explain how to create RDLC report in
asp.net using dataset or datatable with example in c#, vb.net in visual studio 2010 or RDLC report example in asp.net with dataset or datatable in visual studio 2010.


Before start implementation first design new table in your database and give name UserInfomation

ColumnName
DataType
UserId
Int(set identity property=true)
UserName
varchar(50)
FirstName
Varchar(50)
LastName
varchar(50)
Location
varchar(50)

After completion of table creation enter some dummy data because we need to use that data to populate reports.

Now Open visual studio and create new website after that right click on your website and select Add New item à in that select Dataset and give proper name and click Add like as shown below

Now Open Dataset and add new datatable for that right click Add à Select Datatable like as shown below

Once we add datatable next step is we need to add columns for that right click on datatable select Add à select Column like as shown below

Here you need to remember one point that is the entire column names should be same as the table (UserInfomation) which we created in database

Suppose if you want to change datatype for any column select respective column right click and select properties

Now Change datatype for that respective column like as shown below

Once we completed adding datatable to dataset now right click on your website and select Add New item à in that select Report Wizard and click Add

After add Report Wizard it will prompt Dataset properties window in that choose previously created dataset as datasource and click Next like as shown below

Once we click Next button it will show Arrange Fields window to select columns to display it in report. Now drag and drop required fields from Available Fields section and place it in Values section like as shown

Once we select our column values you can add custom functions like Sum or Avg or Max or Min etc. by click on arrow icon in each column like as shown below and click Next

Once we click Next button it will ask you to Choose Layout for your application in this just click Next button to select required format in next screen like as shown below.

In Next window Choose a Style and click Finish like as shown below


Once we finished all the steps our report will be like as shown below

Now open your Default.aspx page and place Reportviewer control from toolbox in Reporting tab like as shown below


Once you drag and drop ReportViewer control just Run your application it will throw error like “The Report Viewer Control requires System.Web.UI.ScriptManager”. This error occur because of our ReportViewer control depend on scriptmanager to solve this problem just drag and drop scriptmanager from toolbox in Ajax Extensions tab like as shown below

test
Now if you check your Default.aspx page code that will be like as shown below


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ 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">
<div>
<rsweb:ReportViewer ID="ReportViewer1" runat="server">
</rsweb:ReportViewer>
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</form>
</body>
</html>
Now open your code behind file and add following namespaces

C# Code


using System;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;
VB.NET


Imports System.Data.SqlClient
Imports Microsoft.Reporting.WebForms
Now add the following code in code behind to bind data to our Reportviewer

C# Code


protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
UserInformation dsUsersInfo = new UserInformation();
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from userinformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dsUsersInfo, "DataTable1");
con.Close();
}
ReportDataSource datasource = new ReportDataSource("UserInformation", dsUsersInfo.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
}
VB.NET Code


Imports System.Data.SqlClient
Imports Microsoft.Reporting.WebForms
Partial Class VBCode
Inherits System.Web.UI.Page
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 dsUsersInfo As New UserInformation()
Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("select * from userinformation", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dsUsersInfo, "DataTable1")
con.Close()
End Using
Dim datasource As New ReportDataSource("UserInformation", dsUsersInfo.Tables(0))
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(datasource)
End If
End Sub
End Class
Now run your application your report will be like as shown below

Demo

Download Sample Code Attached


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

11 comments :

Unknown said...

A data source instance has not been supplied for the data source 'DataSet1'.

Naveen Rana said...

Thanks for posting RDLC report example. it is really helpful, when i have to deal with large amount of data. it was so so good and it helped me.

Anonymous said...

UserInformation dsUsersInfo = new UserInformation(); line shows error

Unknown said...

Hi Suresh ,
I got below Exception

"An error has occurred during report processing."

Can you give me solution .

Thanks........

Unknown said...

Hi suresh sir expecting help from your forum, and Also I have used many of your article I have used really It was excellent, Now there is a problem,"How to show Print option in non-IE browsers?" I have googled but didn't find solution. Event I kept one button out of Reportviewer but my manager is saying that client needs print option within the reportviewer. Kindly reply It's an emergency,.

Anonymous said...

Awesome suresh sir...I regularly follow your articles...

guru said...

How to add Header and Footer here?

vinay said...

Its really helpfull to me... i regularly use your articles sir

Narendra Kumar said...

How to print rdlc report

Anonymous said...

Hi Sir, I wants to Print RDLC Report like a Receipt but when i am print alignment not getting correct csn u plz suggest a solution

dddd said...

Hello sir
How to image retrieve from folder using database path in rdlc in asp.net

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.