SIGN UP MEMBER LOGIN:    
ARTICLE

Create a Report Using Crystal Report in Visual Studio 2010

Posted by Manish Dwivedi Articles | Crystal Reports C# April 13, 2011
In this article, you will learn how to show a report using Crystal Reports in Visual Studio 2010.
Reader Level:

Create a web site in VS 2010 as in the following:

Image1.jpg

Then we will create a DataSet. For this, right click on the project name in Solution Explorer and click Add New Item. The Add New Item Dialog will open. Select DataSet from the list of items. Give it's name as per your choice  and click Add.

Image2.jpg

Now we will add the table to the DataSet. On the Server Explorer right click Data Connections and then click Add Connection.

Image3.jpg

Enter your server name and select the Database.

Image4.jpg

After that drag a table to the DataSet designer and save it.

Image5.jpg

Now right click on the project in Solution Explorer and select Add New Item. Select Crystal Report from the list and add it as CustomerReport.

Image6.jpg 

The Crystal Report Gallery will open. Select Blank Report and click OK.

Image7.jpg

Now in the field explorer window right click on Database Fields and click Database Expert.

Image8.jpg


Click the plus sign of Project Data, then ADO.NET DataSets. Select Customers under Customer. Click the add (>) button to add the table to our report and click OK.

Image9.jpg


You will get all the fields of the Customers table under the Customers option under Database Fields in Field Explorer. Drag and drop the required fields in the Section 3 (Details) part of the report and save it.

Image10.jpg


Finally drag CrystalReportViewer control from the Reporting section of the Toolbox to our webpage. 

Image11.jpg

Now go to the code behind page and write the following code in the Page_Load event.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using CrystalDecisions.CrystalReports.Engine;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        SqlConnection connection = new SqlConnection("Server=myserver;uid=sa;pwd=nothing;Database=MyDB;");

        SqlCommand command = new SqlCommand("Select top 10 * From Customers", connection);

        SqlDataAdapter adapter = new SqlDataAdapter(command);

        //Customer _Customer = new Customer();

        DataSet dataset = new DataSet();

        adapter.Fill(dataset, "Customer");

        ReportDocument CustomerReport = new ReportDocument();

        CustomerReport.Load(Server.MapPath("CustomerReport.rpt"));

        CustomerReport.SetDataSource(dataset.Tables["Customer"]);

        CrystalReportViewer1.ReportSource = CustomerReport;

        CrystalReportViewer1.DataBind();

    }

}


Now it's done. Press F5; you will get the report.

Image12.jpg 

Login to add your contents and source code to this article
Article Extensions
Contents added by Nikhil Atewar on Mar 03, 2012
share this article :
post comment
 

Nicely Explained Manish..

Posted by mohan kumar Apr 23, 2012

very nice manish

Posted by ashlu shaju Apr 02, 2012

very nice manish

Posted by ashlu shaju Apr 02, 2012

Nice article above. Please advise on what approach i can use to generate reports in VS c#express 2010

Posted by CsharpUser Mar 14, 2012

Above code is very useful , however can you help to generate crystal report in visual studio 2010 WPF application ?

Posted by Vishal Shah Jan 27, 2012
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor