Hi kankon,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:ScriptManager runat="server" />
<rsweb:ReportViewer ID="ReportViewer1" runat="server"></rsweb:ReportViewer>
Namespaces
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;
Code
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/EmployeeReport.rdlc");
ReportDataSource datasource = new ReportDataSource("Employees", GetData().Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
}
private Employees GetData()
{
string query = "SELECT TOP 5 FirstName 'Name',CAST(BirthDate AS DATE) 'StartDate',CAST(HireDate AS DATE) 'EndDate' FROM Employees " +
" UNION SELECT 'Dharmendra',NULL,NULL";
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (Employees employees = new Employees())
{
sda.Fill(employees, "DataTable1");
return employees;
}
}
}
}
}
Expresasions
For displaying empty when null date
=Replace(CDate(Fields!StartDate.Value).ToString("yyyy/MM/dd"),"0001/01/01","")
& System.Environment.NewLine &
Replace(CDate(Fields!EndDate.Value).ToString("yyyy/MM/dd"),"0001/01/01","")
For date difference calculation
=DateDiff("d",
CDate(Fields!StartDate.Value).ToString("yyyy/MM/dd"),
CDate(Fields!EndDate.Value).ToString("yyyy/MM/dd")
)
Output