How to create checkbox in Crystal Report?
In this article I am going to explained about how to place checkbox control in crystal report. Normally there is no direct option provide check box control in crystal report, using this concept you can create checkbox checked/unchecked image easily.
Description
For an example we are collecting user information in the registration page during that time we also collecting user want news letter or not using check box control. In that time of view user details in crystal report we can show checkbox image checked / un checked based on the user table.Table structure
create table reg(ID int identity(1,1),Name varchar(50),email varchar(75),pwd varchar(10),desig varchar(50),Newsletter varchar(1))Client side
I have created registration page with the above fields to collect information from the user
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>New User Registration</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" align="center" width="700">
<tr>
<td height="40" colspan="2">
<b>New User Registration</b>
</td>
</tr>
<tr>
<td height="40" colspan="2" align="center">
<asp:Label ID="Label1" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td height="30" align="left" width="50%">
Name
</td>
<td height="30">
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td height="30" align="left">
Login Email Id
</td>
<td height="30">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td height="30" align="left">
Login Password
</td>
<td height="30">
<asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td height="30" align="left">
Designation
</td>
<td height="30">
<asp:TextBox ID="txtDesig" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td height="40" colspan="2" align="left">
<asp:CheckBox ID="chknews" runat="server" Text="Send me an email of latest update of this website" />
</td>
</tr>
<tr>
<td height="40" colspan="2" align="center">
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Client side web page looking like this after designed
Server side
I have store that collected information in sql server table like this
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
SqlCommand sqlcmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string newsletter;
if (chknews.Checked==true)
{
newsletter="Y";
}
else
{
newsletter="N";
}
try
{
sqlcon.Open();
sqlcmd=new SqlCommand("insert into reg(Name,email,pwd,desig,Newsletter) values('" + txtname.Text + "','" + txtEmail.Text + "','" + txtpwd.Text + "','" + txtDesig.Text +"','" + newsletter + "')",sqlcon);
sqlcmd.ExecuteNonQuery();
Label1.Text="Record insert successfully";
clear();
}
catch (Exception ex)
{
}
finally
{
sqlcon.Close();
}
}
void clear()
{
foreach (Control c in form1.Controls) //This loop takes all controls from the form1
{
//Clear all textbox values
if (c is TextBox)
((TextBox)c).Text = "";
//clear all check boxes
if (c is CheckBox)
((CheckBox)c).Checked = false;
//Clear all radio buttons
if (c is RadioButton)
((RadioButton)c).Checked = false;
}
}
}
Create Dataset with fields
Now right click on the project name add DataSet1.xsd into the App_Code folder and right click on the designer to add new datatable with the table fields like below
Create Crystal Report
Now right click on the Project name again and add new crystal report in the project name it as CrystalReport.rpt
Then right click on the Field Explorer and choose database expert choose newly created DataSet1 -> DataTable1 and click ok button to add that DataSet1 Fields in the crystal report designer.
Design Crystal report
Design your crystal report with the Field explorer fields
Then create one formula field to show check box for example I create one formula field as chkcol , using editor I set Checkbox true or not.
If checkbox is true I set Chr(254) else show unchecked checkbox using Chr(168).
Chr(254): This is decimal value for Wingdings character 254 its shows like checked check box image.
Chr(168): This is decimal value for Wingdings character 168 its shows like rectangle box like unchecked check box image.
That's all now you can drag and drop of your newly created formula field in to the crystal report. Right click on the Formula chkcol move to font tab change the font name as Wingdings
Now design one webpage to show the crystal report like belowDefault2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ 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>Display Records in crystal report</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" align="center" width="700">
<tr>
<td height="40" colspan="2">
<b>Display Record details</b>
</td>
</tr>
<tr>
<td height="30">
Enter User Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td height="40" colspan="2" align="center">
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
</td>
</tr>
<tr>
<td height="400" colspan="2" align="center" valign="top">
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>Default2 page Server side
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;
using CrystalDecisions.ReportSource;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string query;
try
{
sqlcon.Open();
query = "select * from reg where name like '%" + txtName.Text + "%'";
sqlcmd = new SqlCommand(query, sqlcon);
da = new SqlDataAdapter(sqlcmd);
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();
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
sqlcon.Close();
}
}
}
Output
The output is shows the check box image based on the database value checked / unchecked.
Source Code Detail:
I have attached source code of this article download it and try to register and get back details in crystal report.
Front End : ASP.NET
Code Behind : C#
Conclusion:
I hope this article help to know checkbox control in crystal report.