[Solved] ASP.Net Error: Unable to cast object of type System.TimeSpan to type System.IConvertible

on Jun 07, 2021 01:45 AM

Hi Guys,

I'm try to calculate due date between two date, but I can't find how to do that in best way in C#.

I'm found the references code in VB and then I'm trying to translate that code to ASP.NET C#.

This Code in VB

'find the due date based on installments to ???
DueDate.Text = DateAdd(DateInterval.Month, Val(InstallmentsTo.Text), DateValue(RentDate.Text))

And this code I'm trying to translate in C#

    private void SearchDueDate()
        System.DateTime PaymentDate;
        System.DateTime RentDate;
        System.DateTime DateNow;

        DateTime InstallmentsTo;
        InstallmentsTo = Convert.ToDateTime(txtcicilan_ke.Text);

        RentDate = Convert.ToDateTime(txttgl_pinjaman.Text);
        PaymentDate = Convert.ToDateTime(txttanggal_pembayaran.Text);

        txttgl_jatuh_tempo.Text = Convert.ToDateTime(PaymentDate - RentDate),InstallmentsTo.ToString("yyyy/MM/dd");

When I the code running get this error

Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'.

RentDate = 2021/06/01 //yyyy/MM/dd

PaymentDate = 2021/07/01 //yyyy/MM/dd

InstallmentsTo = 1

Any help could be appriciate.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Jun 07, 2021 05:54 AM
trisetia302 says:
InstallmentsTo = 1

 Here is the issue. 1 can't be converted to Date. Use simple ToString.



on Jun 12, 2021 11:55 PM

Problem Solved

    private void ShowDueDate()
        using (SqlConnection con = new SqlConnection(koneksi))
            using (SqlCommand cmd = new SqlCommand())
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Select tbl_pinjaman.tanggal_jatuh_tempo from tbl_pinjaman,tbl_pembayaran where tbl_pinjaman.id_pinjaman = tbl_pembayaran.id_pinjaman and tbl_pinjaman.id_pinjaman ='"+ txtid_pinjaman.Text.Trim()+"'";
                using (SqlDataReader rdr = cmd.ExecuteReader())
                    while (rdr.Read())
                        txtDue_date.Text = rdr["tanggal_jatuh_tempo"].ToString();