Crystal Report does not load - vb.net

My Crystal Report does not load.
This is my code:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class bincard
Private Sub bincard_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
Dim cryRpt As New ReportDocument
cryRpt.Load(Application.StartupPath + "bincard1.rpt")
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = TextBox1.Text
crParameterFieldDefinitions =
cryRpt.DataDefinition.ParameterFields
crParameterFieldDefinition =
crParameterFieldDefinitions.Item("itemid")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class

From:
cryRpt.Load(Application.StartupPath + "bincard1.rpt")
To:
cryRpt.Load(Application.StartupPath + "\\bincard1.rpt")

I changed:
cryRpt.Load(Application.StartupPath + "bincard1.rpt")
To:
cryRpt.Load(Application.StartupPath & "bincard1.rpt")
Then report design set to copy output directory=copy if newer and compile to content.

Your path in cryRpt.Load() is wrong. As it stands it will look something like this:
C:\Program Files\Application Namebincard1.rpt
Notice how there is no backslash between "Application Name" and "bincard1.rpt". This would make the path invalid. That is because you are using + to concatenate paths which will cause some issues.
Instead consider using Path.Combine to join Application.StartupPath and "bincard1.rpt" together:
cryRpt.Load(Path.Combine(Application.StartupPath, "bincard1.rpt"))
This will give you a path similar to:
C:\Program Files\Application Name\bincard1.rpt

Related

Type expected error VB.Net

Error ImagePlease assist, I get a "Type expected" error on my code for a windows based application. I get the error on this line "Dim objSW As New StreamWriter(objFS)"
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim strFileName As String = My.Application.Info.DirectoryPath & "\empout_fixed.txt"
Dim objFS As New FileStream(strFileName, FileMode.Create, FileAccess.Write)
Dim objSW As New StreamWriter(objFS)
Dim strEmpName As String
Dim intDeptNbr As Integer
Dim strJobTitle As String
Dim dtmHireDate As Date
Dim sngHrlyRate As Single
strEmpName = “Thabo Lereko”
intDeptNbr = 1001
strJobTitle = “Junior Programmer”
dtmHireDate = #10/05/2014#
sngHrlyRate = 99.99
' Write out the record to the file ...
objSW.WriteLine(strEmpName.PadRight(20) &
intDeptNbr.ToString.PadLeft(4) &
Space(5) &
strJobTitle.PadRight(21) &
Format(dtmHireDate, "M/d/yyyy").PadRight(10) &
Format(sngHrlyRate, "Standard").PadLeft(5))
MsgBox("Record was written to the output file.")
objSW.Close()
End Sub
End Class
The problem is that you have named your project "StreamWriter", which causes "StreamWriter" to refer to the namespace of the project. Maybe you should use more a bit more descriptive project names in the future just for clarity?
You can fix this by referring to the real StreamWriter with namespace:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim strFileName As String = My.Application.Info.DirectoryPath & "\empout_fixed.txt"
Using objFS As New FileStream(strFileName, FileMode.Create, FileAccess.Write)
Using objSW As New System.IO.StreamWriter(objFS)
Dim strEmpName As String
Dim intDeptNbr As Integer
Dim strJobTitle As String
Dim dtmHireDate As Date
Dim sngHrlyRate As Single
strEmpName = “Thabo Lereko”
intDeptNbr = 1001
strJobTitle = “Junior Programmer”
dtmHireDate = #10/05/2014#
sngHrlyRate = 99.99
' Write out the record to the file ...
objSW.WriteLine(strEmpName.PadRight(20) &
intDeptNbr.ToString.PadLeft(4) &
Space(5) &
strJobTitle.PadRight(21) &
Format(dtmHireDate, "M/d/yyyy").PadRight(10) &
Format(sngHrlyRate, "Standard").PadLeft(5))
MsgBox("Record was written to the output file.")
End Using
End Using
End Sub
End Class
Ps. Added the using-statements that should always be used with iDisposable-objects. Removed the unnecessary close-call also.

My Crystal Report is not showing the hole list

