Discussion:
How to dynamicly set datasource
(too old to reply)
Scooby Dog
2010-03-10 22:26:56 UTC
Permalink
Could someone help: I have a report that I have created using VB.net 2008
and a stored procedure. When I created the report I used the Database
Expert and connected to the database and then the stored proc. Is there a
way to set this at runtime?


Thanks

Dave.




Here is the web page that I use to display the report
----------------------------------------------------

<body>
<form id="form1" runat="server">

<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
AutoDataBind="true" ReportSourceID="CrystalReportSource1"/>
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="ActiveInq.rpt">
</Report>
</CR:CrystalReportSource>
</div>
</form>
</body>

Here is the code behind to populate the report:
-----------------------------------------------

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Partial Class PrintActiveInquiry
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
' Load the ID of the Inquiry.
Dim field1 As ParameterField =
Me.CrystalReportViewer1.ParameterFieldInfo(0)
Me.CrystalReportViewer1.BorderWidth = 100
Me.CrystalReportViewer1.HasCrystalLogo = False
Dim val1 As New ParameterDiscreteValue()
Dim ID As Integer = CType(Request.QueryString("ID"), Integer)

val1.Value = ID
field1.CurrentValues.Add(val1)
End Sub
Scooby Dog
2010-03-11 17:20:53 UTC
Permalink
I just wanted to let anyone who reads my previous post that I have figured
out how to do this.

If anyone cares here's how you do it: In my case I use a Stored Proc to
base the report on.

'Steps to Dynamicly create and load a Crystal Report
' 1. Add a dataset to the web site project
' 2. Double click new dataset and then drag table or stored proc
onto dataset.
' This is an important step because Crystal will not display the
data unless
' the dataset used as the data source is the same as the one to
create the report.
' 3. Create new Crystal Report. Go to the Database Expert and click
on 'Create New Connection'
' click on ADO.net and browse to the newly added dataset.xsd, now
design your report with
' the displayed fields.
' 4. For the page theatyour going to display the report on, simply
add a 'CrystalReportViewer'
' 5. Use code below and your good to go!

Try
Dim conStr As String =
System.Configuration.ConfigurationManager.AppSettings("dbconnection")
Dim strReportPath As String =
Server.MapPath("~/Reports/MyReport.rpt")

Dim sqlConnection As New SqlConnection(conStr)
Dim Command As SqlCommand = New SqlCommand()
Command.Connection = sqlConnection
Command.CommandText = "usp_GetInquiryByID"
Command.CommandType = CommandType.StoredProcedure

Dim Parameter As SqlParameter = New SqlParameter("@intInqID",
21))
Parameter.Direction = ParameterDirection.Input
Parameter.DbType = DbType.Int16

Command.Parameters.Add(Parameter)

Dim Adapter As SqlDataAdapter = New SqlDataAdapter(Command)
Dim ds As New dsInquiry ' thid is the dataset that I added to
my web site project.
Adapter.Fill(ds, "Inquirys")

Dim cr As New ReportDocument

cr.Load(strReportPath)
cr.SetDataSource(ds.Tables("Inquirys"))
CrystalReportViewer1.ReportSource = cr
Catch ex As Exception
Util.LogError("Error Generating Report:" & "
***PrintActiveInquiry.aspx*** " & ex.Message & " " & ex.StackTrace.ToString,
Util.REPORT_MSG)
End Try

__________________________________________________
Post by Scooby Dog
Could someone help: I have a report that I have created using VB.net 2008
and a stored procedure. When I created the report I used the Database
Expert and connected to the database and then the stored proc. Is there
a way to set this at runtime?
Thanks
Dave.
Here is the web page that I use to display the report
----------------------------------------------------
<body>
<form id="form1" runat="server">
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
AutoDataBind="true" ReportSourceID="CrystalReportSource1"/>
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="ActiveInq.rpt">
</Report>
</CR:CrystalReportSource>
</div>
</form>
</body>
-----------------------------------------------
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Partial Class PrintActiveInquiry
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
' Load the ID of the Inquiry.
Dim field1 As ParameterField =
Me.CrystalReportViewer1.ParameterFieldInfo(0)
Me.CrystalReportViewer1.BorderWidth = 100
Me.CrystalReportViewer1.HasCrystalLogo = False
Dim val1 As New ParameterDiscreteValue()
Dim ID As Integer = CType(Request.QueryString("ID"), Integer)
val1.Value = ID
field1.CurrentValues.Add(val1)
End Sub
Loading...