Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my problem is that when after I encode the pay of the tenant will display the status that is paid but the display still is not paid.

What I have tried:

C#
private void cbName_TextChanged(object sender, EventArgs e)
        {
            try
            {
                dataGridViewPayment.Columns.Clear();

                int cbid = Int32.Parse(cbName.SelectedValue.ToString());
                con.Close();
                cmd = new MySqlCommand("select * from tenant  WHERE id =  " + cbid, con.con);
                con.Open();
                reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    tbBusinessName.Text = reader["BusinessName"].ToString();
                }



                //fill gridview payment ledger
                con.Close();
                cmd = new MySqlCommand("SELECT a.id,a.Contractid,a.Schedule,a.Amort,IFNULL(Penalty, 0 ) AS penalty,a.Status FROM ledgercard AS a JOIN contract AS b  ON a.contractid = b.id WHERE b.TenantID ='" + cbName.SelectedValue.ToString() + "'", con.con);
                con.Open();
                reader = cmd.ExecuteReader();

                DataTable ledger = new DataTable();
                ledger.Columns.Add("Schedule", typeof(DateTime));
                ledger.Columns.Add("Amort", typeof(string));
                ledger.Columns.Add("Penalty", typeof(string));
                ledger.Columns.Add("Status", typeof(string));

                string Amort = "0.00";
                string Penalty = "0.00";
                string Stat = "";

                while (reader.Read())
                {
                    contractid = Int32.Parse(reader["Contractid"].ToString());
                 


                    // for amort
                    if (totalamort > 0)
                    {
                        if (totalamort >= Decimal.Parse(reader["Amort"].ToString()))
                        {
                            totalamort -= Decimal.Parse(reader["Amort"].ToString());
                        }
                        else
                        {
                            Decimal paybal = Decimal.Parse(reader["Amort"].ToString()) - totalamort;
                            Amort = paybal.ToString();
                            totalamort = 0;
                        }
                    }
                    else
                    {
                        Amort = reader["Amort"].ToString();
                    }


                    // for penalty
                    if (totalpenalty > 0)
                    {
                        if (totalpenalty >= Decimal.Parse(reader["Penalty"].ToString()))
                        {
                            totalpenalty -= Decimal.Parse(reader["Penalty"].ToString());
                        }
                        else
                        {
                            Decimal paybal = Decimal.Parse(reader["Penalty"].ToString()) - totalpenalty;
                            Penalty = paybal.ToString();
                            totalpenalty = 0;
                        }
                    }
                    else
                    {
                        Penalty = reader["Penalty"].ToString();
                    }


                    // for stats
                    if (totalamort == decimal.Parse("0.00") && totalpenalty == decimal.Parse("0.00"))
                    {
                        Stat = "PAID";
                    }
                    else
                    { 
                        Stat = "NOT PAID";
                    }
                       
                   

                    // gridview row entries
                    ledger.Rows.Add(reader["Schedule"].ToString(),
                        Amort.ToString(),
                        Penalty.ToString(),
                        Stat);
                    

                    dataGridViewPayment.DataSource = ledger;
                }
                reader.Close();

                //------------------------------------------------------------

                decimal totalpen = 0;
                decimal totalAmrt = 0;
                decimal totalBal = 0;

                foreach(DataGridViewRow rw in dataGridViewPayment.Rows)
                {
                    totalpen += Convert.ToDecimal(rw.Cells[2].Value.ToString());
                    totalAmrt += Convert.ToDecimal(rw.Cells[1].Value.ToString());
                    totalBal = totalpen + totalAmrt;
                }        
                tbPenalty.Text = totalpen.ToString();
                tbAmortization.Text = totalAmrt.ToString();
                tbTotalBalance.Text = totalBal.ToString();
Posted
Updated 22-Sep-22 23:48pm
v3
Comments
Richard MacCutchan 23-Sep-22 4:53am    
That code does very little and gives no clue as to your problem. Please use the Improve question link above, and add complete details of what is not working.
Marc Chester Ferrer 23-Sep-22 4:56am    
I did improve my question
Richard MacCutchan 23-Sep-22 5:16am    
You also need to explain exactly what the problem is, and which part of that unformatted code causes it. To make your code readable please add <pre></pre> tags around it.
OriginalGriff 23-Sep-22 5:40am    
I just did that for him.
Richard MacCutchan 23-Sep-22 5:46am    
You are too kind (but I already knew that).

1 solution

We can't tell: we have no idea what totalAmort or totalpenalty are, or even why type they are. And I'm not convinced you do either, given code like this:
C#
if (totalamort == decimal.Parse("0.00") && totalpenalty == decimal.Parse("0.00"))

If they are decimal values, why not use the decimal value literal indicator: Floating-point numeric types - C# reference | Microsoft Learn[^]
C#
if (totalamort == 0.00m && totalpenalty == 0.00m)
Is a whole load more readable ...

Use the debugger to find out exactly what is actually going on, and what values your variables actually contain.
 
Share this answer
 
Comments
Marc Chester Ferrer 23-Sep-22 21:01pm    
totalamort that is the total of amortization and the totalpenalty that is the total penalty
and their type is decimal but I convert it to string
OriginalGriff 24-Sep-22 1:01am    
Did you look at what I said, or just read the first sentence?
Marc Chester Ferrer 24-Sep-22 1:49am    
what I mean is if the tenant pays his/her due first of the month that month will show the status paid then next month will still not be paid in terms of 24 months. the output is the number of months will minus the remaining months 23. but thank you so much for helping me with my problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900