Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I had created a web based asp.net project on visual studio 2010 ultimate.
I have crystal reports in my project but I have not used reportviewer to view it.

On clicking on print button, the print dialog box should appear and reports will be printed.

The code that i had written worked fine in my ASP.NET application inside visual studio 2010.

but as soon as i deployed it and uploaded it in server, it showed a error that

Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.


I searched the error in google and came to know that windows dialog box wont work in server.

I am pasting my print code here, can anyone please help me what code to be replaced so that on clicking on print button, print dialog box appears on the server and my reports gets printed.

Kindly please help as its a need.

I always prefer codeproject.com. Here experts are very helpful. I dont have much idea about javascript code or other.

C#
protected void btnPrint_Click(object sender, EventArgs e)
    {
        //  ReportDocument rDoc = new ReportDocument();
        //rDoc.Load("C:/Users/SUDESHNA/Documents/Visual Studio 2010/Projects/Inventory/SoldCrystalReport.rpt");
        //rDoc.SetParameterValue("@Category", ddlCategory.SelectedValue);
        //rDoc.SetDatabaseLogon("sa", "gariahat");
        //this.crystalReportViewer.ReportSource = rDoc;
        //this.crystalReportViewer.DataBind();
        //this.crystalReportViewer.Focus();
        //rDoc.Refresh();
        //rDoc.PrintToPrinter(1, true, 0, 0);

        ReportDocument rDoc = new ReportDocument();
        PrintDialog dialog1 = new PrintDialog();
        rDoc.Load(Server.MapPath("SoldCrystalReport.rpt"));
        rDoc.SetParameterValue("@Category", ddlCategory.SelectedValue);
        rDoc.SetDatabaseLogon("sa", "gariahat");
        dialog1.AllowSomePages = true;
        dialog1.AllowPrintToFile = false;
        if (dialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            int copies = dialog1.PrinterSettings.Copies;
            int fromPage = dialog1.PrinterSettings.FromPage;
            int toPage = dialog1.PrinterSettings.ToPage;
            bool collate = dialog1.PrinterSettings.Collate;

            rDoc.PrintOptions.PrinterName = dialog1.PrinterSettings.PrinterName;
            rDoc.PrintToPrinter(copies, collate, fromPage, toPage);
        }
        rDoc.Dispose();
        dialog1.Dispose();


Thanks in advance
Please kindly help
Posted

1 solution

VB
Protected Sub printDiv()
        Dim key As String = "script3"
        Dim cstype As Type = Me.GetType()
        Dim script As String = "<script type=""text/javascript"">" & vbCrLf _
             + "function printDivnew(divID) {" & vbCrLf _
             + "//Get the HTML of div" & vbCrLf _
             + "var divElements = document.getElementById(divID).innerHTML;" & vbCrLf _
             + "//Get the HTML of whole page" & vbCrLf _
             + "var oldPage = document.body.innerHTML;" & vbCrLf _
             + "//Reset the page's HTML with div's HTML only" & vbCrLf _
             + "document.body.innerHTML =" & vbCrLf _
             + """<html><head><title></title></head><body>""" & vbCrLf _
             + "divElements + ""</body>"";" & vbCrLf _
             + "//Print Page" & vbCrLf _
             + "window.print();" & vbCrLf _
             + "//Restore orignal HTML" & vbCrLf _
             + "document.body.innerHTML = oldPage;" & vbCrLf _
             + "}" & vbCrLf _
             + "</script>"
        'ClientScript.RegisterStartupScript(cstype, key, script)
        Me.ClientScript.RegisterStartupScript(cstype, key, script)
    End Sub
 
Share this answer
 
Comments
sudeshna from bangkok 20-May-15 3:46am    
Where shall i paste this code? Do i have to make any changes in the code? Where will i put my crystal report name?
Sid_Joshi 20-May-15 3:49am    
use this to convert crystal report in html text in a div.
Reportdoc.Value = Server.MapPath("") & "\RPT\PrintClientBrokerageSum.rpt"
Dim orpt As New CrystalDecisions.CrystalReports.Engine.ReportDocument
'Dim reporturl As String = ""
'reporturl = UCase(Mid(rEPORTfILE, rEPORTfILE.LastIndexOf("\") + 2, rEPORTfILE.Length).ToUpper())
orpt.Load(Reportdoc.Value)
orpt.SetDataSource(DtMain)
Dim ReportStream As System.IO.Stream = orpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.HTML40)
Dim bytes(ReportStream.Length) As Byte
ReportStream.Position = 0
ReportStream.Read(bytes, 0, ReportStream.Length)
orpt.Dispose()
DivReportVIew.InnerHtml = Encoding.UTF8.GetString(bytes)
Erase bytes
ReportStream.Flush()
ReportStream.Dispose()
and replace divID with DivReportVIew at above code
sudeshna from bangkok 20-May-15 3:58am    
Sorry to say again. I am new to these. How can I use this code to convert my crystal report to html in a div?
Sid_Joshi 20-May-15 4:04am    
<div id="DivReportVIew" runat="server">
</div>
add this to your aspx code
the above code will convert crystal report in html text and populates in DivReportVIew div and first code will print div contained html text.
sudeshna from bangkok 20-May-15 4:13am    
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="SoldItems.aspx.cs" Inherits="SoldItems" %>
<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
Namespace="CrystalDecisions.Web" TagPrefix="crystalReport" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">
<div id="DivReportVIew" runat="server">
</div>
<div class="content">


View Sold Items


<div class="commonFilters">
<div>
<p>
<asp:Label ID="lblCategory" runat="server">Choose By Category :</p>
<asp:DropDownList ID="ddlCategory" runat="server">
<asp:ListItem >Choose Category
<asp:ListItem>Earrings
<asp:ListItem>Pendant
<asp:ListItem>Bangles
<asp:ListItem>Bracelets
<asp:ListItem>Necklace
<asp:ListItem>Toe Rings
<asp:ListItem>Cuff Links
<asp:ListItem>Rings
<asp:ListItem>Sets
<asp:ListItem>Tie-Pin
<asp:ListItem>Brooch

</div>
<div>
<asp:Button ID="btnShow" runat="server" Text="Show" OnClick="btnShow_Click" />
</div>
<div>
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" />
</div>
</div>
<div class="searchResults">
<asp:Label ID="lblMessageInfo" runat="server" Font-Bold="true" Font-Size="14px" ForeColor="Red"
Visible="false">
<asp:GridView ID="gvJewellery" runat="server" AllowPaging="false" AutoGenerateColumns="false"
Width="960px" OnRowDataBound="gvJewellery_RowDataBound">
<HeaderStyle BackColor="#207ce5" Height="75px" VerticalAlign="Middle" HorizontalAlign="Center"
Font-Bold="true" />
<columns>
<asp:BoundField DataField="SerialNumber" HeaderText="Serial Number" ItemStyle-Width="75px"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Category" HeaderText="Category" ItemStyle-Width="120px"
ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderText="Image" ItemStyle-Width="120px" ItemStyle-HorizontalAlign="Center">
<itemtemplate>
<asp:Image ID="imgJewellery" runat="server" Height="50px" Width="60px" />


<asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-Width="385px"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="CostPrice" HeaderText="Cost Price" ItemStyle-Width="70px"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="SellingPrice" HeaderText="Selling Price" ItemStyle-Width="70px"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="SoldPrice" HeaderText="Sold Price" ItemStyle-Width="70px"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Custo

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900