Hi,
Check this example. Now please take its reference and correct your code.
Using the below article i have created the example.
Database
For this example I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
HTML
<div>
<asp:TextBox ID="txtDate" runat="server" Text="12/08/1948"></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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<link rel="Stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
<script type="text/javascript">
$(function() {
$("[id*=txtDate]").datepicker({
showOn: 'button',
buttonImageOnly: true,
buttonImage: 'https://i.imgur.com/u6upaAs.png'
});
});
</script>
Namespace
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
VB.Net
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Code
C#
protected void SearchClick(object sender, EventArgs e)
{
DateTime searchTerm = Convert.ToDateTime(txtDate.Text.Trim());
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/EmployeesDetail.rpt"));
Employees dsPersonInformations = GetEmployees(searchTerm);
crystalReport.SetDataSource(dsPersonInformations);
crPersonInformation.ReportSource = crystalReport;
}
private Employees GetEmployees(DateTime searchTerm)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand("SELECT EmployeeID,LastName,FirstName,BirthDate FROM Employees WHERE BirthDate = @BirthDate");
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@BirthDate", searchTerm.ToShortDateString());
sda.SelectCommand = cmd;
using (Employees dsEmployees = new Employees())
{
sda.Fill(dsEmployees, "Employee");
return dsEmployees;
}
}
}
}
VB.Net
Protected Sub SearchClick(ByVal sender As Object, ByVal e As EventArgs)
Dim searchTerm As DateTime = Convert.ToDateTime(txtDate.Text.Trim())
Dim crystalReport As ReportDocument = New ReportDocument()
crystalReport.Load(Server.MapPath("~/EmployeesDetail.rpt"))
Dim dsPersonInformations As Employees = GetEmployees(searchTerm)
crystalReport.SetDataSource(dsPersonInformations)
crPersonInformation.ReportSource = crystalReport
End Sub
Private Function GetEmployees(ByVal searchTerm As DateTime) As Employees
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,LastName,FirstName,BirthDate FROM Employees WHERE BirthDate = @BirthDate")
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
cmd.Parameters.AddWithValue("@BirthDate", searchTerm.ToShortDateString())
sda.SelectCommand = cmd
Using dsEmployees As Employees = New Employees()
sda.Fill(dsEmployees, "Employee")
Return dsEmployees
End Using
End Using
End Using
End Function
Screenshot