Display Days difference between two dates in ASP.Net RDLC Report

kankon
 
on Aug 02, 2021 06:40 AM
4645 Views

Report Viwer change error type to nothing

I am using report difference brtween two date if no date record change error to NoThing?

code report viewer

=DateDiff("d", Fields!OPsec.Value, Fields!OPusers.Value)

screenshoot

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 06, 2021 11:47 PM
on Aug 06, 2021 11:49 PM

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