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


i creating bank management system application in c#.net windows application. in banks,acount holders(saving bank account) paid interest(eg.3.5% of current balance) @ end of month. prepared code 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 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 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 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 transactions account_no='" + acctno + "' , 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;         (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 + "' account_no='" + acctno + "'";         sqlcommand cmd = new sqlcommand(str, conn);         cmd.executenonquery();           string comment = "by interest";         string transact = "yes";         string str1 = "insert 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 * transactions account_no='" + acct_no + "' , 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["transactions"].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 executing interest() method in form load event, method run on first day of month when application started. run on same day every time close , restart application. ,interest paid each time applicaion starts on 1st day of month. there no specific time of starting application,so can't use time logic. can me solve problem?

you don't have guarantee run application, less when run it. logic should handled outside of application, in either windows service or database job can depend run on interval or @ time.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -