PDA

Click to See Complete Forum and Search --> : [RESOLVED] crystal reports and VB 2005


gp04lch
Nov 9th, 2006, 03:23 AM
hi everyone,

i'm trying to do reporting with crystal reports but i dont seem to be finding anything useful on the internet. can any of you point me in the correct direction? the syntax in VB2005 is so different from VB6.0.

thanks in advance.

shakti5385
Nov 9th, 2006, 03:45 AM
Hi :wave:
I Make a simple code for printing data in the crystal report just read each and every line in the code and check it.
Remember This code simply on the Form Load event.
Test this code
If you do not want to use the sql query then you can use the selection formulas also
If any problem related to this code then post here



Dim CrystalReportViewer1 As CrystalDecisions.Windows.Forms.CrystalReportViewer = New CrystalDecisions.Windows.Forms.CrystalReportViewer
Dim Report As CrystalDecisions.CrystalReports.Engine.ReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument
CrystalReportViewer1.ActiveViewIndex = 0
CrystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
CrystalReportViewer1.DisplayGroupTree = False
CrystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill
CrystalReportViewer1.Location = New System.Drawing.Point(0, 0)
CrystalReportViewer1.Name = "CrystalReportViewer1"

Dim QueryString As String = "select * from TableName" 'Your Query here
Dim Connection As New OleDbConnection(funcs.con) 'Your Database Connection Here
Connection.Open()

Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter(querystring, connection)'Passing the query in the connection
Dim DataSet As DataSet = New DataSet() 'DataSet
Adapter.Fill(DataSet)
Dim DataTable As DataTable = New DataTable 'DataTable
DataTable = DataSet.Tables(0) 'filling the datatable here
Report.Load(Application.StartupPath & "/ReportName.rpt") 'Report Name Here
Report.SetDataSource(DataTable)
CrystalReportViewer1.ReportSource = Report

gp04lch
Nov 10th, 2006, 10:14 PM
thank you. i'll give it a try when i get back.

Mark Gambo
Nov 11th, 2006, 06:41 PM
I can't wait Until the tags are fixed!

shakti5385
Nov 12th, 2006, 12:28 AM
I can't wait Until the tags are fixed!
Which Tags :confused:

Mark Gambo
Nov 12th, 2006, 08:13 AM
Which Tags :confused:

The your code goes in here Tags

shakti5385
Nov 13th, 2006, 12:21 AM
The your code goes in here Tags
I use that code but why that are not working i have not any idea :confused:

Mark Gambo
Nov 13th, 2006, 07:35 AM
I use that code but why that are not working i have not any idea :confused:


They upgraded the Fourm Software, take a look at this LINK (http://vbforums.com/showthread.php?t=437394). According to the Post the tags should be working by today.

shakti5385
Nov 13th, 2006, 07:44 AM
Ic ;)

gp04lch
Dec 26th, 2006, 12:48 AM
thank you everyone and sorry for the late reply. what happens if i want to preview the report in crystal report itself? i cant seem to find the revelant codes.

AlphaOne
Dec 29th, 2006, 03:21 AM
Please HELP

