Hi SaChie,
Check this example. Now please take its reference and correct your code.
Using the below article i have created the example.
HTML
<%@ 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>
<asp:TextBox ID="txtName" runat="server" Text=""></asp:TextBox>
<asp:Button ID="btnSearchTerm" runat="server" Text="Search" OnClick="SearchClick" />
<br />
<CR:CrystalReportViewer ID="crPersonInformation" runat="server" AutoDataBind="true"
EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False" />
</div>
</form>
</body>
</html>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
Code
C#
protected void Page_PreInit(object sender, EventArgs e)
{
if (Session["Name"] != null)
{
BingReport(Session["Name"].ToString());
}
else
{
BingReport("");
}
}
protected void SearchClick(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtName.Text.Trim()))
{
Session["Name"] = txtName.Text.Trim();
}
else
{
Session["Name"] = "";
}
}
private void BingReport(string name)
{
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/EmployeesDetail.rpt"));
Employees dsPersonInformations = GetEmployees(name);
crystalReport.SetDataSource(dsPersonInformations);
crPersonInformation.ReportSource = crystalReport;
}
private Employees GetEmployees(string searchTerm)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand("SELECT EmployeeID,LastName,FirstName,BirthDate FROM Employees WHERE FirstName LIKE '%'+@Name+'%' OR @Name IS NULL");
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
if (!string.IsNullOrEmpty(searchTerm))
{
cmd.Parameters.AddWithValue("@Name", searchTerm);
}
else
{
cmd.Parameters.AddWithValue("@Name", (object)DBNull.Value);
}
sda.SelectCommand = cmd;
using (Employees dsEmployees = new Employees())
{
sda.Fill(dsEmployees, "Employee");
return dsEmployees;
}
}
}
}
Screenshot
