crystal report viewer The Parameter is incorrect - vb.net

i tried to pass parameter to crystal report viewer. but it show The Parameter is incorrect.
Dim rpt As New ReportDocument
rpt.FileName = My.Application.Info.DirectoryPath & "\MAuditList.rpt"
Dim paramDV As New ParameterDiscreteValue()
paramDV.Value = frmMachine.machine
rpt.ParameterFields("#mchno").CurrentValues.Clear()
rpt.ParameterFields("#mchno").DefaultValues.Clear()
rpt.ParameterFields("#mchno").CurrentValues.Add(paramDV)
paramDV.Value = frmMachine.batch - 1
rpt.ParameterFields("#batch").CurrentValues.Clear()
rpt.ParameterFields("#batch").DefaultValues.Clear()
rpt.ParameterFields("#batch").CurrentValues.Add(paramDV)
paramDV.Value = InpStdPt
rpt.ParameterFields("#Points").CurrentValues.Clear()
rpt.ParameterFields("#Points").DefaultValues.Clear()
rpt.ParameterFields("#Points").CurrentValues.Add(paramDV)
paramDV.Value = OvrPoints
rpt.ParameterFields("#ovrpt").CurrentValues.Clear()
rpt.ParameterFields("#ovrpt").DefaultValues.Clear()
rpt.ParameterFields("#ovrpt").CurrentValues.Add(paramDV)
Form1.CrystalReportViewer1.ReportSource = rpt
Form1.Show()</code>
any error in these code??

You should assign the parameters to your Report object and then assign the report object to your CrystalReportViewer.
Try something like this:
'Create report object and load the report
Dim rpt As New ReportDocument()
rpt.FileName = My.Application.Info.DirectoryPath & "\MAuditList.rpt"
'Set the Parameters
Dim paramDV As New ParameterDiscreteValue()
paramDV.Value = "Parameter Value"
rpt.ParameterFields("#ParameterName").CurrentValues.Clear()
rpt.ParameterFields("#ParameterName").DefaultValues.Clear()
rpt.ParameterFields("#ParameterName").CurrentValues.Add(paramDV)
'Assign the report object to the Viewer Report Source
Form1.CrystalReportViewer1.ReportSource = rpt
Form1.Show()

Related

vb.net .VerifyDatabase for Crystal Reports using a password restricted Access DB

As the question title suggests, my vb.net project that uses Crystal Reports 9 and a password restricted MS-Access 97 database.
When loading reports, the Load method calls cReport.VerifyDatabase() which has previously been working fine without a password, but since adding one, it isn't working, and gives me the following error
Log on failed.
at CrystalDecisions.CrystalReports.Engine.ReportDocument.VerifyDatabase()
What do I need to add/change to this code for it to log on successfully?
Private Sub frmReportViewer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
setFormSizes(Me, con)
Me.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2), 10)
Me.Text = "Report Viewer - (" & Replace(cReport.FileName, "rassdk://", "") & ")"
cReport.VerifyDatabase()
cReport.Refresh()
crViewer.ReportSource = cReport
crViewer.ToolPanelView = CrystalDecisions.Windows.Forms.ToolPanelViewType.None
crViewer.Zoom(87)
Catch ex As Exception
errorLog(ex)
End Try
End Sub
EDIT
The data can be viewed when opening the report file in Crystal Reports itself, but this is because I can manually enter the password.
According to this documentation : 1.4.4.1.2
Use this method to log database with your report before verify the database :
Private Function SubreportLogon_RD(ByVal TestReport As ReportDocument) As ReportDocument
Dim dataSourceConnections As DataSourceConnections = TestReport.DataSourceConnections
Dim connectInfo As IConnectionInfo = dataSourceConnections(0)
connectInfo.SetConnection("ServerName", "DatabaseName", "UserName", "Password")
Dim TestSubreport As ReportDocument = TestReport.Subreports("Subreport1")
Dim subreportDataSourceConnections As DataSourceConnections = TestSubreport.DataSourceConnections
Dim connectInfo As IConnectionInfo = subreportDataSourceConnections(0)
connectInfo.SetLogon("UserName", "Password")
End Function
As per MSDN
Dim logOnInfo As New TableLogOnInfo()
Dim i As Integer
For i = 0 To cReport.Database.Tables.Count - 1
logOnInfo.ConnectionInfo.DatabaseName = "Database_Name"
logOnInfo.ConnectionInfo.Password = "Password"
cReport.Database.Tables.Item(i).ApplyLogOnInfo(logOnInfo)
Next i

Crystal report Formula Error String is required

