如何将参数从 vb.net 代码传递给水晶报表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11322735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass parameters to crystal report from vb.net code
提问by Gunner
I have created a crystal report (cross tab). I'm not using any dataset, instead I used the wizard in crystal report to call an procedure from my Database schema (Provider given is Microsoft OLEDB provider for oracle, after which I gave my DB credentials(i.e. schema, username, password) and selected the procedure and selected the columns I wanted to display in the report).
我创建了一个水晶报表(交叉表)。我没有使用任何数据集,而是使用 Crystal Report 中的向导从我的数据库架构中调用一个过程(提供的提供程序是 Oracle 的 Microsoft OLEDB 提供程序,之后我提供了我的数据库凭据(即架构、用户名、密码)和选择了程序并选择了我想在报告中显示的列)。
There are 5 parameters that I need to pass it from the front end to generate the report. While viewing the crystal report preview, by giving parameters, the report works fine.
我需要从前端传递 5 个参数来生成报告。在查看水晶报表预览时,通过给出参数,报表工作正常。
Now i want to pass these 5 parameters from the front end(vb.net) to show the report in the CrystalReportViewer. Please suggest the code to write in aspx.vb file. (PS:- I did go through other forums and found out some code, but all of them were giving some or the other error, so am posting one so that i can get the code specific to my requirement).
现在我想从前端(vb.net)传递这 5 个参数以在 CrystalReportViewer 中显示报告。请建议在 aspx.vb 文件中编写的代码。(PS:-我确实浏览了其他论坛并找到了一些代码,但他们都给出了一些或其他错误,所以我发布了一个,以便我可以获得特定于我的要求的代码)。
Thanks in advance..
提前致谢..
回答by Gunner
I have gotten the report to work... I wrote the code below:
我已经得到了报告的工作......我写了下面的代码:
Dim RptDocument As New ReportDocument
RptDocument.Load(Server.MapPath("rpt\Report.rpt"))
RptDocument.SetParameterValue("param1", Session("param1"))
RptDocument.SetParameterValue("param2", ddlparam2.SelectedValue)
RptDocument.SetParameterValue("param3", param3.text)
RptDocument.SetParameterValue("param4", param4.text)
RptDocument.SetParameterValue("param5", param5.text)
'Set login info
Dim myLogin As CrystalDecisions.Shared.TableLogOnInfo
Dim myTable As Table
For Each myTable In RptDocument.Database.Tables
myLogin = myTable.LogOnInfo
myLogin.ConnectionInfo.ServerName = "server name"
myLogin.ConnectionInfo.DatabaseName = ""
myLogin.ConnectionInfo.UserID = "userid"
myLogin.ConnectionInfo.Password = "pwd"
myTable.ApplyLogOnInfo(myLogin)
myTable.Location = myTable.Location
CrystalReportViewer1.ReportSource = RptDocument
Created a System DNS and had to add Oracle.DataAccess.dll
to reference and a class file (with functions same as that in connectooracle.vb class file but with different name), also set up a connection in global.asax to refer to that class connection and using
Imports Oracle.DataAccess.Client
instead of Imports System.Data.OracleClient
(to avoid ambiguity)...
创建的系统DNS,不得不添加Oracle.DataAccess.dll
到基准和一个类文件(在connectooracle.vb类的文件,但有不同的名称相同的功能),还设置了在Global.asax中的连接来指代类连接,并使用
Imports Oracle.DataAccess.Client
替代的Imports System.Data.OracleClient
(为了避免歧义)...
This somehow made it work and there might be some other solution for that..:)
这以某种方式使它起作用,并且可能有其他一些解决方案..:)
(For ref:- Adding myLogin.ConnectionInfo.IntegratedSecurity = True gave me this error--
Logon failed. Error in File C:\DOCUME~1\Username\LOCALS~1\Temp\Report {1AG3DD86-141D-43YA-B6A2-AEDF3459AE49}.rpt: Unable to connect: incorrect log on parameters.)
回答by Androidz
This works for me and I'm using Visual Studio 2008 for this one since VS2010 doesn't have crystal engine for the reference.
这对我有用,因为 VS2010 没有用于参考的水晶引擎,所以我正在使用 Visual Studio 2008。
First, make sure you have imported these two:
首先,确保你已经导入了这两个:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Now, on my part I was using the odbcbecause as what I have noticed crystal report works fine with this and since we are working with odbc. So I did not include the login property on the report in my code. On the report just choose odbc connection.
现在,就我而言,我正在使用odbc,因为正如我所注意到的,Crystal Report 可以很好地处理这个问题,而且我们正在使用 odbc。所以我没有在我的代码中的报告中包含登录属性。在报告上只需选择 odbc 连接。
Private Sub ReportViewer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cryRpt As New ReportDocument
Dim str1 As String
Try
str1 = Title.str1
str2 =Title.str2
cryRpt.Load("c:\Program Files\Report\" & str2 & "")
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = strStore
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("Store")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
rptviewer.Cursor = Cursors.AppStarting
rptviewer.ReportSource = cryRpt
rptviewer.Refresh()
rptviewer.Cursor = Cursors.Default
Catch ex As Exception
MsgBox(ex.Message)
Me.Close()
ReportInterface.Show()
End Try
End Sub
I have a class that will get the data inputted by the user. But this will not work using stored procedure parameters.
我有一个类可以获取用户输入的数据。但这在使用存储过程参数时不起作用。
please mark as accepted if it works for you
如果它适合您,请标记为已接受
Thank You
谢谢你