Solved

Print Crystal report direct without preview in vb.net

Posted on 2009-07-16
55
8,831 Views
Last Modified: 2013-11-15
Hi..
I have a crystal report (crsSTUD) which designed depends on the database,
and I have a form (frmSTUDENT) contains on some textboxes which taking its data from the database table (STUDENT) , and print button,
what I want is when the (txtS_ID) Is filled by student ID and the press print button
to print the crystal report, the print dialog will show direct without showing the crystal report,

I'm using the following code but I couldn't complete it
Dim rpt As New crsSTUD

        Try

            CMD.CommandType = CommandType.Text

            CMD.CommandText = "SELECT * FROM STUDENT WHERE S_ID = '" + Me.txtS_ID.Text + "'"

            adp.SelectCommand = CMD

            CMD.Connection = CON

            ds.Clear()

            adp.Fill(ds, "STUDENT")

            rpt.SetDataSource(ds)
 
 

           
 

        Catch

            'Display error message, if any

            UnhandledExceptionHandler()

        End Try

Open in new window

0
Comment
Question by:Jassimi
  • 25
  • 23
  • 4
  • +1
55 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24867607
1
 
LVL 2

Author Comment

by:Jassimi
ID: 24867858
I used that already but I got the following error:
Object reference on set to an instance of an object
1
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24867914
Can you show the code which gave that error?
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24867962
I put this code:
rpt.PrintToPrinter(1, False, 0, 0)
and the error is as mentioned above
 
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24868033
Did you set the printername property?
rpt.PrintOptions.PrinterName = "printerName"
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24868056
I don't want to define the printer name
I want the user to choose the printer name from print dialog
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24868131
I dont think that is possible. You can either display the report and let the user print or you can directly print to a printer without any user interaction. One option is to use the PrintDialog to let user select a printer

http://msdn.microsoft.com/en-us/library/system.windows.controls.printdialog.aspx

If that does not help, you can use WMI to retrieve a list of all installed printers and then show a form to let the user select the printer.

http://www.dotnetcurry.com/ShowArticle.aspx?ID=148&AspxAutoDetectCookieSupport=1
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 24870803
Is there a printer available?

mlmcc
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24872793
of course there is a printer
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24876548
Did you try using the PrintDialog? Or retrieving a list of printers?
0
 
LVL 5

Expert Comment

by:vivekpv10
ID: 24877004
I will show u an example..Here objrpt represents a crystal report object....
Dim docprint As System.Drawing.Printing.PrintDocument

docprint= New System.Drawing.Printing.PrintDocument

Dim DftPrinter As String = docprint.PrinterSettings.PrinterName

objrpt.PrintOptions.PrinterName = DftPrinter

objrpt.PrintToPrinter(1, False, 0, 0)

Open in new window

0
 
LVL 2

Author Comment

by:Jassimi
ID: 24888621
vivekpv10:
I used your method and still getting same error which I mentioned above:
Object reference on set to an instance of an object

CodeCruiser:
I'm trying to use PrintDialog
0
 
LVL 5

Expert Comment

by:vivekpv10
ID: 24892672
On which line u getting this error???
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24902254
I solve that error now .. it was from adp variable,
now the print problem..
I want to display the print dialog to choose the printer and print the specific record as I mentioned in my question..
I mean.. when (txtS_ID) is filled with ID, and press print .. it will print only the record that has the same ID..

if I put
rpt.PrintToPrinter(1, False, 0, 0)
it will print the whole records and without showing the print dialog
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24902513
To print the report for the given ID only, you need to redesign the report so that it accepts an ID as parameter and displays the record for that ID only.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24902522
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24902860
I designed the report already
and everything is ok with the report
the problem is with print command only
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24902884
Did you use parameters in the report? In the last comment you said you want to print the report for the given ID only. Do you know how to programmatically pass parameters?
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24902940
if u take a look to my question u will see that I used the sql statement to display the given ID,
I just want to put the print command now to put it after sql command
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24902967
You said
"if I put
rpt.PrintToPrinter(1, False, 0, 0)
it will print the whole records and without showing the print dialog"

