1

I have a table column as status(Varchar,records "Paid"/"Unpaid" only) and another column as nextDateOfPay(datetime, records a date).I need to update this status column as Unpaid for the records which are as Paid when the date(present date) is passed the date in nextDateOdPay column.And I need to repeat this update for every row in the table. Primary key is Id column(integer). I have no clue where to start,do i need to use T-sql or if i have to use sql agent job.I'd like to know if there is a simple solution that i can write in the program itself.Thank you so much in advance ! Code so far is as,

dateTimePicker3.Value = DateTime.Now;
textBox12.Enabled = false;
string newstat;
string query = "select Id,nextDateOfPay from gymTb where status='Paid'";

SqlConnection cn = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(query, cn);

DateTime x = DateTime.Now;
DateTime y;

if (cn.State.ToString() == "Closed")
{
    cn.Open();
}
try        
{
    object dtx = cmd.ExecuteScalar();
    y = Convert.ToDateTime(dtx);
    int result = DateTime.Compare(x, y);

    if (result >= 0)
        newstat = "Unpaid";
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
AliBoronsi
  • 764
  • 1
  • 12
  • 19
angels65
  • 57
  • 1
  • 6

1 Answers1

3

A simple UPDATE query would do:

UPDATE gymTb SET status='Unpaid'
WHERE status='Paid' AND nextDateOfPay <= getdate()

You could call this from your application, triggered by manually pressing a button or perhaps using timer logic.

To make it run daily, and independent from your application, you can put the UPDATE statement in a recurring job in SQL Server Agent. For more on that see here:
how to schedule a job for sql query to run daily?

Peter B
  • 22,460
  • 5
  • 32
  • 69