Friday, 11 November 2011

Crystal Report Basic Sample

 
7 Easy Steps to get start with Crystal Report.


To Start With You Shold Have A Table In your Database.





Before I start I would like to recommend new kind of reporting which is which is currently a hot topic in IT industry, that is Power bi,and you can head start with it by viewing this popular video on power bi which can make you power bi professional in very simple stepsas below.




1)Open visual studio...then create website

2)Now open  toolbox,go on Reporting TAB,Add Crystal Report Viewer To Any Of Your Blank ASPX page,
(if only Pointer And MicrosoftViewer Is Present or If Crystal Report Is Not Existed In the Reporting TAB.Go through the VISUAL STUDIO SET UP AGAIN AND CHOOSE  ADD NEW ITEM instead of UPDATE OPTION THIS TIME and you'll see in the installation list crystal report is in the last!)



2)Now After You Drag The Crystal Report Viewer Control you'll get a control in your page with name 'CrystalReportViewer1')

Now right click to your solution add new item and select CrystalReport.this will add crystal report to  your solution (as you add webform or any file) wth name 'CrystalReport.rpt'

Now You Have
a Crystal Report (CrystalReport.rpt)
And
a Crystal Report Control (CrystalReportViewer1)In a Form Who Can Handle It.




3)Now When You Open Crystal Report  The Screen Will Look Some Thing Like This



 
4)Now You’ll Find Field Explorer side bar in right with solution Explorer As Shown open it and right click Database Field




5) After That A New Window Will open ‘DATABASE EXPERT’

Now Here  Is The Thing You Have To Go Smoothly.
Click ‘create new connection’ + sign
then + sign of OL EDB ADO



then a window will appear which wil ask you to set your database path...

make proper connection…
After that your database will get generate on right hand side pane.





Click > and transfer your database to 'selected table' box(on right hand side)
Click table name press ok

and you have done 90% of work!






6)
Now just Explore database field of field explorer(Field Explorer  Only Appears in CrytalReport) and drag coloumn name to the Crystal report .
wherever you want
(Coloum automaticly behave as they are bind in gridview or datalist)




Check the toolbox you have four tools to design.










7)
Write this code in form .cs file where the control exist

i)add these namespaces must


using System.IO;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Enterprise;
using CrystalDecisions.ReportSource;



ii)and continue to this as you understand

Now the basic thing you have to do is Binding Your Database Value to Crystal Report!
if You Understand the basic binding you can easily notice that i 've just binded values to crytal report!


public void NormalBinding()
{

SqlCommand sqlcmd = new SqlCommand("
select * from sample", con);SqlDataAdapter  da = new SqlDataAdapter(sqlcmd); 
DataTable dt = new DataTable();
dt.Clear();
da.Fill(dt);


if (dt.Rows.Count > 0)
{
ReportDocument RptDoc = new ReportDocument();

RptDoc.Load(Server.MapPath("CrystalReport.rpt"));
RptDoc.SetDataSource(dt);

CrystalReportViewer1.ReportSource = RptDoc;
CrystalReportViewer1.DataBind();

}

                            



Above one is all about a function  NormalBinding() who's used to bind data in crystal report!      
you can take a look to basic binding on my basic asp.net post.



Now Your Whole Project Will Look Some thing Like This.

.ASPX PAGE
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="crys.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.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>

.CS FILE
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Enterprise;
using CrystalDecisions.ReportSource;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection sconn = new SqlConnection(ConfigurationManager.AppSettings["regattacon"]);
    string sid;
    SqlDataAdapter da;
    string query;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
         
            bindproformainvoicepi_no();

        }
    }

    public void bindproformainvoicepi_no()
    {

        query = "select * from sample";
        SqlCommand sqlcmd = new SqlCommand(query, sconn);

        da = new SqlDataAdapter(sqlcmd); DataTable dt = new DataTable();
        dt.Clear();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            ReportDocument RptDoc = new ReportDocument();

            RptDoc.Load(Server.MapPath("CrystalReport.rpt"));
            RptDoc.SetDataSource(dt);

            CrystalReportViewer1.ReportSource = RptDoc;
            CrystalReportViewer1.DataBind();
          


        }
    }
}






RUN it and have fun with CRYSTAL REPORT,with its nice printing,exporting to excel,pdl,doc ,style












4 comments:

  1. the very very useful guide for beginers ,you saved my life dude,i was searching for the exact same

    ReplyDelete
  2. Nice Article....Useful for beginners....

    ReplyDelete
  3. Hey Do We need License for Hosting Crystal reports on production server.

    Please let me know

    Thanks in advance....

    Sai

    ReplyDelete
  4. Wow nice post. In this post, mentions a lot of things about Crystal Report Hosting. Yes, Crystal Reports is a business intelligence application, currently marketed to small businesses. I developed on Visual Studios that uses the version of Crystal Reports built in. I need to deploy this so visitors to my site can read and print reports. I have a great idea on ASP.NET MVC Hosting; I am looking for this, if you have any interest, then see http://www.myasp.net/

    ReplyDelete

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...