Hi mahesh213,
The WebServer IIS is continuously running, we can add a timer in the application and the timer can manage all these activities to update the Count bay checking the condition.
For this we have to make use of Global.asax file in which the timer will run at every second and On TimerElapsed event it check every month 1 date and update the record in database.
Check this example. Now please take its reference and correct your code.
Global.asax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Update_Count_Every_Month_MVC
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.Elapsed += OnTimerElapsed;
timer.Start();
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
private void OnTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// Runs at every day 12AM.
if (DateTime.Now.Hour == 0 && DateTime.Now.Minute == 0 && DateTime.Now.Day == 1)
{
TestEntities db = new TestEntities();
List<EmployeeCount> employees = db.EmployeeCounts.ToList();
foreach (EmployeeCount emp in employees)
{
emp.Count = emp.Count + 2;
}
db.SaveChanges();
}
}
}
}