I am working in crystal reports and I have created two parameters named startDate and endDate and when I go to Formula selection and I write the code and save it I got an error message "string is required"
the code is:
if {Employees.DateField} >={?startDatePF} and {Employees.DateField}<={?endDate} then true
Please tell me how can I resolve this error.
Public Sub GetReport()
Dim rpt As New CrystalReport1
rpt.Load(Application.StartupPath & "\CrystalReport1.rpt")
Dim pfield As New ParameterField
Dim pfields As New ParameterFields
Dim pdescrete As New ParameterDiscreteValue
Dim pfield1 As New ParameterField
Dim pfields1 As New ParameterFields
End Sub

change crystal reports ADO.NET(XML) dynamically

I am developing a project using VB2008 and Crystal Reports 11.5.
I have added new crystal report to the project and it is working fine with the folowing code.
Dim ds As New DataSet
mysqlDataAdapter1 = New MySqlDataAdapter("SELECT * from client_reg where c_qid='" & TextBox2.Text & "'", SQLConnection)
mysqlDataAdapter1.Fill(ds, "client_reg")
ds.WriteXml(CurDir() + "\work_order.xml", XmlWriteMode.WriteSchema)
Dim cryRpt As New work_order '// this is my rpt file in project
rpt_view.CrystalReportViewer1.ReportSource = cryRpt '// rpt_view is the form with empty report viewer
rpt_view.CrystalReportViewer1.Refresh()
rpt_view.CrystalReportViewer1.RefreshReport()
rpt_view.Show()
Now I want to change the XML file location dynamically as follows;
ds.WriteXml("C:\new\bin\work_order.xml", XmlWriteMode.WriteSchema)
How can I change the RPT datasource to new path???
Thank you,
Sameera
Try Something like :
Create Property called:
Public Property ds() As DataSet
Get
Return ViewState("ds")
End Get
Set(ByVal value As DataSet)
ViewState("ds") = value
End Set
End Property
Then set report datasource to be the dataset
Public Sub BindReport(ByVal Id As String)
ds.WriteXml("C:\new\bin\work_order.xml", XmlWriteMode.WriteSchema)
cryRpt.SetDataSource(ds)
rpt_view.ReportSource = cryRpt
If rblFormat.SelectedValue = 1 Then
cryRpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "ExportedReport")
Else
cryRpt.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, True, "ExportedReport")
End If
Call This method at the button event

Parameter count mismatch. on setDataSource

I am trying the following and my report contains no parameters also Iam using .Net Objects
Dim data = GetDataConnection()
Dim ds = (From th In data.TranHeads Where th.THCSCode = "YYY001"
Select th).ToList
Dim crySalesTran As ReportDocument = New IBS_DAL.rep_SalesTransaction
crySalesTran.SetDataSource(ds.AsEnumerable)
CrystalReportViewer1.ReportSource = crySalesTran
for some reason when it reaches setdatasource
TargetParameterCountException Parameter count mismatch.
I found the answer although not sure why this works and the other option doesn't
Dim da = New System.Data.SqlClient.SqlDataAdapter("SELECT THSysRef, THCSCode FROM TranHead Where THCSCode = "YYY001" ", data.Connection)
Dim dtTH = New DataTable
da.Fill(dtTH)
Dim crySalesTran As ReportDocument = New IBS_DAL.rep_SalesTransaction
crySalesTran.Database.Tables(0).SetDataSource(dtTH)
CrystalReportViewer1.ReportSource = crySalesTran

passing date range parameters from a vb.net windows application

