Wednesday 21 August 2013

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

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



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 linkhttp://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

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_Detailsonto 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 ObjectByVal 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

No comments:

Post a Comment