Calculate Time between two Dates in RDLC Report using C# and VB.Net in ASP.Net

Last Reply 9 months ago By pandeyism

Posted 9 months ago

Hello All,

I have 2 fields i want the diffrence between the 2 time fields in "HH:MM:ss" format RDLC report.

2 Fileds:

Fields!TimeIn.Value = "05:30:00"

Fields!StartTime.Value ="05:41:12 "

 

I want the result like Result = "00:11:12"

 

I have tried like the below code it will give only minutes

 

DateDiff(DateInterval.Minute,Fields!TimeIn.Value,Fields!StartTime.Value)

 

Posted 9 months ago

Hey vail,

Please refer below sample

HTML

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Height="235px" 
    Width="565px">
</rsweb:ReportViewer>

Namespaces

C#

using Microsoft.Reporting.WebForms;
using System.Configuration;
using System.Data.SqlClient;

VB.Net

Imports System.Data.SqlClient
Imports Microsoft.Reporting.WebForms

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ReportViewer1.ProcessingMode = ProcessingMode.Local;
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
        Orders dsOrders = GetData("select top 20 * from Orders WHERE OrderID IN (11090,11091)");
        ReportDataSource datasource = new ReportDataSource("DataSet1", dsOrders.Tables[0]);
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(datasource);
    }
}

private Orders GetData(string query)
{
    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 (Orders dsOrders = new Orders())
            {
                sda.Fill(dsOrders, "DataTable1");
                return dsOrders;
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ReportViewer1.ProcessingMode = ProcessingMode.Local
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
        Dim dsOrders As Orders = GetData("select top 20 * from Orders WHERE OrderID IN (11090,11091)")
        Dim datasource As ReportDataSource = New ReportDataSource("DataSet1", dsOrders.Tables(0))
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(datasource)
    End If
End Sub

Private Function GetData(ByVal query As String) As Orders
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As SqlCommand = New SqlCommand(query)
    Using con As SqlConnection = New SqlConnection(conString)
        Using sda As SqlDataAdapter = New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dsOrders As Orders = New Orders()
                sda.Fill(dsOrders, "DataTable1")
                Return dsOrders
            End Using
        End Using
    End Using
End Function

Expression

=Format(Floor((DateDiff("n",Fields!ShippedDate.Value, Fields!RequiredDate.Value))/60),"00") 
+ ":" + 
Format(((DateDiff("n",Fields!ShippedDate.Value, Fields!RequiredDate.Value)) Mod 60),"00")

Screenshot