Automaticaly Printing RDLC in Asp.Net MVC 3

29 Mar

Download (Code Project)

Introduction
Here i am explaining a small method which i am adopted for automatically printing an rdlc file in my Asp.Net MVC3 application.

In my Asp.Net MVC 3 application I had suffered with the report section. We are using the client definition (rdlc) of Reporting Service for report creation. But the things were stuck on one scenario where our client needs to automatically print the reports by specifying a particular printer name which is presented.

After a lot of googling I realized that it is a hard scenario. Finally we decided to achieve it with acrobat reader and java script to pdf. Requirement is that you must install the acrobat reader plugin in your browser.
The requirement is that there is a printer setting where we are setting a particular printer for a particular report by providing the IP address of the system. According to these settings a particular report must print through a printer which is provided in the settings table.

Using the code
First we designed the report using rdlc. The next step is converting the rdlc to pdf from the action method in the controller. Also we need to inject the javscript for automatically printing the report in acrobat viewer. For that with the help of  iTextSharp dll we converted the rdlc file into pdf at the same time we injected some javscript for automatic printing to the pdf.
You need to include the following namespaces which is in iTextSharp DLL
using iTextSharp.text.pdf;

using iTextSharp.text;

The action method is as follows

public void RecieptPrint(long inv_ReceiptID)

{

           LocalReport localReport = new LocalReport();

           localReport.ReportPath = @”Reprt1.rdlc”;

          DataTable dt = DataSelect();

            The following code is for dynamically setting the data source to rdlc. I already wrote about this here

ReportDataSource reportDataSource = new ReportDataSource();

reportDataSource.Value = dt;

reportDataSource.Name = “DataSet1″;

localReport.SetParameters(param);

localReport.DataSources.Add(reportDataSource);

string reportType = “PDF”;

string mimeType;                  string encoding;

string fileNameExtension = “pdf”;

//The DeviceInfo settings should be changed based on the reportType

string deviceInfo =@”<DeviceInfo>

<OutputFormat>PDF</OutputFormat>

<PageWidth>9.2in</PageWidth>

<PageHeight>12in</PageHeight>

<MarginTop>0.25in</MarginTop>

<MarginLeft>0.45in</MarginLeft>

<MarginRight>0.45in</MarginRight>

<MarginBottom>0.25in</MarginBottom>

      </DeviceInfo>”;
Warning[] warnings;
string[] streams;
byte[] renderedBytes;

Now we are going to converting the rdlc to byte array using the above device info for pdf . To know more about it click here

           renderedBytes= localReport.Render(
reportType,
deviceInfo,out mimeType, out   encoding,               out   fileNameExtension,out streams,out warnings);

Next comes the important section. We are adding this byte array to our pdf file. Also we are injecting the javascript which did the automatic printing into the pdf.

var doc = new Document();

var reader = new PdfReader(renderedBytes);

using (FileStream fs = new FileStream(Server.MapPath(“~/Summary”+ Convert.ToString(Session[“CurrentUserName“]) + “.pdf“),   FileMode.Create))

{

      PdfStamper stamper = new PdfStamper(reader, fs);

      string Printer =     PrinterName(Convert.ToInt32(Session[“localOutletID“]));

// This is the script for automatically printing the pdf in acrobat viewer

stamper.JavaScript= “var pp = getPrintParams();pp.interactive =  pp.constants.interactionLevel.automatic;pp.printerName = ” + Printer + “;print(pp);\r”;

stamper.Close();

}

reader.Close();

}

}

          In view using an iframe we can render this pdf and it will be printed automatically. Requirement is that you must install the acrobat reader plugin in your browser.

Points of Interest

If we saved that pdf instead of printing while opening it will be printed automaticlay.

SQL Query for geting all Stored Procedure under a schema

10 Dec

 SELECT *
FROM DB_Name.information_schema.routines
WHERE routine_type = ‘PROCEDURE
AND SPECIFIC_SCHEMA=’SchemaName

Using this query we can get all the stored procedure under  SchemaName in DB_Name

Adding Report Datasource On Runtime to a rdlc file on ReportViewer

27 Jul

While using client report definition (rdlc) for reporting in a .net application we need to create a dataset for its datatsource. It may cause a problem of diiferent connections for each report are there in a single application . For avoiding  this we can dynamically set datasource for report during run time.

1.      For this first we need to add the rdlc File

2.Add a Dataset for designing the report

3.Add a table adapte r with corresponding stored procedure or query.

4. After creating the data adapter we can access this in our report definition(rdlc)

5. Design the rdlc file using this dataset. After designing remove tha dataset from the project.

We can dynamicaaly assign the datasource from our bussiness logic at runtime to the rdlc file. For this e need to add  MicrosoftReportViwer into our form.

.

Here we can  set the datasource to the rdlc file using the following code.

 BLL.Class obj= new BLL.Class();

 DataTable dtTest = obj.SelectData();

 this.reportViewer1.RefreshReport();

 reportViewer1.Visible = true;

 ReportDataSource rds = new ReportDataSource();

 reportViewer1.Reset();

 reportViewer1.ProcessingMode = ProcessingMode.Local;

 LocalReport rep = reportViewer1.LocalReport;

 rep.Refresh();

 rep.ReportEmbeddedResource =“Report1.rdlc”;

 rds.Name = “DataSet1_Data”

reportViewer1.RefreshReport();

reportViewer1 : Name of the MicrosoftReportViwer control

We can also pass report parameters to rdlc as follows

//Report parameter,

ReportParameter rp = new ReportParameter(“content”,”HELLO”);//(Param name,value)

this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp });

FOR GETTING MONTHS BETWEEN TWO DATES IN SQL

20 Jan

For getting month names between two dates we may use the following Query

DECLARE @date1 DATETIME,@date2 DATETIME

SET @date1 = ‘2010-12-20′

SET @date2 = GETDATE();

WITH cte

AS (

SELECTdatename(month, @date1) AS [Month_Name], @date1 AS dat

UNIONALL

SELECTdatename(month, DateAdd(Month, 1, dat)), DateAdd(Month, 1, dat) FROM cte

WHEREDateAdd(Month, 1, dat) < @date2

)

SELECT [Month_Name]

FROM CTE

 

OURTPUT

Query for Geting dependent tables of a stored procedure

17 Dec

In MS SQL Server 2008, you may get the table names dependents by a stored procedure using the following query

SELECT name

,(SELECT name FROM sys.schemas WHERE schema_id=t.schema_id) AS schemaName

FROM  sys.tables t

WHERE  t.object_id in (SELECT depid

FROM sys.sysdepends

WHERE id In (SELECT s.object_id FROM sys.procedures s WHERE name=’SPName‘))

SPName  is the name of  stored procedure.

Th output will be the table name and its schema. Happy querying .. :)

Aside

Best DB

15 Nov

Avoid iframe scrolling issue Using Jquery

15 Nov

In my asp.net web application am using iframe. But i faced some scrolling issues. ie, some of my pages are large in size, then there will be scroll bars for browsers and iframe. Am stuck in this issues for long time. At last i find out a solution using jquery. I fix browser’s scrolling off and set height and width of iframe related to browsers height and width, and then set scroll bars for iframe. Here I am success with the following jquery,

var newheight = $(document.body).height();

var newH = parseInt(newheight) – 90;

var newHI = parseInt(newheight) – 130;

var width = $(document.body).width();

var newwidth = $(‘#LeftPane‘).height(newH).width();

var newW = parseInt(width) – (parseInt(newwidth) + 30);

$(“#ifrforms).height(newHI).width(newW);

Here LeftPane is div in my page where all menus resides. I just wants to set scroll bar for that too based on browser’s height and width.

Follow

Get every new post delivered to your Inbox.