Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Crystal Reports Example in Asp.net using Dataset or Datatable

Dec 2, 2014
Introduction:

Here I will explain how to create crystal reports in asp.net using dataset or datatable with example in c#, vb.net in visual studio 2010 or crystal reports example in asp.net with dataset or datatable. Crystal Report is standard reporting tool for visual studio by using these we can display reports regarding user information and charts related to user activities etc and crystal reports need minimal coding to display result.

Description:

In Previous posts I explained
how to install crystal reports in visual studio 2010 and how to create rdlc reports using asp.net, create crystal reports with using oledb connection in asp.net, Pass parameters to crystal reports in asp.net, pass parameters to rdlc reports using asp.net and many articles related to crystal reports, asp.net, c#, vb.net . Now I will explain how to create crystal reports in asp.net using dataset or datatable with example in c#, vb.net in visual studio 2010.  

Before start implementation first design new table in your 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 Dataset and give proper name and click Add like as shown below

Now Open Dataset and add new datatable for that right click Add à Select Datatable like as shown below

Once we add datatable next step is we need to add columns for that right click on datatable select Add à select Column like as shown below

Here you need to remember one point that is the entire column names should be same as the table (UserInfomation) which we created in database

Suppose if you want to change datatype for any column select respective column right click and select properties

Now Change datatype for that respective column lilke as shown below

Once we completed adding datatable to dataset now right click on your website and select Add New item à in that select Crystal Report and click Add


After add crystal report it will prompt Crystal Report Gallery window in that select using the Report wizard option and click OK

Once we add Report Wizard it will open another window Standard Report Creation Wizard to select database connection for crystal report. Here we will select previously created UserInformation table from respective database like as shown below

Once we select our dataset and tables press Next button now it will ask you to select required columns to display in crystal report from dataset based on your requirement like select all columns or only few columns and click Next button like as show below

Now in next windows it will give you option to group columns or Record Selection or other options but all are optional so you can click Next à Next àFinish Once we finished everything it will create crystal report like as shown below

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


If you check your Default.aspx page code that will be like as shown below


<%@ 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>Crystal Reports Example in asp.net with dataset or datatable</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
</div>
</form>
</body>
</html>
Now open your code behind file write the code like as shown below

C# Code


using System;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ReportDocument reportdocument = new ReportDocument();
reportdocument.Load(Server.MapPath("~/CrystalReport.rpt"));
UserInformation dsCustomers = new UserInformation();
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from userinformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dsCustomers,"UserInfo");
con.Close();
}

reportdocument.SetDataSource(dsCustomers);
CrystalReportViewer1.ReportSource = reportdocument;
}
}
}

VB.NET Code


Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim reportdocument As New ReportDocument()
reportdocument.Load(Server.MapPath("~/CrystalReport.rpt"))
Dim dsCustomers As New UserInformation()
Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("select * from userinformation", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dsCustomers, "UserInfo")
con.Close()
End Using
reportdocument.SetDataSource(dsCustomers)
CrystalReportViewer1.ReportSource = reportdocument
End If
End Sub
End Class

Now run your application your report will be like this

Demo


create Crystal Reports Example in Asp.net using Dataset or Datatable

Download Sample Code Attached


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

8 comments :

Abdul said...

Nice Article..........

Unknown said...

how to create a parameter in crystal report

Anonymous said...

Nice....Good Post.

Unknown said...
This comment has been removed by the author.
Unknown said...

ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/fees_other.rpt"));

DAODB db8 = new DAODB();
Hashtable h8 = new Hashtable();

h8.Add("aproc_year", lblyear.Text);

h8.Add("aid", txtid.Text);
h8.Add("type", "O");



DataSet ds8 = new DataSet();
ds8.Clear();
ds8.Reset();

ds8 = db8.getresult("FEES_OTHER", h8);
if (ds8.Tables[0].Rows.Count != 0)
{
crystalReport.SetDataSource(ds8);
CrystalReportViewer1.ReportSource = crystalReport;
}

Unknown said...

crystal report cannot be displayed pls help me

sunny said...

tengo una duda, ¿que es dsCustomers?

Unknown said...

Please post how to make crystal report without database using grid view details in asp.net

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.