Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Crystal Report Direct Print

Crystal Report Direct Print

Scheduled Pinned Locked Moved ASP.NET
comquestion
18 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T thatraja

    |wrote |i am getting error "Object reference not set to an instance of an object." This error always raise when creating instance, so confirm that, but your code looks like right one. Before checking these things make sure if the Dataset ods is not null. |wrote it is not returning me the correct default printer name Because these codes are executing in server so you can't get the client machine printer name. So your server machine's default printer name will be displayed in the textbox1. Confirm that. So you can get DefaultPrinter only on server machine using this code. |wrote direct print using crystal report on clicking of a button on client's line matrix / dot matrix printer use the code which i gave you,

    System.Net.IPHostEntry host;
    host = System.Net.Dns.GetHostEntry(Context.Request.ServerVariables["REMOTE_HOST"]);
    string strSystemName = host.HostName;//You will get client machine name like \\RAJA
    string strPrinterName = "Generic / Text Only";//Mention here your client machine default printer
    rptPrint.PrintOptions.PrinterName = strSystemName + "\\" + strPrinterName;

    The above code must be run with out fail. Actually i gave you my running code which is using for my clients. I'm not using Oracle, otherwise i'll test your code & make full solution to you, actually i'm using SQL 2005. try my steps & then let me know your feedback.

    G Offline
    G Offline
    Girish_Sharma
    wrote on last edited by
    #7

    First let me thank you again. Here 3 issues : 1."Object reference not set to an instance of an object." I am not getting why it is giving me this error... TextBox1.Text = ods.Tables(0).Rows(0).Item("empno") -- No error rptprint.SetDataSource(ods) -- No error 'CrystalReportViewer1.ReportSource = rptprint -- Error I have already placed the crystalreportviewer1 control on the page. 2. Since i am using VB for this page, so i am not able to write the System.Net.IPHostEntry host; code. So, please check the VB conversion is right ? Dim host As New System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(Context.Request.ServerVariables("REMOTE_HOST")) Dim strSystemName As String = host.HostName Dim strPrinterName As String = "Generic / Text Only" rptprint.PrintOptions.PrinterName = strSystemName + "\\" + strPrinterName 3. As far as backend concern, i do'nt think that oracle or sql server is playing any role in this case. I am just using a table and not able to get the error reason. Actually, i am having less experience in dotnet (printing issues). If my client side printing issue (line matrix / dot matrix, because we need thousands of page printing, after data processing) solves, then i think i would be able to convert my all current foxpro based applications into dotnet using oracle (because i am an OCP). Thanks with Kind Regards Girish Sharma

    T 1 Reply Last reply
    0
    • G Girish_Sharma

      First let me thank you again. Here 3 issues : 1."Object reference not set to an instance of an object." I am not getting why it is giving me this error... TextBox1.Text = ods.Tables(0).Rows(0).Item("empno") -- No error rptprint.SetDataSource(ods) -- No error 'CrystalReportViewer1.ReportSource = rptprint -- Error I have already placed the crystalreportviewer1 control on the page. 2. Since i am using VB for this page, so i am not able to write the System.Net.IPHostEntry host; code. So, please check the VB conversion is right ? Dim host As New System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(Context.Request.ServerVariables("REMOTE_HOST")) Dim strSystemName As String = host.HostName Dim strPrinterName As String = "Generic / Text Only" rptprint.PrintOptions.PrinterName = strSystemName + "\\" + strPrinterName 3. As far as backend concern, i do'nt think that oracle or sql server is playing any role in this case. I am just using a table and not able to get the error reason. Actually, i am having less experience in dotnet (printing issues). If my client side printing issue (line matrix / dot matrix, because we need thousands of page printing, after data processing) solves, then i think i would be able to convert my all current foxpro based applications into dotnet using oracle (because i am an OCP). Thanks with Kind Regards Girish Sharma

      T Offline
      T Offline
      thatraja
      wrote on last edited by
      #8

      The 1st one is object instance related issue & then your VB.NET conversion code is right one. Your code looks like right one but something makes trouble. ok let me do something, you just send me the report page, i'll test & will make it run. It should not take more time here after any more. Regards, thatraja thatraja@yahoo.com

      G 1 Reply Last reply
      0
      • T thatraja

        The 1st one is object instance related issue & then your VB.NET conversion code is right one. Your code looks like right one but something makes trouble. ok let me do something, you just send me the report page, i'll test & will make it run. It should not take more time here after any more. Regards, thatraja thatraja@yahoo.com

        G Offline
        G Offline
        Girish_Sharma
        wrote on last edited by
        #9

        Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ocn As New System.Data.OracleClient.OracleConnection Dim oda As New System.Data.OracleClient.OracleDataAdapter("select empno,ename from emp", ocn) Dim ods As New System.Data.DataSet() ocn.ConnectionString = "Data Source=orcl;User ID=scott;Password=pw;Unicode=True" oda.Fill(ods) Dim rptprint As New CrystalDecisions.CrystalReports.Engine.ReportDocument() rptprint.Load(Server.MapPath("Reports").ToString() + "\\CrystalReport.rpt") 'TextBox1.Text = ods.Tables(0).Rows(0).Item("empno") 'This is no error rptprint.SetDataSource(ods) 'This is no error CrystalReportViewer1.ReportSource = rptprint 'This is error of object instantiate. 'CrystalReportViewer1.DataBind() ''rptprint.PrintOptions.PrinterName = DefaultPrinterName() ''rptprint.PrintToPrinter(1, False, 0, 0) 'TextBox1.Text = DefaultPrinterName() rptprint.Close() rptprint.Dispose() ocn.Close() ocn.Dispose() ods.Dispose() oda.Dispose() 'Dim host As New System.Net.IPHostEntry 'host = System.Net.Dns.GetHostEntry(Context.Request.ServerVariables("REMOTE_HOST")) 'Dim strSystemName As String = host.HostName 'Dim strPrinterName As String = "Generic / Text Only" 'rptprint.PrintOptions.PrinterName = strSystemName + "\\" + strPrinterName End Sub Public Shared Function DefaultPrinterName() As String Dim oPS As New System.Drawing.Printing.PrinterSettings Try DefaultPrinterName = oPS.PrinterName Catch ex As System.Exception DefaultPrinterName = "" Finally oPS = Nothing End Try End Function End Class This is what i have in default.aspx.vb file. I have : 1.one crystalreportviewer control 2.one button control 3.one textbox control I have not yet used your code, because this object instance error is frustraing me. Regards Girish Sharma

        T 1 Reply Last reply
        0
        • G Girish_Sharma

          Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ocn As New System.Data.OracleClient.OracleConnection Dim oda As New System.Data.OracleClient.OracleDataAdapter("select empno,ename from emp", ocn) Dim ods As New System.Data.DataSet() ocn.ConnectionString = "Data Source=orcl;User ID=scott;Password=pw;Unicode=True" oda.Fill(ods) Dim rptprint As New CrystalDecisions.CrystalReports.Engine.ReportDocument() rptprint.Load(Server.MapPath("Reports").ToString() + "\\CrystalReport.rpt") 'TextBox1.Text = ods.Tables(0).Rows(0).Item("empno") 'This is no error rptprint.SetDataSource(ods) 'This is no error CrystalReportViewer1.ReportSource = rptprint 'This is error of object instantiate. 'CrystalReportViewer1.DataBind() ''rptprint.PrintOptions.PrinterName = DefaultPrinterName() ''rptprint.PrintToPrinter(1, False, 0, 0) 'TextBox1.Text = DefaultPrinterName() rptprint.Close() rptprint.Dispose() ocn.Close() ocn.Dispose() ods.Dispose() oda.Dispose() 'Dim host As New System.Net.IPHostEntry 'host = System.Net.Dns.GetHostEntry(Context.Request.ServerVariables("REMOTE_HOST")) 'Dim strSystemName As String = host.HostName 'Dim strPrinterName As String = "Generic / Text Only" 'rptprint.PrintOptions.PrinterName = strSystemName + "\\" + strPrinterName End Sub Public Shared Function DefaultPrinterName() As String Dim oPS As New System.Drawing.Printing.PrinterSettings Try DefaultPrinterName = oPS.PrinterName Catch ex As System.Exception DefaultPrinterName = "" Finally oPS = Nothing End Try End Function End Class This is what i have in default.aspx.vb file. I have : 1.one crystalreportviewer control 2.one button control 3.one textbox control I have not yet used your code, because this object instance error is frustraing me. Regards Girish Sharma

          T Offline
          T Offline
          thatraja
          wrote on last edited by
          #10

          Hi, i have checked code, the issue is you are closing & disposing the Report Document object in Button1_Click event which was created there. For that you should create the Report Document object in class & then close & dispose in Page_Unload event. use the code

          Imports CrystalDecisions.Shared
          Imports CrystalDecisions.CrystalReports.Engine
          Imports CrystalDecisions.ReportSource
          Imports CrystalDecisions.Web

          Partial Public Class _Default
          Inherits System.Web.UI.Page

          Dim rptprint As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
          
          Protected Sub Page\_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
              rptprint.Close()
              rptprint.Dispose()
          End Sub
          
          Protected Sub Button1\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
              Dim ocn As New System.Data.SqlClient.SqlConnection
              Dim oda As New System.Data.SqlClient.SqlDataAdapter("select \* from Employees", ocn)
              Dim ods As New System.Data.DataSet()
              ocn.ConnectionString = "Data Source=RAJA;User ID=sa;Password=123;Initial Catalog=Northwind"
              oda.Fill(ods)
          
              rptprint.Load("C:\\CrystalReport1.rpt")
              rptprint.SetDataSource(ods)
          
              ' For DB Login
              Dim crtableLogoninfos As New TableLogOnInfos()
              Dim crtableLogoninfo As New TableLogOnInfo()
              Dim crConnectionInfo As New ConnectionInfo()
              Dim CrTables As Tables
              crConnectionInfo.ServerName = "RAJA"
              crConnectionInfo.DatabaseName = "Northwind"
              crConnectionInfo.UserID = "sa"
              crConnectionInfo.Password = "123"
          
              CrTables = rptprint.Database.Tables
              For Each CrTable As CrystalDecisions.CrystalReports.Engine.Table In CrTables
                  crtableLogoninfo = CrTable.LogOnInfo
                  crtableLogoninfo.ConnectionInfo = crConnectionInfo
                  CrTable.ApplyLogOnInfo(crtableLogoninfo)
              Next
              '/ For DB Login
          
              CrystalReportViewer1.ReportSource = rptprint
              CrystalReportViewer1.DataBind()
          
              ocn.Close()
              ocn.Dispose()
              ods.Dispose()
              oda.Dispose()
          End Sub
          

          End Class

          Here i want to inform you another thing, you can see the property EnableDatabaseLogonPrompt

          G 1 Reply Last reply
          0
          • T thatraja

            Hi, i have checked code, the issue is you are closing & disposing the Report Document object in Button1_Click event which was created there. For that you should create the Report Document object in class & then close & dispose in Page_Unload event. use the code

            Imports CrystalDecisions.Shared
            Imports CrystalDecisions.CrystalReports.Engine
            Imports CrystalDecisions.ReportSource
            Imports CrystalDecisions.Web

            Partial Public Class _Default
            Inherits System.Web.UI.Page

            Dim rptprint As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
            
            Protected Sub Page\_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
                rptprint.Close()
                rptprint.Dispose()
            End Sub
            
            Protected Sub Button1\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
                Dim ocn As New System.Data.SqlClient.SqlConnection
                Dim oda As New System.Data.SqlClient.SqlDataAdapter("select \* from Employees", ocn)
                Dim ods As New System.Data.DataSet()
                ocn.ConnectionString = "Data Source=RAJA;User ID=sa;Password=123;Initial Catalog=Northwind"
                oda.Fill(ods)
            
                rptprint.Load("C:\\CrystalReport1.rpt")
                rptprint.SetDataSource(ods)
            
                ' For DB Login
                Dim crtableLogoninfos As New TableLogOnInfos()
                Dim crtableLogoninfo As New TableLogOnInfo()
                Dim crConnectionInfo As New ConnectionInfo()
                Dim CrTables As Tables
                crConnectionInfo.ServerName = "RAJA"
                crConnectionInfo.DatabaseName = "Northwind"
                crConnectionInfo.UserID = "sa"
                crConnectionInfo.Password = "123"
            
                CrTables = rptprint.Database.Tables
                For Each CrTable As CrystalDecisions.CrystalReports.Engine.Table In CrTables
                    crtableLogoninfo = CrTable.LogOnInfo
                    crtableLogoninfo.ConnectionInfo = crConnectionInfo
                    CrTable.ApplyLogOnInfo(crtableLogoninfo)
                Next
                '/ For DB Login
            
                CrystalReportViewer1.ReportSource = rptprint
                CrystalReportViewer1.DataBind()
            
                ocn.Close()
                ocn.Dispose()
                ods.Dispose()
                oda.Dispose()
            End Sub
            

            End Class

            Here i want to inform you another thing, you can see the property EnableDatabaseLogonPrompt

            G Offline
            G Offline
            Girish_Sharma
            wrote on last edited by
            #11

            Thank you for your replies. Yes now object error has been removed; but now i am getting further error : Dim host As New System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(Context.Request.ServerVariables("REMOTE_HOST")) Dim strSystemName As String = host.HostName Dim strPrinterName As String = "Generic / Text Only" TextBox1.Text = strSystemName 'Here i am getting 127.0.0.1 not \\mymachine name as you said i.e. \\RAJA In my case it should be \\GIRISH (as this machine where i am using) or on another client machine it should be that client machine's name. rptprint.PrintOptions.PrinterName = strSystemName + "\\" + strPrinterName rptprint.PrintToPrinter(1, False, 0, 0) So, i am getting error : (A new error windows pop up) COMException was unhandled by user code Invalid printer specified. Troubleshooting tips: Check the ErrorCode property of the exception to determine the HRESULT returned by the COM object. Get general help for this exception. Regards Girish Sharma

            T 1 Reply Last reply
            0
            • G Girish_Sharma

              Thank you for your replies. Yes now object error has been removed; but now i am getting further error : Dim host As New System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(Context.Request.ServerVariables("REMOTE_HOST")) Dim strSystemName As String = host.HostName Dim strPrinterName As String = "Generic / Text Only" TextBox1.Text = strSystemName 'Here i am getting 127.0.0.1 not \\mymachine name as you said i.e. \\RAJA In my case it should be \\GIRISH (as this machine where i am using) or on another client machine it should be that client machine's name. rptprint.PrintOptions.PrinterName = strSystemName + "\\" + strPrinterName rptprint.PrintToPrinter(1, False, 0, 0) So, i am getting error : (A new error windows pop up) COMException was unhandled by user code Invalid printer specified. Troubleshooting tips: Check the ErrorCode property of the exception to determine the HRESULT returned by the COM object. Get general help for this exception. Regards Girish Sharma

              T Offline
              T Offline
              thatraja
              wrote on last edited by
              #12

              I'm sorry i'm not well since last week with heavy fever that's i can't able to reply quickly. As per my coding, you will be get IP address instead of System name, i mentioned "\\RAJA" for just easy understand. Please mention which line error occurs? ok.

              1 Reply Last reply
              0
              • T thatraja

                Hi, Please change the System & Printer names based on yours. code is below

                ReportDocument rptPrint = new ReportDocument();
                rptPrint.Load(Server.MapPath("Reports").ToString() + "\\rptSales.rpt");
                //Report displaying in Screen
                DataSet dsData = (DataSet)Session["EmpData"];
                rptPrint.SetDataSource(dsData);
                CrystalReportViewer1.ReportSource = rptPrint;
                CrystalReportViewer1.DataBind();
                //Report directly printing by Printer
                string strSystemName = "\\\\RAJA\\";
                string strPrinterName = "SAMSUNG";
                rptPrint.PrintOptions.PrinterName = strSystemName + strPrinterName;
                rptPrint.PrintToPrinter(1, false, 0, 0);
                rptPrint.Close();
                rptPrint.Dispose();

                Please let me know your feedback because i was posted a wrong link as answer for a question today in Quick Answers section. Regards, thatraja

                S Offline
                S Offline
                shrikant kudlur
                wrote on last edited by
                #13

                i dont want to hard code the printer name. if i do so then it will print the report to same printer every time. in my office there are 20 comp and each comp has printer connected to it. i want to print to a printer which is connected to particular client. thanks,

                T 1 Reply Last reply
                0
                • S shrikant kudlur

                  i dont want to hard code the printer name. if i do so then it will print the report to same printer every time. in my office there are 20 comp and each comp has printer connected to it. i want to print to a printer which is connected to particular client. thanks,

                  T Offline
                  T Offline
                  thatraja
                  wrote on last edited by
                  #14

                  shrikant.kudlur wrote:

                  i dont want to hard code the printer name.

                  It's just an example snippet. You can set default printer if you want. So you should change the code based on your need. That's all. You can find code for "Get/Set Default printer" in Google. Cheers.

                  thatraja


                  **My Tip/Tricks
                  My Dad had a Heart Attack on this day so don't...
                  **

                  S 1 Reply Last reply
                  0
                  • T thatraja

                    shrikant.kudlur wrote:

                    i dont want to hard code the printer name.

                    It's just an example snippet. You can set default printer if you want. So you should change the code based on your need. That's all. You can find code for "Get/Set Default printer" in Google. Cheers.

                    thatraja


                    **My Tip/Tricks
                    My Dad had a Heart Attack on this day so don't...
                    **

                    S Offline
                    S Offline
                    shrikant kudlur
                    wrote on last edited by
                    #15

                    Here is my code ReportDocument doc = new ReportDocument(); doc.Load(Server.MapPath("crystalreport.rpt")); System.Net.IPHostEntry host; host = System.Net.Dns.GetHostEntry(Context.Request.ServerVariables["REMOTE_HOST"]); string strSystemName = host.HostName; string strPrinterName = "Generic / Text Only"; doc.PrintOptions.PrinterName=strSystemName + "\\" + strPrinterName; doc.PrintToPrinter(1,true,1,1); i m getting this error: System.Runtime.InteropServices.COMException: Invalid printer specified at this line: doc.PrintOptions.PrinterName=strSystemName + "\\" + strPrinterName; i m geting the name of the system currect but i think i mad simple mistake in assigning printer name plz help me thanks in advance

                    T 1 Reply Last reply
                    0
                    • S shrikant kudlur

                      Here is my code ReportDocument doc = new ReportDocument(); doc.Load(Server.MapPath("crystalreport.rpt")); System.Net.IPHostEntry host; host = System.Net.Dns.GetHostEntry(Context.Request.ServerVariables["REMOTE_HOST"]); string strSystemName = host.HostName; string strPrinterName = "Generic / Text Only"; doc.PrintOptions.PrinterName=strSystemName + "\\" + strPrinterName; doc.PrintToPrinter(1,true,1,1); i m getting this error: System.Runtime.InteropServices.COMException: Invalid printer specified at this line: doc.PrintOptions.PrinterName=strSystemName + "\\" + strPrinterName; i m geting the name of the system currect but i think i mad simple mistake in assigning printer name plz help me thanks in advance

                      T Offline
                      T Offline
                      thatraja
                      wrote on last edited by
                      #16

                      This link will help you to resolve the issue. Clickety[^]

                      thatraja


                      **My Tip/Tricks
                      My Dad had a Heart Attack on this day so don't...
                      **

                      S 1 Reply Last reply
                      0
                      • T thatraja

                        This link will help you to resolve the issue. Clickety[^]

                        thatraja


                        **My Tip/Tricks
                        My Dad had a Heart Attack on this day so don't...
                        **

                        S Offline
                        S Offline
                        shrikant kudlur
                        wrote on last edited by
                        #17

                        link is not working...

                        T 1 Reply Last reply
                        0
                        • S shrikant kudlur

                          link is not working...

                          T Offline
                          T Offline
                          thatraja
                          wrote on last edited by
                          #18

                          But It's still working for me. Please try again. Try in different browsers.

                          thatraja


                          **My Tip/Tricks
                          My Dad had a Heart Attack on this day so don't...
                          **

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups