Question

Print Crystal report direct without preview in vb.net

Asked by: Jassimi

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

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-07-16 at 02:04:59ID24574990
Topics

Microsoft Visual Basic.Net

,

Crystal Reports Software

,

Business Objects

Participating Experts
3
Points
200
Comments
55

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Crystal Reports
    Hi, I have a crystal report(crStatement) and a dataset(dsNumber): Call CreateXMLFile("c:\commercial\statement.ds", dsNumber) Dim myStatement As New crStatement() myStatement.SetDataSource(dsNumber) Problem: I would like to populate a textbox of the report from wit...
  2. crystal reports
    I am trying to get the avg grade in a winform using vb.net and crystal reports. Below is the code that I have tried that does not work. How do I find the avg of 3 rows in a crystal report? {student.grade1} + {student.grade2} + {student.grade3} / 3 this code does not avg th...
  3. VB.Net  Crystal Reports 8.5  Date Range Problem in Sel…
    I had the following selection formula written in VB.Net for a Crystal Report 8.5 . The fields in Table1 called StartDate and EndDate are of DataType ...Date/Time 2 textboxes are on a form txt_Start & txt.End to hold the date range when entered How would this Selection ...
  4. Crystal Reports and VB.NET
    Hi, i have a VB.NET form contains EmpNo, EmpName and EmpSalary ... and a command button called print me .. and i have a crystal report called Emps and it has a parameter field called Eno .. now what i need is when the user clicks over the Print me command the report will be d...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: CodeCruiserPosted on 2009-07-16 at 02:16:23ID: 24867607

 

by: JassimiPosted on 2009-07-16 at 02:55:41ID: 24867858

I used that already but I got the following error:
Object reference on set to an instance of an object

 

by: CodeCruiserPosted on 2009-07-16 at 03:03:38ID: 24867914

Can you show the code which gave that error?

 

by: JassimiPosted on 2009-07-16 at 03:13:22ID: 24867962

I put this code:
rpt.PrintToPrinter(1, False, 0, 0)
and the error is as mentioned above
 

 

by: CodeCruiserPosted on 2009-07-16 at 03:27:38ID: 24868033

Did you set the printername property?
rpt.PrintOptions.PrinterName = "printerName"

 

by: JassimiPosted on 2009-07-16 at 03:32:21ID: 24868056

I don't want to define the printer name
I want the user to choose the printer name from print dialog

 

by: CodeCruiserPosted on 2009-07-16 at 03:45:29ID: 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

 

by: mlmccPosted on 2009-07-16 at 09:21:56ID: 24870803

Is there a printer available?

mlmcc

 

by: JassimiPosted on 2009-07-16 at 12:23:16ID: 24872793

of course there is a printer

 

by: CodeCruiserPosted on 2009-07-17 at 01:03:05ID: 24876548

Did you try using the PrintDialog? Or retrieving a list of printers?

 

by: vivekpv10Posted on 2009-07-17 at 02:17:36ID: 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)

                                              
1:
2:
3:
4:
5:

Select allOpen in new window

 

by: JassimiPosted on 2009-07-19 at 01:09:35ID: 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

 

by: vivekpv10Posted on 2009-07-19 at 23:06:11ID: 24892672

On which line u getting this error???

 

by: JassimiPosted on 2009-07-21 at 01:50:25ID: 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

 

by: CodeCruiserPosted on 2009-07-21 at 02:34:12ID: 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.

 

by: JassimiPosted on 2009-07-21 at 03:34:19ID: 24902860

I designed the report already
and everything is ok with the report
the problem is with print command only

 

by: CodeCruiserPosted on 2009-07-21 at 03:38:24ID: 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?

 

by: JassimiPosted on 2009-07-21 at 03:49:15ID: 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

 

by: CodeCruiserPosted on 2009-07-21 at 03:52:56ID: 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?

 

by: JassimiPosted on 2009-07-21 at 03:57:17ID: 24902997

