Link to home
Start Free TrialLog in
Avatar of nduguyettu
nduguyettu

asked on

Step-By-Step Method of creating Parameters in Crystal Reports 9 in ASP.NET 1.1 Visual Basic

Dear all,

I am new to ASP.NET and am using ASP.NET 1.1 Visual Basic. I have created some reports using Crystal Reports 9. I have successfully designed reports without parameters. I have also designed reports with parameters for the Windows applications. I have however failed to know how to design parameters for the ASP.NET 1.1 application.

May someone give me a step-by-step method of how I can design such reports.

Secondly I wish also to Export such reports to different formats such as pdf, doc, htm, etc. Please give steps too.

Nduguyettu
Avatar of Deathrace
Deathrace
Flag of India image

Avatar of nduguyettu
nduguyettu

ASKER

Thanks for the link but I ve not been helped. I ve copied the code but when I execute I just get a blank page. Can you give me more details about what should appear on the aspx page, what should appear in the Page_Load, etc.

As I told you am new to ASP.NET and Crystal Reports.


Nduguyettu

Try this....some predefined user control and simple to use

http://www.codeproject.com/KB/vb/CrystalContrl.aspx
Thanks a lot but the link you referred me to is for a Windows Application not ASP.NET which am interested in.

Nduguyettu
Avatar of Mike McCracken
Is it possible to be directed to a link that can give me simplified information similar to the attached file?


Nduguyettu


VBNET-Crystal-Reports-String-Par.doc
I managed to get some code that exports crystal reports to different formats which I incorporated into my application. The report displays well in the CrystalReportViewer but when I click the button to export, it returns the following error message.

Exception Details: CrystalDecisions.CrystalReports.Engine.LogOnException: Logon failed

Please advise me what I should do to solve the problem.

I have attached the entire code.

Nduguyettu


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BindReport()
    End Sub
    Sub BindReport()
        Dim con As New SqlClient.SqlConnection
        con.ConnectionString = "Server = localhost; database=ThreeWs; Trusted_Connection=yes"
        Dim com As New SqlClient.SqlCommand
        com.Connection = con
        com.CommandText = "Select * from GenTransactions" 'GenTransactions is a View
        com.CommandType = CommandType.Text
 
        Dim DA As New SqlClient.SqlDataAdapter
        DA.SelectCommand = com
 
        Dim DS As New xsdGenTransactions 'XML Schema for Dataset
 
        DA.Fill(DS, "GenTransactions")
        Dim Rpt As New rptGenTransactions
        Rpt.SetDataSource(DS)
 
        CrystalReportViewer1.DisplayGroupTree = False
 
        CrystalReportViewer1.ReportSource = Rpt
 
    End Sub
 
    Sub ExportReport()
        Dim oStream As New MemoryStream
        Dim Rpt As rptGenTransactions = New rptGenTransactions
        Dim diskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions
        Select Case DropDownList1.SelectedItem.Text 'this contains the value of the selected export format.
 
            Case "Rich Text (RTF)"
 
                oStream = Rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.WordForWindows)
                Response.Clear()
                Response.Buffer = True
                Response.ContentType = "application/rtf"
                '--------------------------------------------------------------------
            Case "Portable Document (PDF)"
 
                oStream = Rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
                Response.Clear()
                Response.Buffer = True
                Response.ContentType = "application/pdf"
                '--------------------------------------------------------------------
            Case "MS Word (DOC)"
 
                oStream = Rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.WordForWindows)
                Response.Clear()
                Response.Buffer = True
                Response.ContentType = "application/doc"
                '--------------------------------------------------------------------
            Case "MS Excel (XLS)"
 
                oStream = Rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel)
                Response.Clear()
                Response.Buffer = True
                Response.ContentType = "application/vnd.ms-excel"
                '--------------------------------------------------------------------
        End Select 'export format
        Try
            Response.BinaryWrite(oStream.ToArray())
            Response.End()
        Catch err As Exception
            Response.Write("<BR>")
            Response.Write(err.Message.ToString)
        End Try
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ExportReport()
    End Sub

Open in new window

SInce you have started a new report you need to set the data source just as you do for the viewer.

mmlmcc
Dear mmlmcc,

May you show me in the above code where I should place the data source as you suggest.

Thanks,

Nduguyettu
I can't be sure since I have never written one in .Net

Probably before the SELECT statement

mlmcc
Can someone find a problem with procedure ExportReport. It is the one returning the error.

Nduguyettu

There is no problem with ExportReport.  It is in the way you are setting it up.

mlmcc
How should I set it up? This is my problem. I am failing to see the problem.

Nduguyettu
Thanks mlmcc

The first link you referred to me is for Windows based applications where I have no problem.

The second link leads me to VS2005. Though am using VS2003 I will read through the document and see if it can help me.


Nduguyettu
Hello mlmcc,

I read through the pdf document you referred to me but I discovered that it for a later version of CrystalReportViewer for VS2005. I am using VS2003.

Nduguyettu
The basic ideas should be the same.

mlmcc
In the document they refer to the ReportExporter Control which does not exist in VS 2003, so the basic idea is different.

Nduguyettu
Did you look at the code methods starting on page 191?