I am trying to get this code to work and the only thing I get is a empty form:-(

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim CrystalReportViewer1 As CrystalDecisions.Windows.Forms.CrystalReportViewer = New CrystalDecisions.Windows.Forms.CrystalReportViewer
Dim Report As CrystalDecisions.CrystalReports.Engine.ReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument
CrystalReportViewer1.ActiveViewIndex = 0
CrystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
CrystalReportViewer1.DisplayGroupTree = False
CrystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill
CrystalReportViewer1.Location = New System.Drawing.Point(0, 0)
CrystalReportViewer1.Name = "CrystalReportViewer"

Dim QueryString As String = "select * from Companies" 'Your Query here
Dim Connection As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=c:\alpha.mdf;Integrated Security=True;User Instance=True") 'Your Database Connection Here
Connection.Open()

Dim Adapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(QueryString, Connection) 'Passing the query in the connection
Dim DataSet As DataSet = New DataSet() 'DataSet
Adapter.Fill(DataSet)
Dim DataTable As DataTable = New DataTable 'DataTable
DataTable = DataSet.Tables(0) 'filling the datatable here
Report.Load(Application.StartupPath & "/CrystalReport1.rpt") 'Report Name Here
Report.SetDataSource(DataTable)
End Sub

Anybody having a sample on how to use Crystal in VB.net?

Bert

shakti5385
Dec 29th, 2006, 04:05 AM
Read Cr in VB.NET At my signature Last Post

AlphaOne
Dec 29th, 2006, 04:34 AM
Well, that is what I have been doing. If you look on my code will you see that its from your "Cr in VB.Net

Bert

shakti5385
Dec 29th, 2006, 04:48 AM
Well, that is what I have been doing. If you look on my code will you see that its from your "Cr in VB.Net

Bert
See Again The Last Post There is New Code I Pot Recently, If Any Problem Then Tell

gp04lch
Dec 30th, 2006, 09:46 AM
thank you so much shakti for the examples. they were great! i was able to load crystal report through vb without any difficulties. however, i DO have difficulties trying to pass parameters into the report.

i start out doing the easy parts first:

1. creat a form called CrystalReportForm with the following function:

Friend Sub ViewReport(ByVal ReportName As String, ByVal TableName() As String, ByVal QueryString() As String, Optional ByVal [Parameter] As String = "")
Me.MdiParent = MainForm
If Not UBound(TableName).Equals(UBound(QueryString)) Then MessageBox.Show("Passed Variable Are Not Correct", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information) : Exit Sub
Dim Report As CrystalDecisions.CrystalReports.Engine.ReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim CrystalReportViewer As CrystalDecisions.Windows.Forms.CrystalReportViewer = New CrystalDecisions.Windows.Forms.CrystalReportViewer
CrystalReportViewer.ActiveViewIndex = 0
CrystalReportViewer.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
CrystalReportViewer.DisplayGroupTree = False
CrystalReportViewer.Dock = System.Windows.Forms.DockStyle.Fill
CrystalReportViewer.Location = New System.Drawing.Point(0, 0)
CrystalReportViewer.Name = "CrystalReportViewer"
Dim Adapter As New OleDb.OleDbDataAdapter
Dim DataSet As New DataSet
For I As Integer = 0 To UBound(TableName)
Adapter = GetDataAdeptor(QueryString(I))
Adapter.Fill(DataSet, TableName(I))
Next
'Report In the report Folder
Report.Load(Application.StartupPath & "/Report/" & ReportName & "")
Report.SetDataSource(DataSet)
If Not [Parameter] = "" Then Report.SetParameterValue(0, [Parameter])
CrystalReportViewer.ReportSource = Report
Me.Panel1.Controls.Add(CrystalReportViewer)
End Sub

2. i then create another form with a simple button and code (no tables no queries yet) just to test if it works. i pasted this code into the buttonand it works.

Dim ReportForm As New CrystalReportForm
Dim TableName(0) As String
Dim QueryString(0) As String
TableName(0) = "TableName"
QueryString(0) = "SELECT * FROM TableName"
'ReportForm.MdiParent = MainForm 'Pass For Mdi True
ReportForm.ViewReport("CrystalReport1.rpt", TableName, QueryString, )
' You can pass the Parameter Value It is Optional
ReportForm.Show()

3. i then read about parameter passing and wanted to try it out so i added the following into the button control as well.

Imports CrystalDecisions.CrystalReports.Engine

then under the button:

Dim crParameterDiscreteValue As ParameterDiscreteValue
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldLocation As ParameterFieldDefinition
Dim crParameterValues As ParameterValues

crParameterFieldDefinitions = report.DataDefinition.ParameterFields
crParameterFieldLocation = crParameterFieldDefinitions.Item("@ParameterName1")
crParameterValues = crParameterFieldLocation.CurrentValues
crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
crParameterDiscreteValue.Value = "Parameter1Value"
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

however for the ParameterDiscretevalue and ParameterValues i get the error 'type not defined'.

and for the crParameterFieldDefinitions = report.DataDefinition.ParameterFields i get an error message 'DataDefinition is not a member of Reportname.crystalreport'.

what am i doing wrong here?? is there something i left out?

best regards and happy new year.

shakti5385
Jan 2nd, 2007, 12:50 AM
. i then read about parameter passing and wanted to try it out so i added the following into the button control as well.

Imports CrystalDecisions.CrystalReports.Engine

then under the button:

Dim crParameterDiscreteValue As ParameterDiscreteValue
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldLocation As ParameterFieldDefinition
Dim crParameterValues As ParameterValues

crParameterFieldDefinitions = report.DataDefinition.ParameterFields
crParameterFieldLocation = crParameterFieldDefinitions.Item("@ParameterName1")
crParameterValues = crParameterFieldLocation.CurrentValues
crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
crParameterDiscreteValue.Value = "Parameter1Value"
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

however for the ParameterDiscretevalue and ParameterValues i get the error 'type not defined'.

and for the crParameterFieldDefinitions = report.DataDefinition.ParameterFields i get an error message 'DataDefinition is not a member of Reportname.crystalreport'.

Do not do this
Pass the value of parameter in the funciton and for multiple parameter you can use array of parameter also.
If any need to change the funciton then change it.
Pass the parameter as I mention
If Not [Parameter] = "" Then Report.SetParameterValue("ParameterName", [Parameter])

gp04lch
Jan 2nd, 2007, 06:46 AM
thank you so very much. that did the trick!

shakti5385
Jan 2nd, 2007, 07:41 AM
thank you so very much. that did the trick!
Your Welcome :thumb:
Get the solution then please go to the thread tool menu and click on the Resolved this thread, so people know that it is resolved.