Please help with my crystal report, because it doesn't show the hole list when i use parameter fields for a list. here is my class below
`Imports System.Windows.Forms.Application
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Windows.Forms
Public Class CrystalReportHelperClass
Dim Sql As New SqlHelper
Dim CR_ParamDisVals As New ParameterDiscreteValue
Dim CR_ParamVals As New ParameterValues
Dim CR_ParamDef As ParameterFieldDefinition
Dim CR_ParamDefs As ParameterFieldDefinitions
Dim CRPTDoc As New ReportDocument
Dim CRPTViewer As New CrystalReportViewer
Public Sub New(ByRef CReport As CrystalDecisions.Windows.Forms.CrystalReportViewer)
CRPTViewer = CReport
CRPTDoc = CReport.ReportSource
End Sub
Public Sub CrystalObjectParam(ByVal CrystalReportObject As String, ByVal Message As String)
CR_ParamDisVals.Value = Message
CR_ParamDefs = CRPTDoc.DataDefinition.ParameterFields
CR_ParamDef = CR_ParamDefs.Item(CrystalReportObject)
CR_ParamVals = CR_ParamDef.CurrentValues
CR_ParamVals.Clear()
CR_ParamVals.Add(CR_ParamDisVals)
CR_ParamDef.ApplyCurrentValues(CR_ParamVals)
End Sub
Public Function GetCrystalReport() As ReportDocument
Return CRPTDoc
End Function
End Class
`
then here is the form load
Private Sub ReportEmployeeList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim RepDoc As New ReportDocument
RepDoc = CRPT_Employee.ReportSource
If Sql.HasConnection() = True Then
Dim DT As DataTable = Sql.ExecuteDataTableSP("SelectWorking")
For Each Data As DataRow In DT.Rows
CRPT.CrystalObjectParam("ID", Data(1))
Next
Else
MsgBox("System Database Cannot be Connected", MsgBoxStyle.Information)
End If
CRPT_Employee.ReportSource = CRPT.GetCrystalReport
CRPT_Employee.Refresh()
End Sub
then i have 1 parameter field in my crystal report, id -> discrete value
what should i do?
i already used other methods like this one
Private Sub SetCurrentValuesForParameterField(ByVal myParameterFields As ParameterFields, ByVal myArrayList As ArrayList)
Dim currentParameterValues As ParameterValues = New ParameterValues()
For Each submittedValue As Object In myArrayList
Dim myParameterDiscreteValue As ParameterDiscreteValue = New ParameterDiscreteValue()
myParameterDiscreteValue.Value = submittedValue.ToString()
currentParameterValues.Add(myParameterDiscreteValue)
Next
Dim myParameterField As ParameterField = myParameterFields(PARAMETER_FIELD_NAME)
myParameterField.CurrentValues = currentParameterValues
End Sub
modify your method as below if i understood what do you want>>>
Dim DT As DataTable = Sql.ExecuteDataTableSP("SelectWorking")
For Each Data As DataRow In DT.Rows
CRPT.SetParameterValue("ID", Data(1))

How to pass parameters to Crystal reports?

I am trying to filter customers using a parameter in Crystal, by typing a customer code in a textbox. I created a parameter for the customer code, and tried to pass it's value using the code shown below, but it is not working. When I click on the button it is showing all customers not only the filtered ones:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class ParametroCrForm
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim relatorio As New ReportDocument
relatorio.Load("C:\Users\Fernando e Flavia\Documents\Visual Studio 2010\Projects\Crystal.Estudo\Crystal.Estudo\CrystalReport1.rpt")
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
If codigoTextBox.Text = "" Then
MessageBox.Show("Digite um c�digo de cliente", "Aten��o", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Exit Sub
End If
crParameterDiscreteValue.Value = codigoTextBox.Text
crParameterFieldDefinitions = relatorio.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("codigoParametro")
crParameterValues.Clear()
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
CrystalReportViewer1.ReportSource = relatorio
CrystalReportViewer1.Refresh()
End Sub
End Class
According to explanation from ABZY, I had to add selection formula in my report.
In the link below it is explained how to do that:
http://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htm
Thanks again Abzy!!

Export crystal report into pdf in vb without using form

I'm a newbie to VB and Crystal Reports.
I want to make an .exe file in VB.NET that doesn't use a Form.
I'm using Visual Studio 2010.
The .exe file is just purely for exporting a Crystal Report into a .pdf file, where should I start?
Should I use the console application?
Should I use the empty project code?
I have searched the internet and cannot find any references.
Please let me know if there is a Reference that I can refer to.
You could probably use a console app or a forms app but just don't display the form. I tend to point people to the code samples at: http://vb.net-informations.com/crystal-report/vb.net_crystal_report_export_pdf.htm
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Form1
Dim cryRpt As New ReportDocument
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim CrExportOptions As ExportOptions
Dim CrDiskFileDestinationOptions As New _
DiskFileDestinationOptions()
Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
CrDiskFileDestinationOptions.DiskFileName = _
"c:\crystalExport.pdf"
CrExportOptions = cryRpt.ExportOptions
With CrExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
.DestinationOptions = CrDiskFileDestinationOptions
.FormatOptions = CrFormatTypeOptions
End With
cryRpt.Export()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class

Dynamic Crystal Report using VS2010 Vb.net

I am trying to generate a report using Crystal studio that takes a public variable from the vb.net application. I think the best way to do it is to just dynamically give the filter to the report at runtime, but I can't figure out how to set it up to take any information at runtime. Any advice?
The best way is to build your report with a parameter which is used in the record selection criteria. You can then load the report and populate the parameter like:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocument
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = TextBox1.Text
crParameterFieldDefinitions = -
cryRpt.DataDefinition.ParameterFields
crParameterFieldDefinition = _
crParameterFieldDefinitions.Item("Customername")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
Code from: http://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htm
End Class
string query = "select * from TestReport";
sqlconn.Open();
da = new SqlDataAdapter(query, sqlconn);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
da.Fill(DS.TestReport);//DS is a DataSet object .
myCrystalReport1.SetDataSource(DS);
//-----------------
ParameterField paramfield = new ParameterField();
ParameterFields paramfields = new ParameterFields();
ParameterDiscreteValue discreteval = new ParameterDiscreteValue();
paramfield.Name = "myfirstname";
discreteval.Value = "10";
paramfield.CurrentValues.Add(discreteval);
paramfields.Add(paramfield);
crystalReportViewer1.ParameterFieldInfo = paramfields;
//-----------------
crystalReportViewer1.ReportSource = myCrystalReport1;
crystalReportViewer1.Refresh();
sqlconn.Close();