Crystal Report always asking to enter parameter value - vb.net

I am having a problem passing the combobox value to crystal report I am kinda newbie with the crystal report so I really need your help guys!
here is my code:
dir = Path.GetFullPath(dir)
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
Dim reportsfolder As String = Application.StartupPath + "\PrintArea\LoanProdReport.rpt"
Dim report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim DS As New DataSet
Dim query As String
query = "SELECT * FROM ClientDatabase"
Dim DA As New SqlDataAdapter(query, jonsqlcon)
DA.Fill(DS)
report.Load(reportsfolder)
report.SetDatabaseLogon(dbSettingsAdder.TBUserID.Text, dbSettingsAdder.tbPassword.Text)
crParameterFieldDefinitions = report.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("YearHeader")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterDiscreteValue = New ParameterDiscreteValue()
crParameterDiscreteValue.Value = ComboBoxEx2.SelectedItem.ToString
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
report.SetDataSource(DS.Tables(0))
LoanProdRpt.CrystalReportViewer1.ReportSource = report
LoanProdRpt.CrystalReportViewer1.Refresh()
LoanProdRpt.ShowDialog()
and here is the Screenshot of my Report
as you can see I am trying to pass the Combobox value into parameter YearHeader.
Any help will be appreciated. Thanks in Advance :)

How silly of me, I just did it like this
dir = Path.GetFullPath(dir)
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
Dim reportsfolder As String = Application.StartupPath + "\PrintArea\LoanProdReport.rpt"
Dim report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim DS As New DataSet
Dim query As String
query = "SELECT * FROM ClientDatabase"
Dim DA As New SqlDataAdapter(query, jonsqlcon)
DA.Fill(DS)
report.Load(reportsfolder)
report.SetDatabaseLogon(dbSettingsAdder.TBUserID.Text, dbSettingsAdder.tbPassword.Text)
report.SetDataSource(DS.Tables(0))
crParameterFieldDefinitions = report.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("YearHeader")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterDiscreteValue = New ParameterDiscreteValue()
crParameterDiscreteValue.Value = ComboBoxEx2.SelectedItem.ToString
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
LoanProdRpt.CrystalReportViewer1.ReportSource = report
LoanProdRpt.CrystalReportViewer1.Refresh()
LoanProdRpt.ShowDialog()

Related

how to put numbering for Images in Listview

How to add numbering Images in Listview and below attached the code.
Private Sub retrieve()
ListView1.Clear()
Dim imglist As New ImageList
imglist.ColorDepth = ColorDepth.Depth32Bit
ListView1.LargeImageList = imglist
ListView1.LargeImageList.ImageSize = New System.Drawing.Size(200, 200)
Dim strsql As String = "select * from picEmploy where NumFile=" & TextBox1.Text
Dim dt_images As New DataTable
cmd.Connection = con
cmd.CommandText = strsql
adp.SelectCommand = cmd
adp.Fill(ds)
For Each dr As DataRow In ds.Tables(0).Rows
Dim imb_buffer = CType(dr("pic"), Byte())
Dim img_stream As New IO.MemoryStream(imb_buffer, True)
img_stream.Write(imb_buffer, 0, imb_buffer.Length)
imglist.Images.Add(dr("NumFile").ToString(), New Bitmap(img_stream))
img_stream.Close()
Dim lsvparent As New ListViewItem
'lsvparent.Text = dr("pic").ToString
lsvparent.ImageKey = dr("NumFile").ToString
ListView1.Items.Add(lsvparent)
Next
End Sub

How to pass values to two different parameters

