开发者

How to run a method on a particular day and also only once in .net

I am creating a Bank Management system application in C#.net windows application. As in all banks,acount holders(saving bank account) are paid interest(eg.3.5% of current balance) at the end of the month. I am prepared the code for it.

    public void frmbankmg_Load(object sender, EventArgs e)

    {
        try
        {
            Transact();

        DateTime date = DateTime.Now.Date;
        //DateTime date = DateTime.Parse("Nov 1,2010");
        int day = date.Day;
        double Account = 0, Balance = 0;
        string status = "";
        if (day == 1)
        {
            SqlConnection conn = ConnectionString();
            string s = "select Account_No,Balance_Amount,Status from SavingAcct";
            SqlCommand cmd = new SqlCommand(s, conn);
            conn.Open();
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                Account = Convert.ToDouble(rd["Account_No"].ToString());
                Balance = Convert.ToDouble(rd["Balance_Amount"].ToString());
                status = rd["Status"].ToString();
                if (status == "Open")
                {
                    Interest(Account, Balance);
                }
            }
            conn.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}        

private void Transact()
{
    try
    {
        DateTime date = DateTime.Now.Date;
        int month = date.Month;
        int year = date.Year;
        int days = DateTime.DaysInMonth(year, month);
        if (days == date.Day)
        {
            double Account = 0, Balance = 0;
            string status = "";

            SqlConnection conn = ConnectionString();
            string s = "select Account_No,Balance_Amount,Status from SavingAcct";
            SqlCommand cmd = new SqlCommand(s, conn);
            conn.Open();
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                Account = Convert.ToDouble(rd["Account_No"].ToString());
                Balance = Convert.ToDouble(rd["Balance_Amount"].ToString());
                status = rd["Status"].ToString();
                if (status == "Open")
                {
                    string Transaction = "No";
                    string s1 = "insert into Transactions(Account_No,Date,Current_Balance,Month,Year,Transact) values('" + Account + "','" + date + "','" + Balance + "','" + month + "','" + year + "','" + Transaction + "'";
                    SqlCommand cmd1 = new SqlCommand(s1, conn);
                    conn.Open();
                    cmd1.ExecuteNonQuery();
                    conn.Close();
                }
            }
            conn.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void Interest(double Acctno,double balance)
{
    try
    {
        DateTime date = DateTime.Now.Date;
        //DateTime date = DateTime.Parse("Nov 1,2010");
        int month = date.Month;
        int year = date.Year;
        month = month - 1;
        SqlConnection conn = ConnectionString();
        string s = "select Date,Current_Balance from Transactions where Account_No='" + Acctno + "' and Month='" + month + "'";
        SqlDataAdapter da = new SqlDataAdapter(s, conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "Transactions");
        int totalRows = ds.Tables["Transactions"].Rows.Count;
        conn.Open();
        DateTime date1 = DateTime.Now, date2, date3;
        int row, row1 = -1, row2 = -1, day1, day2;
        for (row = 0; row < totalRows; row++)
        {
            date1 = (DateTime)ds.Tables["Transactions"].Rows[row][0];
            int d = date1.Day;
            if (d <= 10)
            {
                date2 = date1;
                day1 = d;
                row1 = row;
            }
            else
            {
                date3 = date1;
                day2 = d;
                row2 = row;
            }
        }
        double Bal1 = 0;
        if (row1 == -1)
        {
            Bal1 = NoTransactions_Bet1_and_10(Acctno, month);
        }
        else
        {
            Bal1 = Convert.ToDouble(ds.Tables["Transactions"].Rows[row1][1]);
        }
        double Bal2 = Convert.ToDouble(ds.Tables["Transactions"].Rows[row2][1]);
        double credit = 0;
        if (Bal1 <= Bal2)
        {
            credit = (3.5) / 100 * Bal1;
            balance = balance + (credit);
        }
        else
        {
            credit = (3.5) / 100 * Bal2;
            balance = balance + (credit);
        }
        string str = "update SavingAcct set Balance_Amount='" + balance + "' where Account_No='" + Acctno + "'";
        SqlCommand cmd = new SqlCommand(str, conn);
        cmd.ExecuteNonQuery();


        string comment = "By Interest";
        string transact = "Yes";
        string str1 = "insert into Transactions(Account_No,Date,Particulars,Credit,Current_Balance,Month,Year,Transact) values('" + Acctno + "','" + DateTime.Now.Date + "','" + comment + "','" + credit + "','" + balance + "','" + month + "','" + year + "','" + transact + "')";
        SqlCommand cmd1 = new SqlCommand(str1, conn);
        cmd1.ExecuteNonQuery();
        conn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private double NoTransactions_Bet1_and_10(double Acct_no,int month)
{
    try
    {
        month = month - 1;
        string s = "select * from Transactions where Account_No='" + Acct_no + "' and Month='" + month + "'";
        SqlConnection con = ConnectionString();

        SqlDataAdapter da = new SqlDataAdapter(s, con);
        DataSet ds = new DataSet();
        da.Fill(ds, "Transactions");

        con.Open();
        int Tot_row = ds.Tables["Tr开发者_StackOverflow社区ansactions"].Rows.Count;
        double Balance = Convert.ToDouble(ds.Tables["Transactions"].Rows[Tot_row - 1][6]);
        con.Close();

        return Balance;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

As i am executing the interest() method in form load event, this method will run on the first day of the month when the application is started. But it will also run on the same day every time I close and restart my application. so ,interest will be paid each time the applicaion starts on the 1st day of the month. As there is no specific time of starting the application,so i can't use the time logic. Can you help me to solve this problem?


You don't have any guarantee that anyone will run your application, much less when they will run it. So logic like this should be handled outside of the application, in either a windows service or even a database job that you can depend to run on a certain interval or at a certain time.


You could simply write the last date you ran the function back to the database and check that date at the start of the function.


Typically you would do this via a scheduled job in the database itself rather than in client code.


This seems like a system maintenance task. Certainly, you wouldn't want people to have to wait to get their savings interest until someone opened the application - what if the last day of the month is a bank holiday, or everyone is doing something else for some reason. Don't put these sorts of tasks in the windows.forms application - set it up as a scheduled task on a server that runs once a month.


Dont run it on application start, Never try to do that.

Run it on a button click then if you must. And also save the state of when it was run, so that it would not happen twice.

Maybe also save the values that were generated, so that yo have some record of what happened, and if it was to be run again, for lets say ammendments, you have an amendment screen, so that specific accounts can be updated.

You should probably have this running as a service that does its own scheduling, or at least a app that can be run by the windows schedular without human interaction.

Whom is going to press the button 1st of Jan???


You set up a headless console app that runs the code automatically when executed (bonus points for logging errors to a file somewhere) and you set up Windows Task Scheduler to call it once a month on a repeating basis (perhaps 12:01 AM on the 1st of the month or something).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