Google How to create crystal reports in visual studio 2010 using asp.net | Asp.Net,C#.Net,VB.Net,MVC,jQuery,JavaScipt,AJAX,WCF,Sql Server example

How to create crystal reports in visual studio 2010 using asp.net

Introduction In this article i am going to explain step by step procedure to generate / create dynamic crystal report using visual studio 2010 in asp.net with C# and VB language.In previous articles i explained How to create and consume WCF Services in asp.net ? and Bind,Save,Edit,Update,Cancel,Delete,Paging example in GridView in asp.net C# and Bind and implement Custom paging in asp.net DataList control in 3 different ways and Bind state categories and cities sub categories in single dropdownlist and Implement searching in gridview and   How to upload, download and delete files from GridView in Asp.net
and How to bind, edit, update and delete data in Repeater in asp.net(C#, VB).

Description: Crystal reports were the part of the all the version of visual studio before the release of visual studio 2010. In Visual Studio 2010 Crystals reports are excluded i.e. it is no longer part of the visual studio package. But still it is available as a separate setup on the SAP website for free.
You can download the Crystal report setup from the SAP website using the link http://downloads.businessobjects.com/akdlm/cr4vs2010/CRforVS_13_0.exe
 
How to create crystal reports in visual studio 2010 in asp.net
click on image to enlarge
Implementation: Let's create an example to see it in action.

First create the database in Sql server and name it "Book_DB" and create a table in this database and name it "Book_Details" as shown in figure.


Note: Book_Id column is set to Primary key and Identity specification is set to yes with Identity increment and Identity seed equal to 1. Insert some data in this table that you want to show in the Crystal report.



Step 1: Open Visual Studio -> Go to File Menu of Visual Studio’s toolbar -> New -> Website -> Select Visual C# or Visual Basic from the left pane -> Select ASP.NET Empty Website and name it “CrystalReportDemo” or whatever you want as shown in figure below:
How to create crystal reports in visual studio 2010 in asp.net
Click on Image to enlarge

  Step 2: Go to website menu of the Visual studio toolbar -> Add New Item -> Select DataSet and name it “BooksDataSet.xsd” or whatever you want as shown in fig below.

How to create crystal reports in visual studio 2010 in asp.net
Click on image to enlarge

 Step 3: A pop up window will open asking for adding the BooksDataSet.xsd file in the App_Code folder as shown in fig below. Click Yes. 

How to create crystal reports in visual studio 2010 in asp.net
Click on Image to enlarge

  Step 4: BooksDataSet.xsd will be opened as shown in fig below.

How to create crystal reports in visual studio 2010 in asp.net
Click on Image to enlarge
 Step 5: Now it’s time to get the data from the Database and add the BooksDataSet.xsd dataset created.

Step 6:  So to to server explorer ->Right click on the Data Connections -> Add Connection as shown in figure below:

How to create crystal reports in visual studio 2010 in asp.net
Click on image to enlarge
  Step 7: A new window will open for adding connection. Enter your Server Name and Database name and click on Ok button as shown in Fig (a).We have used window authentication here.

How to create crystal reports in visual studio 2010 in asp.net
Fig (a) Click on Image to enlarge
   Note: If you are using Sql Server Authentication then follow Fig (b). else skip this


Fig (b) Click on Image to enlarge

   Step 8: Now expand your database from server explorer and drag the table Book_Details onto the BooksDataSet.xsd as shown in figure below:

How to create crystal reports in visual studio 2010 in asp.net
Click on image to enlarge
  Step 9: Now add crystal report in the project. Go to website menu -> Add New Item -> Select Crystal Reports and name it “BooksCrystalReport.rpt” or whatever you want as shown in figure below:
 
How to create crystal reports in visual studio 2010 in asp.net
Click on Image to enlarge

 Step 10: A window will open as shown in figure below. Click on Ok.

How to create crystal reports in visual studio 2010 in asp.net
Click on Image to enlarge

  Step 11: A new window will open.  Expand Project Data -> Expand ADO.NET DataSsts and move the Books_Details from the left pane to the right pane using > sign as shown in figure below. Click on Next button.

How to create crystal reports in visual studio 2010 in asp.net
Click on image to enlarge

Step 12: In the next window as shown below you can select the fields that you want to show on crystal reports. We are selecting all fields. So click on >> sign to select all the record to be displayed on crystal report. Click on Finish Button.

How to create crystal reports in visual studio 2010 in asp.net
Click on Image to enlarge

   Step 13: It will automatically add all the fields on the Crystal report created as shown in figure below. You can also drag and drop the fields from the field Explore as shown in left side ion the figure by expanding the Database Fields and drag and drop the columns in the Details section of the Crystal report.

How to create crystal reports in visual studio 2010 in asp.net
Click on Image  to enlarge

Step 14: Now Add a web page in the website. Go to Website menu -> Add new item - > web form e.g. (default.aspx) 

Step 15: Drag CrystalReportViewer control on to the default.aspx page from the Reporting category of the Visual Studio’s toolbox. And also a Button control from the standard category of the toolbox as:

  <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
        <asp:Button ID="btnGenerate" runat="server" Text="Generate Report"
            onclick="btnGenerate_Click" />

Note: If you creating this project in asp.net with Visual Basic language selected Drag CrystalReportViewer control on to the default.aspx page from the Reporting category of the Visual Studio’s toolbox. And also a Button control from the standard category of the toolbox as:

<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
        <asp:Button ID="btnGenerate" runat="server" Text="Generate Report" />

Step 16: Now in the code behind file (.default.aspx.cs) write the code on Generate Report Button’s click event as:

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 btnGenerate_Click(object sender, EventArgs e)
    {
        //if using Window Authentication then create the connectionstring as:
        SqlConnection conStr = new SqlConnection("Data Source=MyServer;Initial Catalog=Books_DB;Integrated Security=True;");
        // if using Sql server authentication then create the connectionstring as
        //SqlConnection conStr = new SqlConnection("Server=MyServer;uid=sa;pwd=nothing;Database=Books_DB;");
        SqlCommand cmd = new SqlCommand("Select * From Book_Details", conStr);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);    
        DataSet ds = new DataSet();
        adp.Fill(ds, "Book_Details");
 ReportDocument BooksReport = new ReportDocument();
        BooksReport.Load(Server.MapPath("BooksCrystalReport.rpt"));
        BooksReport.SetDataSource(ds.Tables["Book_Details"]);
        CrystalReportViewer1.ReportSource = BooksReport;
        CrystalReportViewer1.DataBind();
    }
}
 
