[Solved] Print ASP.Net ReportViewer Error: No printers are installed in IIS

samsmuthu
 
on Apr 12, 2021 07:52 PM
3747 Views

Hi,

I have been tried Print ReportViewer without Print Preview using C# and VB.Net in ASP.Net

  1. Internet explorer -- working
  2. Google Chrome -- not working
  3. Microsoft Edge -- working
  4. Print not proposedly scaled

It worked in well on the localhost.

But not working in remote host IIS server.

the error log as follows

Edge

The process cannot access the file because it is being used by another process

IE and Chrome

No printers are installed

Please advise fixed above 2 and 4

Thank You

samsmuthu

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Apr 14, 2021 03:16 AM
on Jan 10, 2022 10:03 AM

Hi samsmuthu,

Refer the updated code.

Make sure there is a default printer configured for the server.

Make sure the identity under which the web application is running has permission to access the printer.

Database

I have made use of the following table Customers with the schema as follows.

I have already inserted few records in the table.

You can download the database table SQL by clicking the download link below.

Download SQL file

HTML

<asp:ScriptManager runat="server" />
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="300" Height="300"></rsweb:ReportViewer>
<br />
<asp:Button Text="Print" runat="server" OnClick="Print" />

Namespaces

C#

using System.Configuration;
using System.Data.SqlClient;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using Microsoft.Reporting.WebForms;

VB.Net

Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.IO
Imports System.Text
Imports Microsoft.Reporting.WebForms

Code

C#

private int pageIndex = 0;
private IList<Stream> streams;
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 5 * from customers");
        ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(datasource);
    }
}

protected void Print(object sender, EventArgs e)
{
    Export(ReportViewer1.LocalReport);
    pageIndex = 0;
    Print();
}

private void Export(LocalReport report)
{
    string deviceInfo =
                        "<DeviceInfo>" +
                        "  <OutputFormat>EMF</OutputFormat>" +
                        "  <PageWidth>8.5in</PageWidth>" +
                        "  <PageHeight>11in</PageHeight>" +
                        "  <MarginTop>0.25in</MarginTop>" +
                        "  <MarginLeft>0.25in</MarginLeft>" +
                        "  <MarginRight>0.25in</MarginRight>" +
                        "  <MarginBottom>0.25in</MarginBottom>" +
                        "</DeviceInfo>";
    Warning[] warnings;
    streams = new List<Stream>();
    report.Render("Image", deviceInfo, CreateStream, out warnings);
    foreach (Stream stream in streams)
    {
        stream.Position = 0;
    }
}

private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
    Stream stream = new FileStream(Server.MapPath("~/Files/") + name + DateTime.Now.ToString("ddMMyyyyHHMMss") + "." + fileNameExtension, FileMode.Create);
    streams.Add(stream);
    return stream;
}

private void Print()
{
    if (streams == null || streams.Count == 0)
    {
        return;
    }
    PrintDocument printDoc = new PrintDocument();
    printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
    printDoc.Print();
}

private void PrintPage(object sender, PrintPageEventArgs ev)
{
    Metafile pageImage = new Metafile(streams[pageIndex]);
    ev.Graphics.DrawImage(pageImage, ev.PageBounds);
    pageIndex++;
    ev.HasMorePages = (pageIndex < streams.Count);
}

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

Private pageIndex As Integer = 0
Private streams As IList(Of Stream)

Protected Sub Page_Load(ByVal sender As Object, ByVal 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 5 * from customers")
        Dim datasource As ReportDataSource = New ReportDataSource("Customers", dsCustomers.Tables(0))
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(datasource)
    End If
End Sub

Protected Sub Print(ByVal sender As Object, ByVal e As EventArgs)
    Export(ReportViewer1.LocalReport)
    pageIndex = 0
    Print()
End Sub

Private Sub Export(ByVal report As LocalReport)
    Dim deviceInfo As String = "<DeviceInfo>" &
                            "  <OutputFormat>EMF</OutputFormat>" &
                            "  <PageWidth>8.5in</PageWidth>" &
                            "  <PageHeight>11in</PageHeight>" &
                            "  <MarginTop>0.25in</MarginTop>" &
                            "  <MarginLeft>0.25in</MarginLeft>" &
                            "  <MarginRight>0.25in</MarginRight>" &
                            "  <MarginBottom>0.25in</MarginBottom>" &
                            "</DeviceInfo>"
    Dim warnings As Warning()
    streams = New List(Of Stream)()
    report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)
    For Each stream As Stream In streams
        stream.Position = 0
    Next
End Sub

Private Function CreateStream(ByVal name As String, ByVal fileNameExtension As String, ByVal encoding As Encoding, ByVal mimeType As String, ByVal willSeek As Boolean) As Stream
    Dim stream As Stream = New FileStream(Server.MapPath("~/Files/") & name & DateTime.Now.ToString("ddMMyyyyHHMMss") & "." & fileNameExtension, FileMode.Create)
    streams.Add(stream)
    Return stream
End Function

Private Sub Print()
    If streams Is Nothing OrElse streams.Count = 0 Then
        Return
    End If

    Dim printDoc As PrintDocument = New PrintDocument()
    AddHandler printDoc.PrintPage, AddressOf PrintPage
    printDoc.Print()
End Sub

Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
    Dim pageImage As Metafile = New Metafile(streams(pageIndex))
    ev.Graphics.DrawImage(pageImage, ev.PageBounds)
    pageIndex += 1
    ev.HasMorePages = (pageIndex < streams.Count)
End Sub

Private Function GetData(ByVal query As String) As Customers
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As SqlCommand = New SqlCommand(query)
    Using con As SqlConnection = New SqlConnection(conString)
        Using sda As SqlDataAdapter = New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dsCustomers As Customers = New Customers()
                sda.Fill(dsCustomers, "DataTable1")
                Return dsCustomers
            End Using
        End Using
    End Using
End Function