We will write an RDLC report on the server silently without visibility to the user.
- Create a new asp.net website project,
Give name as “RdlcWriteOnServer”.
- Right click on project,
Add --> Add New Item --> Web Form.
- Add Web Form,
Give file name as DEFAULT.ASPX.
- Now, Insert a new Report File (RDLC).
Report file named as FRIENDREPORT.RDLC.
- Open your WEB.CONFIG file and set connection strings.
- <connectionStrings>
- <add name="RDLCConnectionString" connectionString="Data Source=SAIBABA-PC\SAIBABA;Initial Catalog=MemberCDAC;Integrated Security=True" providerName="System.Data.SqlClient" />
- </connectionStrings>
Table Structure:
- USE[MBKTest]
- GO
- /****** Object: Table [dbo].[tblFriends] Script Date: 02/17/2016 11:05:19 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- SET ANSI_PADDING ON
- GO
- CREATE TABLE[dbo].[tblFriends]
- (
- [FriendID][int] IDENTITY(1, 1) NOT NULL, [Name][varchar](50) NULL, [Place][varchar](25) NULL, [Mobile][varchar](15) NULL, [EmailAddress][varchar](150) NULL
- ) ON[PRIMARY]
- GO
- SET ANSI_PADDING OFF
- GO
- Right click on proejct add a DATASET,
Give the name of file “FriendDataSet.xsd”
As you add dataset file, system prompt you this message,
Select Yes,
This is will generate a single DLL file of your DATASET and OTHER FILES.
- Now, double click on FREINDDATASET.XSD file.
Right click on canvas of dataset file.
Select Add-->DataTable (To create a hardcoded datatable inside dataset).
- As you click on DataTable screen will look like this:
Single click on DataTable1 and rename to FriendDataTable.
Right click on FriendDataTable and add new column or press CTRL+L .
Add the following columns:
• FriendID
• Name
• Place
• Mobile
• EmailAddress
- After adding the above said field FriendDataSet.xsd's FriendDataTable looks like this.
- Double click on RDLC file. (FriendReport.rdlc)
On your left side you can see ToolBox containing three tabs.
a. ReportData
b. Server Explorer
c. ToolBox
If you can see ReportData toolbox Press CTRL + ALT + D to activate ReportData toolbox option. Click on Report Data Toolbox and Select DataSets,
In DataSource dropdownlist select your strong typed DATASET called FriendDataSet.
In Available DataSets dropdownlist select your strongly typed DataTable called FriendDataTable.
- After selection of DataTable press ok. You can see Dataset1 populated with Fields.
- Now, click on ToolBox and Select Table tool Option. Drag and drop Table on the report canvas. Three columns are created by default. You can add more columns by simply right clicking.
- When you click o the table header and press right click you can addan extra column, it will display like this.
- Report Header and Footer,
Click on REPORT menu from menu bar.
- You can see there is option to activate Add Page Header, Add Page Footer.
- Select ToolBox and Select TextBox tool and write, CSharpCorner Friend List as report title.
- Your report should look like this:
NOTE:
While you export RDLC report as PDF format it expands the column size visually, so make it as reduced as you can and check export copy as per your report UI.
IMPORTANT NOTE:
In webform you can give DLL reference manually and write web.config code for report viewer.
1. Drag N Drop Report viewer on DEFAULT.ASPX, it will auto write and upate your WEB.CONFIG file and reference in the code behind file.
OR
2. Writ the following code in WEB.CONFIG file and rebuild the solution.
Under system.web section. - <compilation debug="true" targetFramework="4.5">
- <assemblies>
- <add assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" />
- <add assembly="Microsoft.ReportViewer.Common, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" />
- <add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
- </assemblies>
- </compilation>
- DEFAULT.ASPX code
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
-
- <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
-
- </div>
- </form>
- </body>
-
- </html>
- DEFAULT.ASPX.CS code behind,
- using Microsoft.VisualBasic;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Diagnostics;
- using System.Configuration;
- using System.Data.SqlClient;
- using Microsoft.Reporting.WebForms;
- using System.IO;
- using Microsoft.ReportingServices;
- partial class _Default: System.Web.UI.Page
- {
-
- protected void Page_Load(object sender, EventArgs e)
- {
-
- CreatePDF();
-
-
- }
-
-
-
- private string CreatePDF()
- {
-
- FriendDataSet frndDataSet = new FriendDataSet();
-
-
- string constr = ConfigurationManager.ConnectionStrings["RDLCConnectionString"].ConnectionString;
-
- SqlConnection con = new SqlConnection(constr);
- SqlDataAdapter da = new SqlDataAdapter("Select * From tblFriends", con);
- da.Fill(frndDataSet, "FriendDataTable");
-
-
- ReportDataSource datasource = new ReportDataSource("DataSet1", frndDataSet.Tables["FriendDataTable"].DefaultView);
-
-
- Warning[] warnings = null;
- string[] streamids = null;
- string mimeType = null;
- string encoding = null;
- string extension = null;
-
-
- ReportViewer viewer = new ReportViewer();
- viewer.ProcessingMode = ProcessingMode.Local;
- viewer.LocalReport.ReportPath = "FriendReport.rdlc";
- viewer.LocalReport.DataSources.Clear();
- viewer.LocalReport.DataSources.Add(datasource);
- viewer.LocalReport.EnableHyperlinks = true;
- viewer.ShowParameterPrompts = true;
-
- viewer.LocalReport.Refresh();
-
-
- string path = @ "G:\Article\RdlcWriteOnServer\PDF\";
-
- Guid id = Guid.NewGuid();
-
- dynamic savePath2 = (path + "\\FriendList" + id.ToString() + ".pdf");
-
- byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
-
- FileStream file =
- default (FileStream);
-
- file = new FileStream(savePath2, FileMode.Create);
- file.Write(bytes, 0, bytes.Length);
-
- file.Close();
- file.Dispose();
-
- return savePath2;
- }
- }
- Run Application and check in folder G:\Article\RdlcWriteOnServer\PDF.
Inthe next article I will explain How to EXPORT RDLC report at CLIENT / USER SIDE.