I am developing a Windows Application and use a Crystal Report in it.
I am able to pass value to one parameter of Crystal Report. Now, I have added one more parameter to Crystal Report, but confused about what modification I have to make in below code.
Below code is written to pass value to one parameter of CR.
Private Sub btnLoadBatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadBatch.Click
Try
Dim cr As New crMonthwiseBatch
Dim oBatches As New Batches
Dim Month As Integer = dtFrom.Value.Date.Month
Dim StartDateForBatch As Date = New DateTime(dtBatchStartFromMonth.Value.Year, dtBatchStartFromMonth.Value.Month, "1")
Dim DaysinMonths As Integer = System.DateTime.DaysInMonth(dtBatchStartFromMonth.Value.Year, dtBatchStartFromMonth.Value.Month)
Dim EndDateForBatch = StartDateForBatch.AddDays(DaysinMonths)
oBatches.LoadByQuery("CreatedDate >= #" + StartDateForBatch + "# and CreatedDate <= #" + EndDateForBatch + "#")
cr.SetDataSource(oBatches)
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = "Batch List of Month - " + MonthName(dtBatchStartFromMonth.Value.Month) + " " + dtBatchStartFromMonth.Value.Year.ToString
crParameterFieldDefinitions = cr.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("MonthName")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
CrystalReportViewerMonthwiseBatch.ReportSource = cr
CrystalReportViewerMonthwiseBatch.Refresh()
Catch ex As Exception
End Try
End Sub
You can achieve it this way (look for comments above statement(s) for description)
Approach 1:
'...
'collection of objects for every parameter field
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
'represent a parameter field
Dim crParameterFieldDefinition As ParameterFieldDefinition
'collection of ParameterValue objects for every parameter field
Dim crParameterValues As New ParameterValues()
'for retrieving and setting discrete value parameters
Dim crParameterDiscreteValue As New ParameterDiscreteValue()
'get parameters collection in current crystal report
crParameterFieldDefinitions = cr.DataDefinition.ParameterFields
'adding FIRST parameter
'get the parameter in the current report and assign a value
crParameterFieldDefinition = crParameterFieldDefinitions("PARAM_1_NAME")
crParameterDiscreteValue.Value = "PARAM_1_VALUE"
crParameterValues = crParameterFieldDefinition.CurrentValues
'clear old/default values assigned to current parameter, add and apply new value
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
'finished adding FIRST parameter
'adding SECOND and subsequent parameters
'reset the collections
crParameterDiscreteValue = New ParameterDiscreteValue()
crParameterValues = New ParameterValues()
'get parameter, assign values, clear old values, add to collection and apply
crParameterFieldDefinition = crParameterFieldDefinitions("PARAM_2_NAME")
crParameterDiscreteValue.Value = "PARAM_2_VALUE"
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
'finished adding SECOND parameter
'...
'Just display the report
Approach 2:
If you have multiple parameters it can also be achieved by using a separate procedure such as
Private Sub SetParams(crRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument, strParamName As String, strParamValue As String)
For Each pfField As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition In crRpt.DataDefinition.ParameterFields
If pfField.Name.ToString().ToLower() = strParamName.ToLower() Then
Try
Dim crParameterValues As New CrystalDecisions.Shared.ParameterValues()
Dim crParameterDiscreteValue As New CrystalDecisions.Shared.ParameterDiscreteValue()
crParameterDiscreteValue.Value = strParamValue
crParameterValues = pfField.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
pfField.ApplyCurrentValues(crParameterValues)
Catch ex As Exception
'add your exception handling mechanism here
MessageBox.Show(ex.Message)
End Try
End If
Next
End Sub
The above routine can be called for any report object (as cr in example below) to add any number of parameters to the reports
SetParams(cr, "#Param_1_Name", "Param_1_Value")
SetParams(cr, "#Param_2_Name", "Param_2_Value")
'...
SetParams(cr, "#Param_n_Name", "Param_n_Value")

How to export dataset to xml with columns caption

I need to export dataset to xml file. For this i try next sample code:
Dim dt As DataTable = New DataTable()
dt.TableName ="Test"
Dim dc As DataColumn = New DataColumn("Name", GetType(String))
dc.Caption = "ColumnCaption1"
dt.Columns.Add(dc)
Dim nr As DataRow = dt.NewRow()
nr("Name") = "TestRow"
dt.Rows.Add(nr)
Dim Xml As System.Xml.XmlDocument = New System.Xml.XmlDocument()
Dim RootNode As System.Xml.XmlNode = Xml.CreateElement("Export")
Xml.AppendChild(RootNode)
Dim DocsNode As System.Xml.XmlNode = Xml.CreateElement("Documents")
RootNode.AppendChild(DocsNode)
Dim DocDataSet As DataSet = New DataSet("Doc1")
DocDataSet.Tables.Add(dt)
DocDataSet.EnforceConstraints = False
Dim XmlDoc As System.Xml.XmlDataDocument = New System.Xml.XmlDataDocument(DocDataSet)
Dim DocElement As System.Xml.XmlNode = Xml.ImportNode(XmlDoc.DocumentElement, True)
'DocElement.
DocsNode.AppendChild(DocElement)
Dim Settings As System.Xml.XmlWriterSettings = New System.Xml.XmlWriterSettings()
Settings.Encoding = System.Text.Encoding.UTF8
Settings.Indent = True
Settings.IndentChars = Chr(9)
Settings.CloseOutput = True
Settings.OmitXmlDeclaration = true
Dim DirectoryName as string = "C:\Temp"
Dim FileName As String = DirectoryName & "\myExport"
Dim OutStream As System.IO.FileStream = System.IO.File.Create(FileName)
Dim Writer As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(OutStream, Settings)
Xml.WriteTo(Writer)
Writer.Close()
It works and the result of export is:
<Export>
<Documents>
<Doc1>
<Test>
<Name>TestRow</Name>
</Test>
</Doc1>
</Documents>
</Export>
But I want that column caption (ColumnCaption1) also will in this xml. How can i do this?

