Hi Bhavesh23,
Please Refer Below Sample.
Note:I have created sample by refering the below Article. For more details pleaser refer below Article.
Report Design
You need right click on the rpt -> Insert and add text object inside section 2 for displaying the From and To date.
HTML
<asp:TextBox ID="txtFromDate" runat="server"></asp:TextBox>
<asp:TextBox ID="txtToDate" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="Search" />
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports CrystalDecisions.CrystalReports.Engine
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
this.BindCrystalReport();
}
protected void Search(object sender, EventArgs e)
{
this.BindCrystalReport();
}
private void BindCrystalReport()
{
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/Sales.rpt"));
DataSet1 dsCustomers = GetData(txtFromDate.Text, txtToDate.Text);
crystalReport.SetDataSource(dsCustomers);
CrystalReportViewer1.ReportSource = crystalReport;
((TextObject)crystalReport.ReportDefinition.Sections["Section2"].ReportObjects["txtFromDate"]).Text = txtFromDate.Text;
((TextObject)crystalReport.ReportDefinition.Sections["Section2"].ReportObjects["txtToDate"]).Text = txtToDate.Text;
}
public DataSet1 GetData(string fromDate, string toDate)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
string query = "SELECT bno,name,cdate,totalgst,disc,paidamt,pymtmode FROM Sales";
using (SqlCommand cmd = new SqlCommand())
{
if (!string.IsNullOrEmpty(fromDate) && !string.IsNullOrEmpty(toDate))
{
query += " WHERE CONVERT(DATETIME,cdate,103) BETWEEN CONVERT(DATETIME,@FromDate,103) AND CONVERT(DATETIME,@ToDate,103)";
cmd.Parameters.AddWithValue("@FromDate", fromDate);
cmd.Parameters.AddWithValue("@ToDate", toDate);
}
cmd.CommandText = query;
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataSet1 dsCustomers = new DataSet1())
{
sda.Fill(dsCustomers, "DataTable1");
return dsCustomers;
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.BindCrystalReport()
End Sub
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Me.BindCrystalReport()
End Sub
Private Sub BindCrystalReport()
Dim crystalReport As ReportDocument = New ReportDocument()
crystalReport.Load(Server.MapPath("~/Sales.rpt"))
Dim dsCustomers As DataSet1 = GetData(txtFromDate.Text, txtToDate.Text)
crystalReport.SetDataSource(dsCustomers)
CrystalReportViewer1.ReportSource = crystalReport
CType(crystalReport.ReportDefinition.Sections("Section2").ReportObjects("txtFromDate"), TextObject).Text = txtFromDate.Text
CType(crystalReport.ReportDefinition.Sections("Section2").ReportObjects("txtToDate"), TextObject).Text = txtToDate.Text
End Sub
Public Function GetData(ByVal fromDate As String, ByVal toDate As String) As DataSet1
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Dim query As String = "SELECT bno,name,cdate,totalgst,disc,paidamt,pymtmode FROM Sales"
Using cmd As SqlCommand = New SqlCommand()
If Not String.IsNullOrEmpty(fromDate) AndAlso Not String.IsNullOrEmpty(toDate) Then
query += " WHERE CONVERT(DATETIME,cdate,103) BETWEEN CONVERT(DATETIME,@FromDate,103) AND CONVERT(DATETIME,@ToDate,103)"
cmd.Parameters.AddWithValue("@FromDate", fromDate)
cmd.Parameters.AddWithValue("@ToDate", toDate)
End If
cmd.CommandText = query
cmd.Connection = con
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dsCustomers As DataSet1 = New DataSet1()
sda.Fill(dsCustomers, "DataTable1")
Return dsCustomers
End Using
End Using
End Using
End Using
End Function
Screenshot