We will first download SAP Crystal Reports from the source and install it on our system, then using some simple code we will proceed.
Initial chamber
First we need to download the SAP Crystal Reports for respective IDE’s of Visual Studio as in the following:
Step 1
Open Your Visual Studio 2010 and create an Empty Website, provide a suitable name (crystalrpt_demo).
Step 2
In Solution Explorer you get your empty website and then add a Web Form and SQL Server Database and Dataset as in the following:
For Web Form
crystalrpt_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it crystalrpt_demo.aspx.
For SQL Server Database
crystalrpt_demo (your empty website) then right-click then right-click then seelctAdd New Item -> SQL Server Database. (Add the database inside the App_Data_folder).
For DataSet
crystalrpt_demo (your empty website) then right-click then right-click then seelct Add New Item -> DataSet (add the DataSet inside the App_Code_folder).
Database chamber
Step 3
In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table then make the table like this:
- Table ->tbl_data (Don’t forget to make ID as IS Identity -- True).
Insert some data into your table by going to your table tbl_data then right-click then select Show tbl_data -> Add data in the table.
- Stored Procedure – > sp_select:
Double-click on the dataset that is resides under the App_code_Folder, see the following Image.
When you double-click the dataset you get the blank area, there you need to do something like the following.
Right-click on the blank area then seelct Add -> Datatable.
Change the name of the table from Datatable1 to Newtbl_data. There you must add 3 columns for ID, Name and City as we did in tbl_data, so add the three columns. You will get something as in the following image.
You must then go to crystalrpt_demo then right-click then select Add New Item -> Find Crystal Report.rpt file.
When you add this new Crystal Report Gallery opened there you need to do as in the following image.
You will have something like this:
When your Crystal Reports report is opened as in the preceding image, you will find in the left or right dock Field Explorer is opened.
Right-click on Field Explorer then select Database Expert -> Create New Connection. You will need to find the table that we made in the Dataset, Newtbl_data.
Once you have your table, add that table to the right side pane using the “ >>” button then press OK as in the following image.
You will get you Newtbl_data in Field Explorer like this.
Here you must add these ID, Name and City from Database Fields to the Crystal Reports report, Section 3 Page Details. You can drag these fields to the Crystal Report Page Details.
Design chamber
Step 4
Now open your crystalrpt_demo.aspx file, where we create our design for Crystal Reports. We will add a Crystal Reports Viewer here.
Go to the toolbox and find Reporting then Drag and Drop a Crystal Reports Viewer.
crystalrpt_demo.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
-
- <%@ Register assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
-
- <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
- AutoDataBind="true" />
-
- </div>
- </form>
- </body>
- </html>
Code chamber
Step 5
Open your crystalrpt_demo.aspx.cs and write some code so that the application works.
crystalrpt_demo.aspx.cs
Add some namespaces to the crystalrpt_demo.aspx.cs page as in the following:
- 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 CrystalDecisions.CrystalReports.Engine;
- using System.Data.SqlClient;
-
- public partial class _Default : System.Web.UI.Page
- {
- ReportDocument rprt = new ReportDocument();
-
- protected void Page_Load(object sender, EventArgs e)
- {
- rprt.Load(Server.MapPath("~/CrystalReport.rpt"));
-
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("sp_select", con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- sda.Fill(ds, "Newtbl_data");
- rprt.SetDataSource(ds);
- CrystalReportViewer1.ReportSource = rprt;
-
-
- }
- }
Output chamber I hope you like this. Thank you for reading. Have a good day.