mlmcc
I think I need someone to look at the above code and tell me where the problem arises that causes the Exception. Otherwise I ve read the book and failed to find a solution. If not I wish I could get a book specifically designed for Visual Studio 2003.

Nduguyettu
I have managed to get a solution to the part of the question regarding Exporting to other formats. Through continued searching I came across the attached code which solved the problem.

What I was really missing in my code which kept on returning the Exception was the DataSource.

Nduguyettu
The following Code shows how to convert a Crystal Report to MS Word.
 
'First Create a crystal Report.
'Drag and drop the Crystal Report Viewer in the form
'copy this code on top of the code
 
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared       
 
Private myDS As New Dataset1() ' Dataset you created.
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        
    Dim rpt As New CrystalReport1() 'The report you created.        
    Dim myConnection As SqlConnection        
    Dim MyCommand As New SqlCommand()        
    Dim myDA As New SqlDataAdapter()        
    Try            
        myConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI; Initial       Catalog=pubs;")            
        MyCommand.Connection = myConnection            
        MyCommand.CommandText = "SELECT * FROM authors"            
        MyCommand.CommandType = CommandType.Text            
        myDA.SelectCommand = MyCommand            
        myDA.Fill(myDS, "authors")            
        rpt.SetDataSource(myDS)            
        CrystalReportViewer1.ReportSource = rpt        
        Catch Excep As Exception            
              MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)        
       End Try    
End Sub   
 
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click        
 'This Code is used to Convert to word document        
       Dim reportWord As New CrystalReport1() ' Report Name         
       Dim strExportFile As String = "d:\TestWord.doc"        
       reportWord.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile        
       reportWord.ExportOptions.ExportFormatType = ExportFormatType.WordForWindows        
       Dim objOptions As DiskFileDestinationOptions = New DiskFileDestinationOptions()        
       objOptions.DiskFileName = strExportFile        
       reportWord.ExportOptions.DestinationOptions = objOptions        
       reportWord.SetDataSource(myDS)        
       reportWord.Export()        
       objOptions = Nothing        
       reportWord = Nothing    
End Sub

Open in new window

I have attached here some code which when used to display a report after selecting a parameter from the Dropdown list, a blank screen is displyed.

I have also attached a screen shot for the blank screen

Nduguyettu


 Private Sub ReportPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReportPreview.Click
        Dim Rpt As rptSelectedAccount = New rptSelectedAccount
        Dim DS As New xsdGenTransactions
        '=================================================
        Dim myConnection As SqlConnection
        Dim MyCommand As New SqlCommand
        Dim myDA As New SqlDataAdapter
 
 
        myConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI; Initial Catalog=ThreeWs;")
        MyCommand.Connection = myConnection
        MyCommand.CommandText = "SELECT * FROM GenTransactions"
        MyCommand.CommandType = CommandType.Text
        myDA.SelectCommand = MyCommand
        myDA.Fill(DS, "GenTransactions")
 
        Rpt.SetDataSource(DS)
        '============================
 
 
        Dim crParameterDiscreteValue As ParameterDiscreteValue
 
        Dim crParameterFieldDefinitions As ParameterFieldDefinitions
 
        Dim crParameterFieldLocation As ParameterFieldDefinition
 
        Dim crParameterValues As ParameterValues
 
        ' Get the report parameters collection. 
 
        crParameterFieldDefinitions = Rpt.DataDefinition.ParameterFields
 
        ' Add a parameter value - START
 
        crParameterFieldLocation = crParameterFieldDefinitions.Item("AccountName")
 
        crParameterValues = crParameterFieldLocation.CurrentValues
 
        crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
 
        crParameterDiscreteValue.Value = cboAccount.SelectedItem.Text
 
        crParameterValues.Add(crParameterDiscreteValue)
 
        crParameterFieldLocation.ApplyCurrentValues(crParameterValues)
 
 
        ' Add a parameter value - END
     
 
        ' Set report's DataSource. Skip this step if your report is calling a 
        ' stored procedure direct in the report.
        
        
        CrystalReportViewer1.DisplayGroupTree = False
 
        ' Set CrystalReportViewer.ReportSource
 
        CrystalReportViewer1.ReportSource = Rpt
        CrystalReportViewer1.RefreshReport()
    End Sub

Open in new window

Screen3.bmp
My last comment has not been responded to, am still waiting for some comments.
RefreshReport generally dumps the existing parameters.  Try using ViewReport

mlmcc
May you give me more information on how to use ViewReport because it is not an option under CrystalReportsViewer.

Nduguyettu
I made a small modification that resulted into some output. I included a textbox on the web form into which I type a parameter value. On clicking the preview button the parameter information is displayed on the screen.

The code below is similar to the above code only that I replaced cboAccount.SelectedItem.Text with txtAccount.Text.

The question is why the information cannot be displayed using the dropdown list. It may not be possible for the user to know which parameter name one may need to use.

Nduguyettu
crParameterFieldLocation = crParameterFieldDefinitions.Item("AccountName")
 
        crParameterValues = crParameterFieldLocation.CurrentValues
 
        crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
 
        crParameterDiscreteValue.Value = txtAccount.Text

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nduguyettu
nduguyettu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial