crytsal reports in Asp.net

{ Posted on 12/29/2009 12:42:00 PM by Silvercode }
Tags :
Steps to create crystal reports in Asp.Net

Step1.
Create an aspx page :
Incase you need a date range add any, add in the optional parameters for the report like query by employee name,department name etc:


Query = "Select name,id from tablename where id=10"
Dim myConnection As New SqlConnection
myConnection.ConnectionString = Constr
Dim MyCommand As New SqlCommand
MyCommand.Connection = myConnection
MyCommand.CommandText = query
MyCommand.CommandType = CommandType.Text
Dim MyDA As New SqlDataAdapter
MyDA.SelectCommand = MyCommand
Dim myDS As New dsDaily
'Below tablename is the same name to be used in the dataset which we create
MyDA.Fill(myDS, "TABLENAME")
oRpt1.Load(AppPhyPath & "/folder\filename.rpt")
oRpt1.SetDataSource(myDS)
'Below is the code to create parameters in crystal reports
oRpt1.SetParameterValue("date1", ValDate1)
oRpt1.SetParameterValue("date2", ValDate2)
CrystalReportViewer1.ReportSource = oRpt1
CrystalReportViewer1.DataBind()
MyDA.Dispose()
myConnection.Close()
myConnection = Nothing


After adding the code above, create a dataset using the same query for what you have mentioned in the dataset(Not the schema one).Add in the query and get the fields in the dataset.

Once created add the same dataset in the crystal reports.
Below are the steps:
1)Create a blank report
2)Database fields -> create a new connection -> Project Data-> Ado.net datasets ->
choose your xsd file ie dataset file -> and click ok.
Once the dataset is added to the field explorer, drag and drop the fields as you require and design the same.Do formatting for the font, date etc...
3)About the crystal reports parameter, in the parameter fields -> add new -> create two date fields Date1, Date2 which is of datetype
Note: Parameter field Scenario is used when we are passing two date ranges choosed by the user and we need to display the same in the report ie .
Date from : 12/10/2009 Date to: 12/12/2009
4)Run your code and Voila the report works.....