Did you manage to solve the original problem of printing?
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24902997
thats without the sql command,
I mean if I put
rpt.PrintToPrinter(1, False, 0, 0)
without anything more
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24903003
You have to set the datasource for the report before you can print it. Do everything else other than showing it and instead of showing, use the printing.
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24903075
I already set the datasource before put the print cmmand
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24903097
Then it should print that record only. Are you sure you use the same code for populating the report for printing which you use for displaying it?
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24903157
if I want to show the report first, I'm using CrystalReportViewer, and from there I can print it from print tool,
but here I don't want to show it,
yes I'm using the same code, but I removed the CrystalReportViewer command from it.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24903169
>yes I'm using the same code, but I removed the CrystalReportViewer command from it.
It does not make any sense then why it would print all the records. Please post the full code here.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24903175
Did you decrease the points from 500 to 120? Its not worth all this effort!
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Author Comment

by:Jassimi
ID: 24903221
I didn't decrease any thing, and u know that I can not do that.. I will increase it now to 200
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24903229
its 200 now
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24903277
Can you post the full code here?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24903283
By Full code i mean the code being used to load and print the report; not the full project code.
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24903288
see..
it will print all records if I remove the sql command only
put if I put the sql command it will print the report without any data
although there is a data in the database with this ID and its showing when I make search
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24903305
That's what i said in the previous comment and you said you are using that datasource. Now you say if you use that then nothing is printed! Are you using the same report for showing and printing?
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24903318
this is the code:

Dim adp As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim rpt As New crsSTUD
        Try
            CMD.CommandType = CommandType.Text
            CMD.CommandText = "SELECT * FROM STUDENT WHERE S_ID = '" + Me.txtS_ID.Text + "'"
            adp.SelectCommand = CMD
            CMD.Connection = CON
            ds.Clear()
            adp.Fill(ds, "STUDENT")
            rpt.SetDataSource(ds)
            rpt.PrintToPrinter(1, False, 0, 0)
 
           
 
        Catch
            'Display error message, if any
            UnhandledExceptionHandler()
        End Try

and as I said before ... I want to print it through the print dialog to choose the printer first
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24903331
yes... I'm using the same report
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24903673
It does not make sense that same report with same datasource DISPLAYs the correct record but does not PRINT it.
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24903709
so..
what is the solution
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24903749
Solution is to configure the report with the datasource and then use parameters to display particular records based on ID values supplied. I posted plenty links about that in a previous comment.
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24903790
I already configure th report dependson the datasourse,
and if I used that code.. it should print the data..

see..
if I use the CrystalReportViewer, the data will display normally without any problem
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24903821
That is what i am saying. The crystal report viewer is just displaying the data. The same report should print ok if it is displayed ok. If you use the print button in the viewer, does it print correctly?
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24904394
as I said,
in CrystalReportViewer there is a print tool  in the tool bar of the viewer..
if I click on this print tool, it will print the report correctly without any problem
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24904486
As i said, it is beyond understand why same report would not print any records when printing directly.
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24907566
now leave this problem..
the main thing is how to print it form the print dialog
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24912254
As suggested previously, retrieve a list of printers using WMI and let the user select the printer.
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24912974
WMI is not a solution,
if u think this is a solution, please explain it
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24912977
Have you checked the links i provided initially?
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24913552
I checked them and I didn't find what i want
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 24917378
To print through the print dialog try

rpt.PrintToPrinter(1, TRUE, 0, 0)

mlmcc
0
 
LVL 2

Author Comment

by:Jassimi
ID: 24922200
mimcc,

I tried this also, it's print direct to the default printer without showing the print dialog
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 24931798
Try using the PrintReport method on the viewer object.

mlmcc
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 24931799
I just looked at the PrintToPrinter help and it prints directly to the printer with no option to display a dialog.

mlmcc
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 200 total points
ID: 24932892
As i suggested previously, there are three options you have

1) Let the user print from the report viewer using the print button.
2) Print to the default printer using the PrintToPrinter without user interaction.
3) Retrieve(using WMI for example) and display a list of printers to user allowing them to choose a printer   and then print to that printer.
0
 
LVL 2

Author Comment

by:Jassimi
ID: 25248209
I didn't get the answer yet
0
 
LVL 2

Author Comment

by:Jassimi
ID: 25269316
thats not the solution what I want
I don't know how you accept that ,
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now