thats without the sql command,
I mean if I put
rpt.PrintToPrinter(1, False, 0, 0)
without anything more

 

by: CodeCruiserPosted on 2009-07-21 at 03:58:38ID: 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.

 

by: JassimiPosted on 2009-07-21 at 04:09:50ID: 24903075

I already set the datasource before put the print cmmand

 

by: CodeCruiserPosted on 2009-07-21 at 04:12:33ID: 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?

 

by: JassimiPosted on 2009-07-21 at 04:21:47ID: 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.

 

by: CodeCruiserPosted on 2009-07-21 at 04:23:47ID: 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.

 

by: CodeCruiserPosted on 2009-07-21 at 04:24:20ID: 24903175

Did you decrease the points from 500 to 120? Its not worth all this effort!

 

by: JassimiPosted on 2009-07-21 at 04:30:18ID: 24903221

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

 

by: JassimiPosted on 2009-07-21 at 04:31:14ID: 24903229

its 200 now

 

by: CodeCruiserPosted on 2009-07-21 at 04:36:11ID: 24903277

Can you post the full code here?

 

by: CodeCruiserPosted on 2009-07-21 at 04:36:35ID: 24903283

By Full code i mean the code being used to load and print the report; not the full project code.

 

by: JassimiPosted on 2009-07-21 at 04:37:11ID: 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

 

by: CodeCruiserPosted on 2009-07-21 at 04:39:47ID: 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?

 

by: JassimiPosted on 2009-07-21 at 04:41:00ID: 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

 

by: JassimiPosted on 2009-07-21 at 04:42:23ID: 24903331

yes... I'm using the same report

 

by: CodeCruiserPosted on 2009-07-21 at 05:20:57ID: 24903673

It does not make sense that same report with same datasource DISPLAYs the correct record but does not PRINT it.

 

by: JassimiPosted on 2009-07-21 at 05:25:04ID: 24903709

so..
what is the solution

 

by: CodeCruiserPosted on 2009-07-21 at 05:29:28ID: 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.

 

by: JassimiPosted on 2009-07-21 at 05:35:49ID: 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

 

by: CodeCruiserPosted on 2009-07-21 at 05:39:50ID: 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?

 

by: JassimiPosted on 2009-07-21 at 06:42:15ID: 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

 

by: CodeCruiserPosted on 2009-07-21 at 06:50:47ID: 24904486

As i said, it is beyond understand why same report would not print any records when printing directly.

 

by: JassimiPosted on 2009-07-21 at 11:10:23ID: 24907566

now leave this problem..
the main thing is how to print it form the print dialog

 

by: CodeCruiserPosted on 2009-07-22 at 00:41:28ID: 24912254

As suggested previously, retrieve a list of printers using WMI and let the user select the printer.

 

by: JassimiPosted on 2009-07-22 at 03:07:24ID: 24912974

WMI is not a solution,
if u think this is a solution, please explain it

 

by: CodeCruiserPosted on 2009-07-22 at 03:07:59ID: 24912977

Have you checked the links i provided initially?

 

by: JassimiPosted on 2009-07-22 at 04:42:45ID: 24913552

I checked them and I didn't find what i want

 

by: mlmccPosted on 2009-07-22 at 10:17:42ID: 24917378

To print through the print dialog try

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

mlmcc

 

by: JassimiPosted on 2009-07-22 at 23:03:20ID: 24922200

mimcc,

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

 

by: mlmccPosted on 2009-07-23 at 18:45:34ID: 24931798

Try using the PrintReport method on the viewer object.

mlmcc

 

by: mlmccPosted on 2009-07-23 at 18:46:06ID: 24931799

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

mlmcc

 

by: CodeCruiserPosted on 2009-07-24 at 01:00:37ID: 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.

 

by: JassimiPosted on 2009-09-02 at 23:15:02ID: 25248209

I didn't get the answer yet

 

by: JassimiPosted on 2009-09-06 at 02:56:43ID: 25269316

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

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...