VB.NET Getting All Items in ListBox and Pass to a Parameter

Hi I'm stuck with my loop passing all items in my listbox to a crystal report parameter.
For i = 0 To ListBoxBillAccount.Items.Count - 1
If ListBoxBillAccount.Items(i).selected Then
crParameterDiscreteValue = New ParameterDiscreteValue()
crParameterDiscreteValue.Value = ListBoxBillAccount.SelectedItems()
crParameterValues.Add(crParameterDiscreteValue)
End If
Next
I'm getting an error: "Public member 'selected' on type 'DataRowView' not found."
Edit: Here's my full code:
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterFieldDefinitions = crReportDoc.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions("UserAcct")
crParameterValues = crParameterFieldDefinition.CurrentValues
For i = 0 To ListBoxBillAccount.Items.Count - 1
If ListBoxBillAccount.Items(i).selected Then
crParameterDiscreteValue = New ParameterDiscreteValue()
crParameterDiscreteValue.Value = ListBoxBillAccount.SelectedItems()
crParameterValues.Add(crParameterDiscreteValue)
End If
Next
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
frmGenerateConsolidatedBill.CrystalReportViewer1.ReportSource = crReportDoc
frmGenerateConsolidatedBill.Refresh()
frmGenerateConsolidatedBill.Show()
Perhaps:
For Each obj In ListBoxBillAccount.SelectedItems()
Dim crParameterDiscreteValue = New ParameterDiscreteValue()
crParameterDiscreteValue.Value = obj
crParameterValues.Add(crParameterDiscreteValue)
Next

Passing Multiple Values to a single Crystal Reports parameter from VB.net

I am using Visual Basic in Visual Studio 2012 with Crystal Reports for Visual Studio.
I have a parameter in a Crystal Report that can accept multiple values (for instance: Regions). How do I pass multiple values to that single parameter from vb.net? The values would be coming from selections from a ListBox.
Sorry if this is answered elsewhere. I have tried many searches all over internet with no luck.
Thanks in advance!
Bob
As asked for, here is code from where I select a single value from ComboBox. The Parameter in the Crystal Report is BuyDate. I just am not sure how to make it for multiple values. If you need more details, just let me know.
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
Dim SSBuy = New CrystalDecisions.CrystalReports.Engine.ReportDocument()
Dim Filepath = "\\filepath\Report By Buy Date.rpt"
SSBuy.Load(Filepath)
crParameterDiscreteValue.Value = ComboBox1.SelectedItem
crParameterFieldDefinitions = SSBuy.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("BuyDate")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
SSBuyForm.CrystalReportViewer1.ReportSource = SSBuy
SSBuyForm.Refresh()
SSBuyForm.Show()
I was able to find the answer on the SAP website. Here is the link to the article, for anyone else who may need it.
Pass Multiple Values to a Parameter
Also, here is the actual code I used, which runs a report based on PO#'s selected from a ListBox.
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
Dim SSPO = New CrystalDecisions.CrystalReports.Engine.ReportDocument()
Dim Filepath = "\\Filepath\Report Download By PO #.rpt"
SSPO.Load(Filepath)
crParameterFieldDefinitions = SSPO.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions("PO#")
crParameterValues = crParameterFieldDefinition.CurrentValues
Dim Count As Integer = POList1.SelectedItems.Count
For i = 0 To Count - 1
If i > 0 Then
crParameterDiscreteValue = Nothing
End If
crParameterDiscreteValue = New ParameterDiscreteValue()
crParameterDiscreteValue.Value = POList1.SelectedItems(i)
crParameterValues.Add(crParameterDiscreteValue)
Next
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
SSPoForm.CrystalReportViewer1.ReportSource = SSPO
SSPoForm.Refresh()
SSPoForm.Show()