Click here to Skip to main content
15,885,366 members
Articles / Web Development / ASP.NET

Generate a Report using Crystal Reports in Visual Studio 2010

Rate me:
Please Sign up or sign in to vote.
4.88/5 (160 votes)
8 Jun 2011CPOL11 min read 1.7M   82.1K   243  
Reporting Made Easy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class CrossTabReport : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ReportDocument rptDoc = new ReportDocument();
        dsTestCrystal ds = new dsTestCrystal();
        DataTable dt = new DataTable();
        dt.TableName = "Crystal Report Example";
        dt = getAllOrders().Tables[0];
        ds.Tables[0].Merge(dt);
        rptDoc.Load(Server.MapPath("crosstab.rpt"));
        rptDoc.SetDataSource(ds);
        CrystalReportViewer1.ReportSource = rptDoc;
    }
    public DataSet getAllOrders()
    {
        string sqlCon = ConfigurationManager.AppSettings["conStr1"].ToString();
        SqlConnection Con = new SqlConnection(sqlCon);
        SqlCommand cmd = new SqlCommand();
        DataSet ds = null;
        SqlDataAdapter adapter;
        try
        {
            Con.Open();
            cmd.CommandText = "getAllOrders";
            cmd.CommandType = CommandType.StoredProcedure;
            //cmd.Parameters.Add(new SqlParameter("@UserName", bUser.UserName));
            //cmd.Parameters.Add(new SqlParameter("@Password", bUser.Password));
            cmd.Connection = Con;
            ds = new DataSet();
            adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds, "Users");
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            cmd.Dispose();
            if (Con.State != ConnectionState.Closed)
                Con.Close();
        }
        return ds;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Working in Ahmedabad, Gujarat, India
India India

I am working as a Software Engineer in Ahmedabad, Gujrat, India.

I have 12+ Years of Experience in Microsoft Technology Like Asp.Net 4.0,
C#,MVC,EntityFramework, Javascript, Crystal Reports, JQuery etc.

Find out more on :




Comments and Discussions