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

Last Reply one month ago By trisetia302

Posted one month ago

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.

You are viewing reply posted by: trisetia302 one month ago.
Posted one month ago

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();