Pages

Search This Blog

Sunday, 15 April 2012

ADDING CRYSTAL REPORT IN VISUAL STUDIO 2008

  1. Open Visual Studio 2008
  2. Create a new project
  3. Click Project->Add item
  4. In Reporting tab select Crystal Report
  5. Select DataSource and column to display
Here my datasource is a XML file and I generated XML file when a button is clicked:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
        Dim myData As New DataSet
        Dim cmd As New MySqlCommand
        Dim myAdapter As New MySqlDataAdapter
        Try
            obj.connecttodatabase()
            cmd.CommandText = "select mainitemcode as ItemCode,mainitemname as ItemName,mainitemfloor as Location from mainitem"
            cmd.Connection = obj.con
            myAdapter.SelectCommand = cmd
            myAdapter.Fill(myData)
            myData.WriteXml("D:\mainitem.xml", XmlWriteMode.WriteSchema)
            myData.Clear()
            obj.disconnectdatabase()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Report could not be created", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
To view the report add CrystalReportViewer control to a form and place the code:

Imports CrystalDecisions.CrystalReports.Engine

'Place code in any event like form load or click
       Dim cryRpt As New ReportDocument
        cryRpt.Load( "D:\CrystalReport1.rpt" )
        frmF.CrystalReportViewer1.ReportSource = cryRpt
        frmF.CrystalReportViewer1.Refresh()

No comments:

Post a Comment