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();
Related
This is my first post so forgive me if I goof!
I am trying to update myself from VBA to VB.Net. Using loads of help from Google etc I am doing Ok except when I try to update my table from a DataGridView. It just does not update. What I would like is that a cell is update on change. My code so far is shown (I have tried all sorts of builder, tables etc so my code may have a smattering of these that are redundant):
Imports System.Data.SqlClient
Imports System.IO
Imports Microsoft.SqlServer
Public Class FrmData
Private dsS As DataSet = New DataSet
Private adpS As SqlDataAdapter
Private builder As SqlCommandBuilder
Private bsource = New BindingSource
Private Sub FrmData_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
Dim sqlS = "SELECT [Data].[Date] AS [Date] ,
[Data].[PaidBy] AS [Paid By] ,
[Data].[PaidAmount] AS [Paid Amount (£)],
[Data].[AmountLeft] AS [Amount Left (£)]
FROM [Data] WHERE [Data].[Name]= '" & strName & "'
ORDER BY [Data].[Date] DESC"
Dim adpS As SqlDataAdapter
adpS = New SqlDataAdapter(sqlS, connection)
builder = New SqlCommandBuilder(adpS)
Dim dTable As New DataTable
bsource.DataSource = dTable
bsource.EndEdit()
adpS.Fill(dTable)
connection.Close()
DataGridView1.DataSource = dTable
End Sub
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
DataGridView1.EndEdit()
Dim dt As DataTable
dt = TryCast(DataGridView1.DataSource, DataTable)
Dim x As Integer = 0
If dt.GetChanges() Is Nothing Then
MessageBox.Show("The table contains no changes to save.")
Else
Dim builder = New SqlCommandBuilder(adpS)
Dim rowsAffected As Integer = adpS.Update(dt)
If rowsAffected = 0 Then
MessageBox.Show("No rows were affected by the save operation.")
Else
MessageBox.Show(rowsAffected & " rows were affected by the save operation.")
End If
End If
End Sub
End Class
Any help would be appreciated.
This is for SQL Server, right. Try it like this.
Imports System.Data.SqlClient
Public Class Form1
Dim sCommand As SqlCommand
Dim sAdapter As SqlDataAdapter
Dim sBuilder As SqlCommandBuilder
Dim sDs As DataSet
Dim sTable As DataTable
Private Sub load_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_btn.Click
Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
Dim sql As String = "SELECT * FROM Stores"
Dim connection As New SqlConnection(connectionString)
connection.Open()
sCommand = New SqlCommand(sql, connection)
sAdapter = New SqlDataAdapter(sCommand)
sBuilder = New SqlCommandBuilder(sAdapter)
sDs = New DataSet()
sAdapter.Fill(sDs, "Stores")
sTable = sDs.Tables("Stores")
connection.Close()
DataGridView1.DataSource = sDs.Tables("Stores")
DataGridView1.ReadOnly = True
save_btn.Enabled = False
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Private Sub new_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_btn.Click
DataGridView1.[ReadOnly] = False
save_btn.Enabled = True
new_btn.Enabled = False
delete_btn.Enabled = False
End Sub
Private Sub delete_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete_btn.Click
If MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then
DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
sAdapter.Update(sTable)
End If
End Sub
Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
sAdapter.Update(sTable)
DataGridView1.[ReadOnly] = True
save_btn.Enabled = False
new_btn.Enabled = True
delete_btn.Enabled = True
End Sub
End Class
After two days working on this, I finally got it right for myself! Also, I had an eye on #ryguy72 codes as well. these are the steps you can take to get there:
Step 1: Drag and drop DataGridView into your form
Step 2: In the App.config add this between configuration:
<connectionStrings>
<add name="ehsanConnection" connectionString="Data Source=XXX
; User= XX; Password= XXX" ProviderName="System.Data.SqlClient"/>
</connectionStrings>
Step 3: The code below shows how you can get the DataGridView programmatically right, it worked perfectly fine for me.
Dim sCommand As SqlCommand
Dim sAdapter As SqlDataAdapter
Dim sBuilder As SqlCommandBuilder
Dim sDs As DataSet
Dim sTable As DataTable
Dim connStr As String =
ConfigurationManager.ConnectionStrings("ehsanConnection").ToString
Dim connStri = New SqlConnection(connStr)
Dim sql As String = "SELECT * FROM [Ehsan].[dbo].[Data]"
sCommand = New SqlCommand(sql, connStri)
sAdapter = New SqlDataAdapter(sCommand)
sBuilder = New SqlCommandBuilder(sAdapter)
sDs = New DataSet()
sAdapter.Fill(sDs, "Data")
sTable = sDs.Tables("Data")
connStri.Close()
DataGridView1.DataSource = sDs.Tables("Data")
The main point here was that I had to use [Ehsan].[dbo].[Data] not only the name of the table, "Data". Actually, it didn't work for me that way and it kept complaining!
Step 4: If you want to update your database after you changed some of the records in datagridview, use this code :
sAdapter.Update(sDs.Tables(0))
Main important point is that: "you have to set a primary key in your table first otherwise it won't work!"
How to pass the sql value to crystal report parameter? I stuck on here about a week. Please help me thanks all.
Dim header As New SqlCommand("SELECT SalesOrder.salesorder_id, SalesOrder.sales_code, Delivery.driver_code, SalesOrder.invoice_dt, Delivery.remark, SalesOrder.customer_icno, Delivery.c_name, Delivery.c_hpno, Delivery.address FROM SalesOrder,Delivery WHERE SalesOrder.salesorder_id = Delivery.salesorder_id AND SalesOrder.salesorder_id=" & checkInvoiceReceiptSalesOrderNo.Text, conn)
Using readerObj As SqlClient.SqlDataReader = header.ExecuteReader
While readerObj.Read
Dim paramFields As New ParameterFields()
Dim paramField As New ParameterField()
Dim discreteVal As New ParameterDiscreteValue()
paramField.ParameterFieldName = "son"
Dim str As String
str = readerObj("salesorder_id").ToString()
discreteVal.Value = str
paramField.CurrentValues.Add(discreteVal)
paramFields.Add(paramField)
End While
End Using
You this code. You can pass parameter value. Set your value here ParameterDiscreteValue.Value = "Your Parameter Value" and Set parameter name here ParameterFieldDefinition = ParameterFieldDefinitions("Parameter Name of Crystal Report")
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim reportViewer As ReportViewer = New ReportViewer()
Dim cryRpt As ReportDocument = New ReportDocument()
cryRpt = New CrystalReport1()
Dim ParameterFieldDefinitions As ParameterFieldDefinitions
Dim ParameterFieldDefinition As ParameterFieldDefinition
Dim ParameterValues As ParameterValues = New ParameterValues()
Dim ParameterDiscreteValue As ParameterDiscreteValue = New ParameterDiscreteValue()
ParameterDiscreteValue.Value = "Your Parameter Value"
ParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
ParameterFieldDefinition = ParameterFieldDefinitions("Parameter Name of Crystal Report")
ParameterValues = ParameterFieldDefinition.CurrentValues
ParameterValues.Clear()
ParameterValues.Add(ParameterDiscreteValue)
ParameterFieldDefinition.ApplyCurrentValues(ParameterValues)
reportViewer.crystalReportViewer1.ReportSource = cryRpt
reportViewer.crystalReportViewer1.Refresh()
reportViewer.ShowDialog()
End Sub
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
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))
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!!