I need to pass date range parameters to a crystal report from a vb.net application, but I don't know how to. For anyone who may want to help me, please base yourself on the following scenario:
I have a crystal report that pulls data from an oracle database. In the database, I have a table of students, and each record has a birth date, name and surname. I need to display on the report only records where the birth date is between date X and date Y inclusive of both. Date X and Date Y are dates passed on the vb.net application when opening the report.
I am using crystal reports 9, visual studio 2008 and oracle 9i.
Somebody please help me out, how do I achieve this?
Assuming your Crystal Report is bound to an SQL Command, say:
SELECT name, surname FROM students WHERE birthdate BETWEEN {?#pDate1} AND {?#pDate2}
From VB.NET, you need to pass parameters as below:
Private rptSummary As New ReportDocument()
Protected Sub Page_Init(sender As Object, e As EventArgs)
Try
Dim pFields As New ParameterFields()
Dim pField_Date1 As New ParameterField()
Dim pField_Date2 As New ParameterField()
Dim pDiscreteValue_Date1 As New ParameterDiscreteValue()
Dim pDiscreteValue_Date2 As New ParameterDiscreteValue()
Dim ApplPath As String = Server.MapPath("MyReport.rpt")
rptSummary.FileName = ApplPath
pField_Date1.Name = "#pDate1"
pField_Date2.Name = "#pDate2"
Dim BirthDate1, BirthDate2
' Parameter: #pDate1
pDiscreteValue_Date1.Value = BirthDate1
pField_Date1.CurrentValues.Add(pDiscreteValue_Date1)
pFields.Add(pField_Date1)
' Parameter: #pDate2
pDiscreteValue_Date2.Value = BirthDate2
pField_Date2.CurrentValues.Add(pDiscreteValue_Date2)
pFields.Add(pFieldDate2)
CrystalReportViewer1.ParameterFieldInfo = pFields
rptSummary.SetParameterValue(0, pDiscreteValue_Date1)
rptSummary.SetParameterValue(1, pDiscreteValue_Date2)
CrystalReportViewer1.ReportSource = rptSummary
Catch ex As Exception
End Try
End Sub
This code is originally in ASP.NET with C#. I converted it to VB.NET. Please correct any discrepancies.
Just create a crystal report with required parameters and create button, datetimepicker for from date and to date in the from.
insert the code for button click.
Working perfectly for me..............
If any mistakes modify..
Thank Q
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt1 As New ReportDocument
Dim str As String = System.AppDomain.CurrentDomain.BaseDirectory
str = str.Substring(0, str.Length - 10)
cryRpt1.Load(str & "daily_scrl_rep.rpt")
cryRpt1.SetParameterValue(0, DTPFrom.Text)
cryRpt1.SetParameterValue(1, DTPTO.Text)
CrystalReportViewer1.ReportSource = cryRpt1
CrystalReportViewer1.Refresh()
End Sub
Just 2 Lines will be enough
rptSummary.SetParameterValue("your report perameter name", your value 1st value)
rptSummary.SetParameterValue("your report perameter name", your value 2nd value)
Thats it.
string rang1 = Session["Rang1"].ToString();
string rang2 = Session["Rang2"].ToString(); ;
ReportDocument obj = new ReportDocument();
obj.Load(Server.MapPath("~/CType_CrystalReport.rpt"));
obj.SetDatabaseLogon("sa", "12qwaszx", "BTS-10", "BTS_ERP");
ParameterFields paraf = new ParameterFields();
ParameterField par = new ParameterField();
ParameterField par2 = new ParameterField();
par.ParameterFieldName = "CtCode";
ParameterDiscreteValue dcpara1 = new ParameterDiscreteValue();
ParameterDiscreteValue dcpara2 = new ParameterDiscreteValue();
par.Name = rang1;
par2.Name = rang2;
dcpara1.Value = rang1.ToString();
par.CurrentValues.Add(dcpara1);
paraf.Add(par);
dcpara2.Value = rang2.ToString();
par2.CurrentValues.Add(dcpara2);
paraf.Add(par2);
CrystalReportViewer1.ParameterFieldInfo = paraf;
obj.SetParameterValue("CtCode", dcpara1);
obj.SetParameterValue("CtCode", dcpara2);
CType_DataSet dsCustomers = GetData("select * from GL_CUSTTYPE where CT_CODE between '" + rang1.ToString() + "' AND '" + rang2.ToString() + "' ");
obj.SetDataSource(dsCustomers);
CrystalReportViewer1.ReportSource = obj;
CrystalReportViewer1.RefreshReport();
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Dim crtableLogoninfos As New CrystalDecisions.Shared.TableLogOnInfos()
Dim crtableLogoninfo As New CrystalDecisions.Shared.TableLogOnInfo()
Dim crConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo()
Dim crit As String
Dim CrTables As Tables
Dim CrTable As Table
reportdocument.Load(Server.MapPath("~/Reports/IssueReport.rpt"))
CrTables = reportdocument.Database.Tables
For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
CrTable.Location = crConnectionInfo.DatabaseName & ".dbo." & CrTable.Location.Substring(CrTable.Location.LastIndexOf(".") + 1)
Next
Crit = "{tablename.fieldname}>=#" & Format(CDate(txtfromdate.Text), "yyyy/MM/dd") & "#"
Crit = Crit & " and {tablename.fieldname} <=#" & Format(CDate(txtto.Text), "yyyy/MM/dd") & "#"
reportdocument.RecordSelectionFormula = Crit
CrystalReportViewer1.ReportSource = reportdocument
CrystalReportViewer1.RefreshReport()