Note: If you are creating the project in asp.net with Visual language selected then in code behind file (.aspx.vb) write the code on Generate button’s click events as:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub btnGenerate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
        'if using Window Authentication then create the connectionstring as:
        Dim conStr As New SqlConnection("Data Source=MyServer;Initial Catalog=Books_DB;Integrated Security=True;")
        ' if using Sql server authentication then create the connectionstring as
        'SqlConnection conStr = new SqlConnection("Server=MyServer;uid=sa;pwd=nothing;Database=Books_DB;");
        Dim cmd As New SqlCommand("Select * From Book_Details", conStr)
        Dim adp As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        adp.Fill(ds, "Book_Details")
 Dim BooksReport As New ReportDocument()
        BooksReport.Load(Server.MapPath("BooksCrystalReport.rpt"))
        BooksReport.SetDataSource(ds.Tables("Book_Details"))
        CrystalReportViewer1.ReportSource = BooksReport
        CrystalReportViewer1.DataBind()
    End Sub
End Class

Step 17: Now run the web application and click on Generate Report button. It will generate the Crystal reports like shown in below figure:

How to create crystal reports in visual studio 2010 in asp.net
Click on image to enlarge
Now over to you: 
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates." 

10 comments :

  1. Replies
    1. your welcome Edwin.Stay tuned for more updates

      Delete
  2. I love the step by step approach, u can't get lost. Especially for the weak coders.

    ReplyDelete
    Replies
    1. Thanks Gloria for appreciating the article..stay tuned and stay connected..

      Delete
  3. but unable to print and view in asp.net after packaged a programme and run it in client machine. How can be implemented?

    ReplyDelete
  4. Very Comprehensive approach. I am really glad to read it.

    ReplyDelete
  5. thank u but need to print the same generated report from top menu click on the crystal report viewer directly...

    ReplyDelete
    Replies
    1. Hi,crystal report viewer provides a print option in the top by default..

      Delete

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..