I'm facing a problem where every time I refresh my page, the loan paid will increase by 500. I understand that my logic is wrong, after 1 month from the loan application date, the 'loanPaid' will increase by 500 but what I want to happen is every next month it will increase by $500. If anyone who can help me with the logic. I would appreciate it. I was thinking of using some loop but not sure which one and how. I'm a freshman student only so please pardon my coding style. Thank you

---

```
public class LoanDAL
{
    string connString = ConfigurationManager.ConnectionStrings["Oakhorizons"].ToString();
    public LoanDAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public DataTable getAllLoanInfoDT()
    {
        using (SqlConnection conn = new SqlConnection(connString))
        {
            DataTable dt = new DataTable();
            SqlCommand cmd2 = new SqlCommand();
            cmd2.Connection = conn;
            // cmd.CommandType = CommandType.StoredProcedure;
            cmd2.CommandText = "SELECT DISTINCT purchaseDate FROM LoanPortfolio WHERE (custID LIKE 'OH00002') AND (loanType LIKE 'Personal Loan')";
            cmd2.Parameters.AddWithValue("@custID", "OH00002");
            cmd2.Parameters.AddWithValue("@loanType", "Personal Loan");
            conn.Open();
            string custID = "OH00002";
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd2;
            da.Fill(dt);
            int iMonthNo = int.Parse(System.DateTime.Now.Month.ToString());
            DateTime dtDate = new DateTime(2000, iMonthNo, 1);
            double dMonthNow = Double.Parse(dtDate.ToString("MM"));
            LoanTableAdapters.LoanPortfolioTableAdapter loanAdapter = new LoanPortfolioTableAdapter();
            string LoanDate = loanAdapter.RetrieveData(custID.ToString()).ToString();
            string month = dt.ToString();

            double dLoanDate = Double.Parse(LoanDate.Substring(3, 2));

            if (dMonthNow > dLoanDate)
            {
                String sql = "UPDATE LoanPortfolio SET loanPaid = loanPaid + 500";
                sql += "WHERE (loanType LIKE 'Personal Loan') AND (custID LIKE 'OH00002')";
                cmd2.Connection = conn;
                cmd2.CommandText = sql;

                cmd2.ExecuteNonQuery();

            }
            conn.Close();
}
}

```

After edit:

```
 public DataTable getAllLoanInfoDT()
{ 
    using (SqlConnection conn = new SqlConnection(connString))
    {
        SqlCommand cmd2 = new SqlCommand();
        cmd2.Connection = conn;
        // cmd.CommandType = CommandType.StoredProcedure;
        cmd2.CommandText = "SELECT DISTINCT loanUpdateDate FROM LoanPortfolio WHERE (custID LIKE 'OH00002') AND (loanType LIKE 'Personal Loan')"; 
        cmd2.Parameters.AddWithValue("@custID", "OH00002");
        cmd2.Parameters.AddWithValue("@loanType", "Personal Loan");
        conn.Open();
        SqlDataReader myReader = cmd2.ExecuteReader();
        DateTime loanUpdateDate = Convert.ToDateTime(myReader);
        DateTime currDateTime = DateTime.Now;

        int loanToBeAdded = (((currDateTime.Year - loanUpdateDate.Year) * 12) + currDateTime.Month - loanUpdateDate.Month) * 500;
        if (loanToBeAdded > 0)
        {
            String sql = "UPDATE LoanPortfolio SET loanPaid = loanPaid + " + loanToBeAdded.ToString() + ", loanUpdateDate = " + DateTime.Now.ToString();
            sql += " WHERE (loanType LIKE 'Personal Loan') AND (custID LIKE 'OH00002')";

            //Execute the above query here
        }
        conn.Close();

        using (SqlDataAdapter dAd = new SqlDataAdapter("SELECT * FROM LoanPortfolio where custID like 'OH00002'", conn))
        {
            DataTable dTable = new DataTable();
            dAd.Fill(dTable);
            return dTable;
        }
    }
}

```