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

LVL 2
JassimiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
1
JassimiAuthor Commented:
I used that already but I got the following error:
Object reference on set to an instance of an object
1
CodeCruiserCommented:
Can you show the code which gave that error?
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

JassimiAuthor Commented:
I put this code:
rpt.PrintToPrinter(1, False, 0, 0)
and the error is as mentioned above
 
0
CodeCruiserCommented:
Did you set the printername property?
rpt.PrintOptions.PrinterName = "printerName"
0
JassimiAuthor Commented:
I don't want to define the printer name
I want the user to choose the printer name from print dialog
0
CodeCruiserCommented:
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
mlmccCommented:
Is there a printer available?

mlmcc
0
JassimiAuthor Commented:
of course there is a printer
0
CodeCruiserCommented:
Did you try using the PrintDialog? Or retrieving a list of printers?
0
vivekpv10Commented:
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
JassimiAuthor Commented:
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
vivekpv10Commented:
On which line u getting this error???
0
JassimiAuthor Commented:
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
CodeCruiserCommented:
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
JassimiAuthor Commented:
I designed the report already
and everything is ok with the report
the problem is with print command only
0
CodeCruiserCommented:
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
JassimiAuthor Commented:
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
CodeCruiserCommented:
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
JassimiAuthor Commented:
thats without the sql command,
I mean if I put
rpt.PrintToPrinter(1, False, 0, 0)
without anything more
0
CodeCruiserCommented:
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
JassimiAuthor Commented:
I already set the datasource before put the print cmmand
0
CodeCruiserCommented:
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
JassimiAuthor Commented:
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
CodeCruiserCommented:
>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
CodeCruiserCommented:
Did you decrease the points from 500 to 120? Its not worth all this effort!
0
JassimiAuthor Commented:
I didn't decrease any thing, and u know that I can not do that.. I will increase it now to 200
0
JassimiAuthor Commented:
its 200 now
0
CodeCruiserCommented:
Can you post the full code here?
0
CodeCruiserCommented:
By Full code i mean the code being used to load and print the report; not the full project code.
0
JassimiAuthor Commented:
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
CodeCruiserCommented:
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
JassimiAuthor Commented:
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
JassimiAuthor Commented:
yes... I'm using the same report
0
CodeCruiserCommented:
It does not make sense that same report with same datasource DISPLAYs the correct record but does not PRINT it.
0
JassimiAuthor Commented:
so..
what is the solution
0
CodeCruiserCommented:
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
JassimiAuthor Commented:
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
CodeCruiserCommented:
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
JassimiAuthor Commented:
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
CodeCruiserCommented:
As i said, it is beyond understand why same report would not print any records when printing directly.
0
JassimiAuthor Commented:
now leave this problem..
the main thing is how to print it form the print dialog
0
CodeCruiserCommented:
As suggested previously, retrieve a list of printers using WMI and let the user select the printer.
0
JassimiAuthor Commented:
WMI is not a solution,
if u think this is a solution, please explain it
0
CodeCruiserCommented:
Have you checked the links i provided initially?
0
JassimiAuthor Commented:
I checked them and I didn't find what i want
0
mlmccCommented:
To print through the print dialog try

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

mlmcc
0
JassimiAuthor Commented:
mimcc,

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

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

mlmcc
0
CodeCruiserCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JassimiAuthor Commented:
I didn't get the answer yet
0
JassimiAuthor Commented:
thats not the solution what I want
I don't know how you accept that ,
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB Reporting Tools

From novice to tech pro — start learning today.