Call Crystal report from VB.Net
  • Hello All,

    I've a crystal report with 8 subreports in it. I need to print this crystal report from VB.Net or WPF application without showing preview of it to the user. Can somebody please give me a hand on this? I'm stuck with this for too long. Please help guys. Thanks.

    Regards
  • 11 Comments sorted by
  • Hi @VBLearner Welcome to forum,

    Have you tried this link code sample and attached source code try it.

  • Hi Waqas,

    I appreciate your response. Your link was very helpful in understanding. My crystal report is a part of a third party application. I'm trying to call the crystal report from my vb.net application. However, I'm having hard time figuring out the SQL query to call the report. Any help on this will be very very helpful for me. Thanks.
  • Vote Up0Vote Down WaqasWaqas
    172.00 Karma Accepted Answer
    hi i didn't get you what you want further. Do you want to ask how to assign data source to crystal report through coding?
  • Waqas,

    I've assigned a datasource to the crystalreport and also verifed the datasource for the report in visual studio. I still get error message Failed to open the connection. How can I fix this issue? I've attached the screenshot of the error message.
    CRError.png
    954 x 386 - 61K
  • its seems connectivity problem you have assign proper database connection try below code.

    rptDoc.Load("YourReportFilePath")
    rptDoc.SetDatabaseLogon("username", "password", "host", "databasename");
    CrystalReportViewer1.ReportSource = rptDoc
    CrystalReportViewer1.Refresh()

    if code will not work so kindly share your peace of code.
  • Hi Waqas, 

    I've already included the code you suggested in my code and still I get the same error. Below is my piece of code:

    Thanks

    Imports CrystalDecisions.CrystalReports.Engine
    Imports System.Data.SqlClient
    Imports CrystalDecisions.Shared

    Public Class Form2

        Private Sub CrystalReportViewer1_Load(sender As System.Object, e As System.EventArgs) Handles CrystalReportViewer1.Load
            Try
                strsql = "Select * from InvoiceHeader H join InvoiceDetailsPrn D on D.sInvnumber=H.Invnumber where invnumber='703053'"

                Dim sqlda As New SqlDataAdapter(strsql, sqlconn)
                sqlda.Fill(sqlds, "table1")
                strreportname = "Report1"
                Dim strreportpath As String = Application.StartupPath & "\" & strreportname & ".rpt"
                If Not IO.File.Exists(strreportpath) Then
                    Throw (New Exception("unable to locate file : " & vbCrLf & strreportpath))
                End If
                Dim rptdocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
                rptdocument.Load(strreportpath)
                rptdocument.SetDatabaseLogon("sa", "test123", "laptop-10\localAV01", "AV01")
                
                rptdocument.SetDataSource(sqlds.Tables(0))
                CrystalReportViewer1.ReportSource = rptdocument
                CrystalReportViewer1.Refresh()

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    End Class
  • hi @VBLearner

    i suggest you if you are assigning Report.setDataSource Property so dont use SetDatabaseLogon property and simply fill datatable and assign it on SetDataSource Property get idea from my below c# code


    System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
    con.ConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=dbProduct;Integrated Security=True";
    System.Data.SqlClient.SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter("select * from tblProduct", con);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    CrystalReport1 rpt = new CrystalReport1();
    rpt.SetDataSource(dt);
    crystalReportViewer1.ReportSource = rpt; 


  • Waqas, Thanks. Can you please help me integrate your above (c# )code in my original code in VB?. I'm new to VB. Thanks again. Below is my code:

    Imports CrystalDecisions.CrystalReports.Engine
    Imports System.Data.SqlClient
    Imports CrystalDecisions.Shared

    Public Class Form2

        Private Sub CrystalReportViewer1_Load(sender As System.Object, e As System.EventArgs) Handles CrystalReportViewer1.Load
            Try
                strsql = "Select * from InvoiceHeader H join InvoiceDetailsPrn D on D.sInvnumber=H.Invnumber where invnumber='703053'"

                Dim sqlda As New SqlDataAdapter(strsql, sqlconn)
                sqlda.Fill(sqlds, "table1")
                strreportname = "Report1"
                Dim strreportpath As String = Application.StartupPath & "\" & strreportname & ".rpt"
                If Not IO.File.Exists(strreportpath) Then
                    Throw (New Exception("unable to locate file : " & vbCrLf & strreportpath))
                End If
                Dim rptdocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
                rptdocument.Load(strreportpath)
                rptdocument.SetDatabaseLogon("sa", "test123", "laptop-10\localAV01", "AV01")
                
                rptdocument.SetDataSource(sqlds.Tables(0))
                CrystalReportViewer1.ReportSource = rptdocument
                CrystalReportViewer1.Refresh()

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    End Class
    -
  • i think it should be your code

    Imports CrystalDecisions.CrystalReports.Engine
    Imports System.Data.SqlClient
    Imports CrystalDecisions.Shared

    Public Class Form2

        Private Sub CrystalReportViewer1_Load(sender As System.Object, e As System.EventArgs) Handles CrystalReportViewer1.Load
            Try
                strsql = "Select * from InvoiceHeader H join InvoiceDetailsPrn D on D.sInvnumber=H.Invnumber where invnumber='703053'"

                Dim sqlda As New SqlDataAdapter(strsql, sqlconn)
                sqlda.Fill(sqlds, "table1")
                strreportname = "Report1"
                Dim strreportpath As String = Application.StartupPath & "\" & strreportname & ".rpt"
                
       If Not IO.File.Exists(strreportpath) Then
                    Throw (New Exception("unable to locate file : " & vbCrLf & strreportpath))
       Else
    Dim rptdocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
                rptdocument.Load(strreportpath)
                           
                rptdocument.SetDataSource(sqlds.Tables(0))
                CrystalReportViewer1.ReportSource = rptdocument
                End If
                

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    End Class

  • Hi Waqas,

    I tried using the code you suggested. Still get the same error message. Any inputs on this will be greatly appreciated. Thanks.
  • debug it by breakpoints and check which specific line throwing error.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Popular Posts of the Week

Top Posters Weekly

Optimum Creative