Link to home
Start Free TrialLog in
Avatar of Jassimi
JassimiFlag for Bahrain

asked on

Print Crystal report direct without preview in vb.net

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

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of Jassimi

ASKER

I used that already but I got the following error:
Object reference on set to an instance of an object
Can you show the code which gave that error?
Avatar of Jassimi

ASKER

I put this code:
rpt.PrintToPrinter(1, False, 0, 0)
and the error is as mentioned above
 
Did you set the printername property?
rpt.PrintOptions.PrinterName = "printerName"
Avatar of Jassimi

ASKER

I don't want to define the printer name
I want the user to choose the printer name from print dialog
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
Avatar of Mike McCracken
Mike McCracken

Is there a printer available?

mlmcc
Avatar of Jassimi

ASKER

of course there is a printer
Did you try using the PrintDialog? Or retrieving a list of printers?
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

Avatar of Jassimi

ASKER

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
On which line u getting this error???
Avatar of Jassimi

ASKER

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
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.
Avatar of Jassimi

ASKER

I designed the report already
and everything is ok with the report
the problem is with print command only
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?
Avatar of Jassimi

ASKER

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
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?
Avatar of Jassimi

ASKER

thats without the sql command,
I mean if I put
rpt.PrintToPrinter(1, False, 0, 0)
without anything more
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.
Avatar of Jassimi

ASKER

I already set the datasource before put the print cmmand
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?
Avatar of Jassimi

ASKER

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.
>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.
Did you decrease the points from 500 to 120? Its not worth all this effort!
Avatar of Jassimi

ASKER

I didn't decrease any thing, and u know that I can not do that.. I will increase it now to 200
Avatar of Jassimi

ASKER

its 200 now
Can you post the full code here?
By Full code i mean the code being used to load and print the report; not the full project code.
Avatar of Jassimi

ASKER

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
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?
Avatar of Jassimi

ASKER

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
Avatar of Jassimi

ASKER

yes... I'm using the same report
It does not make sense that same report with same datasource DISPLAYs the correct record but does not PRINT it.
Avatar of Jassimi

ASKER

so..
what is the solution
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.
Avatar of Jassimi

ASKER

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
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?
Avatar of Jassimi

ASKER

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
As i said, it is beyond understand why same report would not print any records when printing directly.
Avatar of Jassimi

ASKER

now leave this problem..
the main thing is how to print it form the print dialog
As suggested previously, retrieve a list of printers using WMI and let the user select the printer.
Avatar of Jassimi

ASKER

WMI is not a solution,
if u think this is a solution, please explain it
Have you checked the links i provided initially?
Avatar of Jassimi

ASKER

I checked them and I didn't find what i want
To print through the print dialog try

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

mlmcc
Avatar of Jassimi

ASKER

mimcc,

I tried this also, it's print direct to the default printer without showing the print dialog
Try using the PrintReport method on the viewer object.

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

mlmcc
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jassimi

ASKER

I didn't get the answer yet
Avatar of Jassimi

ASKER

thats not the solution what I want
I don't know how you accept that ,