Sunday 14 July 2013

Crystal Report in Asp.net

Making Of Crystal report

-Joseph Paul Moonjely
Introduction: 

In this article I will explain how to create crystal reports example in asp.net.


Description: 

In Previous posts I explained 
how to install crystal reports in visual studio 2010 and how to create rdlc reports using asp.net and pass parameters to rdlc reports using asp.net. Now I will explain how to create basic crystal reports using asp.net. Crystal Report is standard reporting tool for visual studio by using these we can display reports regarding employee details and display charts etc and crystal reports need minimal coding to display result. 

To implement crystal reports first design the table in database and give name UserInfomation

ColumnName
DataType
UserId
Int(set identity property=true)
UserName
varchar(50)
FirstName
Varchar(50)
LastName
varchar(50)
Location
varchar(50)

After completion of table creation enter some dummy data because we need to use that data to populate reports.

Now Open visual studio and create new website after that right click on your website and select Add new item in that select Crystal Report and click Add
 
After that add crystal report then it will prompt Crystal Report Gallery window in that select blank solution and click OK

A blank report will create in our application now click on CrystalReports menu under that select Database under that select Database Expert
 
After click on Database Expert now Database Expert wizard will open in that select Create New Section>> select OLE DB (ADO) >> in that click on + sign of OLE DB (ADO)

Now select Microsoft OLE DB Provider for SQL Server and click Next (Here we can select SQL Native client option also but sometimes during deployment if servers not contains this native client it will throw error).
 
Now enter SQL Server name, username, password and required database and click Next

After enter credentials for your required database click Next then click Finish (Here for my database I didn’t set any credentials for that reason I didn’t enter userid and password details don’t get confused).

After click Finish now our database loaded in OLEDB (ADO) section >> select your database >> select dbo >> select required tables

Now open tables in that select required table and move to selected tables section and click OK

After that Database Fields in Field Explorer populated with our required data table now drag and drop the required fields from data table to reports Details section

Now open your Default.aspx page drag and drop CrystalReportViewer control from Reporting tab.


Now select CrystalReportViewer and click on smart tag in right hand side and Choose new Report Source

Whenever we click on New report source one window will open in that select crystal report for Report Source from the available reports in dropdownlist and click OK.

After assign available report to CrystalReportViewer control check your code that would be like this

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Crystal Report Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True"ReportSourceID="CrystalReportSource1" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="CrystalReport.rpt">
</Report>
</CR:CrystalReportSource>
</div>
</form>
</body>
</html>
Now run your application your report will be like this

In case your report prompt window for UserName and password before we access data in that situation we need to set those details in code behind instead of assign crystal report to CrystalReportViewer control

Drag and drop CrystalReportViewer control click on right side smart tag of your CrystalReportViewer control and uncheck EnableDatabaseLogonPrompt

Our aspx code will be like this

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Crystal Report Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True"ReportSourceID="CrystalReportSource1" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="CrystalReport.rpt">
</Report>
</CR:CrystalReportSource>
</div>
</form>
</body>
</html>
Now Open your code behind file and set database connection settings and assign reports to the control before that first add following namespaces


using System;
using CrystalDecisions.CrystalReports.Engine;
After add namespaces write the following code in page load event

C# code


protected void Page_Load(object sender, EventArgs e)
{
ReportDocument reportdocument = new ReportDocument();
reportdocument.Load(Server.MapPath("CrystalReport.rpt"));
reportdocument.SetDatabaseLogon("username","password","SureshDasari","MySampleDB");
CrystalReportViewer1.ReportSource = reportdocument;
}
VB.NET Code


Imports CrystalDecisions.CrystalReports.Engine

Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgsHandles Me.Load
Dim reportdocument As New ReportDocument()
reportdocument.Load(Server.MapPath("CrystalReport.rpt"))
reportdocument.SetDatabaseLogon("""""SureshDasari""MySampleDB")
CrystalReportViewer1.ReportSource = reportdocument
End Sub
End Class
Now run your application and check your output that would be like this

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. thanx
    very useful

    to know why Android Apps become popular (10 reasons) please visit my blog
    techadicted.blogspot.com

    ReplyDelete