First create a Data Set
Click on Add Button
It prompts for a message saying as
Click on Yes Button
Click on New Connection
Click on Continue
Give your Server Name
User Name
Password
And Click on Test Connection.
And click on Ok
And Click on Next Button
You can write your own query or u can use the stored procedure
Click on Finish
Select crystal report.rpt file and the below screen is visible
And Click on Ok button
And then press ctrl +alt +T
Double Click on ADO.NET(XML)
Give the dataset path of the folder in FileNamePath
Click on Finish
And Build the solution
And Click on Ok Button
Delete Print Date and Page N from the crystal Report
Go to field explorer and drag your fields to Detail View
In case if you want to Add your own Header Name, like Company Name and Address then, write the following code in .cs page as below
Go to Crystal Report
Press Ctrl+Alt+T
Field Explorer
Similary Add Address
Now, the Report is Ready a Default page as below
write the following code as follows
Design the Form as Follows
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var fontCollection = new System.Drawing.Text.InstalledFontCollection();
foreach (var font in fontCollection.Families)
{
ListBox1.Items.Add(font.Name);
}
}
}
Declare the Following Fields Globally.
private ReportDocument _reportDocument;
string selectedfont = string.Empty;
Namespaces
using CrystalDecisions.CrystalReports.Engine;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using CrystalDecisions.Shared;
using System.Drawing;
using System.Windows;
using System.Diagnostics;
using System.IO;
using System.Threading;
Write the following Code in Button Click
protected void Button2_Click(object sender, EventArgs e)
{
string selectedfont = string.Empty;
selectedfont = Convert.ToString(ListBox1.SelectedItem);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetDepartmentInformation";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
_reportDocument = new ReportDocument();
_reportDocument.Load(Server.MapPath("CrystalReport.rpt"));
_reportDocument.SetDataSource(ds.Tables[1]);
FieldObject fieldObject;
FieldFormat fieldFormat;
Section section;
// Get the Section object by name.
section = _reportDocument.ReportDefinition.Sections["Section3"];
fieldObject = section.ReportObjects[0] as FieldObject;
// Check if the FieldObject is null.
if (fieldObject != null)
{
// Get the FieldFormat object.
fieldFormat = fieldObject.FieldFormat;
fieldObject.Color = Color.Red;
Font fo = new Font(selectedfont, 10F, FontStyle.Regular);
fieldObject.ApplyFont(fo);
}
CrystalReportViewer1.ReportSource = _reportDocument;
CrystalReportViewer1.DataBind();
string CompanyName = "abc";
string Address = "Hyd";
_reportDocument.DataDefinition.FormulaFields["Company"].Text = "'" + CompanyName + "'";
_reportDocument.DataDefinition.FormulaFields["Address1"].Text = "'" + Address + "'";
ShowReport();
}
private void ShowReport()
{
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
//for pdf format
_reportDocument.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, false, "abc");
//forexcel //_reportDocument.ExportToHttpResponse(ExportFormatType.Excel, Response, false, "abc");
//forrichtext //_reportDocument.ExportToHttpResponse(ExportFormatType.RichText, Response, false, "abc");
//forword //_reportDocument.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, false, "abc");
No comments:
Post a Comment
Thank you for visiting my blog