Working With Crystal Report using ASP.NET (C#) and SQL Server 2005 or above


Step: 1 Open visual studio 2005-08
Step: 2 Create website name MyReport
Step: 3 In your Default.aspx page write below code
                <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="CR" Namespace="CrystalDecisions.Web" Assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" %>
 DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>My Crystal Reporttitle>
head>
<body>
    <form id="frmCReport" runat="server">
    <div>
        <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True"
            Height="1114px" Width="886px" ReportSourceID="CrystalReportSource1" />
        <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
            <Report FileName="CrystalReport1.rpt">
            Report>
        CR:CrystalReportSource>
 
    div>
    <div><asp:Label ID="lblMessage" runat="server">asp:Label>div>
    form>
body>
html>

Step: 4 in your Default.aspx.cs (code behind) page write below code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;       //nameSpace for Crystal Report

public partial class _Default : System.Web.UI.Page
{
    ReportDocument rptDoc = new ReportDocument();   //Report Document

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadReport();   //Load Report
    }

    protected void LoadReport()
    {
        try
        {

            SqlConnection dbConn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connString"]);
            string strQry = "SELECT * FROM MyTable";
            dbConn.Open();
            SqlCommand cmd = new SqlCommand(strQry, dbConn);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataTable dtGetData = new DataTable();
            da.Fill(dtGetData);
            string strReportPath = Server.MapPath("CrystalReport1.rpt");
            rptDoc.Load(strReportPath);
            rptDoc.SetDatabaseLogon("userId", "password", "yourServerName", "DataBaseName", false);     //if crystal report prompt login credentials for your database server
            rptDoc.VerifyDatabase();
            rptDoc.SetDataSource(dtGetData);
            CrystalReportViewer1.ReportSource = rptDoc;
            dtGetData.Dispose();
            da.Dispose();
            dbConn.Close();
        }
        catch (Exception ex)
        {
            lblMessage.Text = "Error -" + ex.Message;
        }
    }
}
Step: 5 create your Crystal Report File CrystalReport1.rpt name
Step: 6 go to Field Explorer right side of crystal report viewer.
Step: 7 Select Database Expert and choose Create New Connection tab select OLE DB (ADO)
Step: 8 select in OLE DB (ADO), Microsoft OLEDB Provider for SQL Server
Step: 9 finish the login and database selection step
Step: 10 come again to Database Fields; there is table which you have selected
Step: 11 manage header and footer of your report accordingly
Step: 12 come to data display point, drag the field name inside your Section 3 (Details) from your table under your Database Fields
Step: 13 run your application, you have finished

Comments