In this article I am going to describe you that how can you embed(attach) a crystal report in ASP.NET using VB.NET as a language. To embed a crystal report first you have to select a crystalreportviewer from Toolbox in ASP.NET environment and then you have to right click on the ASP.NET page and click on View Code option. When the view code page will open you will insert the following code.
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnReportGenerate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReportGenerate.Click
Try
Dim path As String
path = Server.MapPath(Request.ApplicationPath)
Dim report As New ReportDocument
report.Load(path + "\zreport.rpt")
report.SetDatabaseLogon("sa", "52xMAX")
CrystalReportViewer1.ReportSource = report
Response.Write("report launch successfully")
Catch ex As Exception
Response.Write("Error: " & Err.Description)
End Try
End Sub
End Class
Further if you want to add a parameter to the crystal report so that only the desired data will be print then you have to use this code. But Before that you have to insert text boxes on your ASP.NET so that you will pass the selective data to the crystal report. I am writing here the VB.NET Code, you have to insert text box by your self.
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
crystalreportshow.Visible = False
ElseIf IsNothing(txtID.Text) Then
crystalreportshow.Visible = False
ElseIf Not IsNumeric(txtID.Text) Then
crystalreportshow.Visible = False
End If
End Sub
Protected Sub btnReportGenerate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReportGenerate.Click
Try
If IsNothing(txtID.Text) Then
Response.Write("Id must be filled")
ElseIf Not IsNumeric(txtID.Text) Then
Response.Write("ID must be in numeric")
Else
crystalreportshow.Visible = True
phase1.Visible = False
Dim path As String
path = Server.MapPath(Request.ApplicationPath)
Dim report As New ReportDocument
report.Load(path + "\zreport.rpt")
report.SetDatabaseLogon("sa", "52xMAX")
Dim crParameterFieldDefinitions As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinitions = report.DataDefinition.ParameterFields
Dim crParameter1 As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition = crParameterFieldDefinitions.Item("id")
Dim crparameter1value As CrystalDecisions.Shared.ParameterValues = crParameter1.CurrentValues
Dim disval As New CrystalDecisions.Shared.ParameterDiscreteValue
disval.Value = txtID.Text
crparameter1value.Add(disval)
crParameter1.ApplyCurrentValues(crparameter1value)
CrystalReportViewer1.ReportSource = report
Response.Write("report launch successfully")
End If
Catch ex As Exception
Response.Write("Error: " & Err.Description)
End Try
End Sub
End Class
In this way you can insert as many parameter as you can. Remember that parameter will select there data type as you pass the data to the report.
I hope that this will help you.
I have created this blog to place some of important things on it for my personal use and also to help others. Now a days I am doing job as Software Engineer in a company. I have completed my graduation in Computer Science from University of Sindh, Jamshoro, Pakistan.
Subscribe to:
Post Comments (Atom)
Docker Tutorial
I have listed all the necessary commands of Docker below. In case if any is missing or if any improvement required, please share in comment...
-
In my case, this error pertains to Oracle Merge query . After 30 minutes of time wastage and a lot of hunting on different forums, I come to...
-
Above problem occur when you have not added a group (of which User try to login) in your local policy login. To resolve the issue, follow t...
-
Dear Readers, I face this problem in a web application build in .aspx. It mainly occurs when you are performing some action with javascrip...
No comments:
Post a Comment