Click here to Skip to main content
15,036,363 members
Articles / Web Development / ASP.NET
Posted 20 Jul 2011


78 bookmarked

Web Scheduler for managing timed jobs with ASP.NET pages

Rate me:
Please Sign up or sign in to vote.
4.76/5 (11 votes)
20 Jul 2011CPOL5 min read
Schedule timed tasks through ASP.NET administration pages.


This article introduces a web based job scheduler where you can add and manage your daily or monthly administration or report sending tasks. There are some other available approaches to do this, but I found none of them fully satisfying so I decided to write my own job scheduler. On one hand, you can use the Windows Task Scheduler for running some scripts, and on the other hand, you can use the MS SQL Server Agent job scheduler to execute some of your Stored Procedures to handle your database maintenance jobs. This project intends to combine these two approaches so they can be handled through an easily manageable ASP.NET page.



  • Daily and weekly jobs (planning to improve this feature in the future)
  • Two kinds of job steps: Run script, Run Stored Procedure
  • For one job, you can add multiple steps
  • Enable/disable a job
  • Run job instantly


The main goal of this project is to send weekly reports and to centralize jobs that need to be automated somehow, like backup jobs and other database maintenance tasks. I didn't want to tumble the Windows Task Scheduler or the Job scheduler in MS SQL Server, so I decided to create my own scheduler which has a clear administration page where jobs can easily be managed.

Using the code

The project has two main parts:

  1. MS SQL Server database (file: this contains a few tables and some Stored Procedures. Basically, this is the framework of the system. It stores the data of your tasks and other relevant information. Besides, there are Stored Procedures that manage your jobs. E.g.: inserting, editing, and deleting jobs or job steps. These procedures then are invoked through the .NET application.
  2. Here is the structure of the JOBS database:


  3. Visual Studio project (file: this solution holds the administration page of the project. It has some SQL execution routines that are fired when inserting, editing, or deleting jobs or job steps. It gives a clear overview of your tasks and also some extra features like enabling/disabling or running a job immediately.
  4. edit.jpg

Program logic

The main question about this project is how we can accomplish executing a job in the background at its own scheduled time without the need to start up the browser or anything else. The answer is very simple: you just need to create a job within the SQL Server Agent that runs daily every 10 seconds, for example. It's like an infinite loop and does nothing else; just starts up a Stored Procedure which executes and takes care of your tasks that are stored in the JOBS table. This way, all of your predefined jobs can be executed silently in the background at the right time. The Stored Procedure that needs to be placed in the loop job is called begin_jobs (also included in the zip files). It creates a CURSOR for the JOBS table and walks through the job records that must be executed at the right time and then fires them step-by-step.

CREATE PROCEDURE [dbo].[begin_jobs]

  DECLARE @job_id int

  WHERE [enabled] = 1 AND
        valid = 1 AND
        job_is_running = 0 AND
        next_run <= GETDATE()
  OPEN jobCursor
  FETCH FROM jobCursor INTO @job_id
      EXEC [dbo].[execute_job] @job_id
      FETCH FROM jobCursor INTO @job_id
  CLOSE jobCursor
  DEALLOCATE jobCursor

Once the database structure is built, we are going to need some more Stored Procedures to manage our jobs. The following example will fully describe the insertion of a job through an ASP.NET page. The Stored Procedure that inserts a new line into the JOBS table is as follows:

CREATE PROCEDURE [dbo].[insert_job]
@scheduler_type varchar(1024),
@description varchar(1024),
@monday bit,
@tuesday bit,
@wednesday bit,
@thursday bit,
@friday bit,
@saturday bit,
@sunday bit,
@occurs_at datetime

  DECLARE @schedule_type_id int,
          @job_id int
  SET @schedule_type_id = (SELECT TOP 1 id
                           FROM SCHEDULE_TYPES
                           WHERE UPPER([description]) = UPPER(@schedule_type))
  IF @description = '' OR @description IS NULL OR @schedule_type_id IS NULL
  INSERT INTO JOBS (schedule_type_id, [description])
  VALUES (@schedule_type_id, @description)
  SET @job_id = (SELECT @@IDENTITY)
  INSERT INTO SCHEDULES (job_id, monday, tuesday, wednesday, 
              thursday, friday, saturday, sunday, occurs_at)
  VALUES (@job_id, @monday, @tuesday, @wednesday, @thursday, 
          @friday, @saturday, @sunday, @occurs_at)

  -- UPDATE next_run
  EXEC [dbo].[update_next_run] @job_id
  -- UPDATE schedule_description
  EXEC [dbo].[update_schedule_description] @job_id

The ASP.NET application will call the above procedure when clicking on the Create job button like this:

using System.Data;
using System.Data.SqlClient;
using HelperLib;

private string connStringJobs = ConfigurationManager.ConnectionStrings["jobs"].ToString();
protected void btnCreate_Click(object sender, EventArgs e)
  //...some security checks
  SqlHelper mySqlHelper = new SqlHelper(connStringJobs, 
    CommandType.StoredProcedure, "insert_job",
    new SqlParameter("@schedule_type", dropListScheduleTypes.SelectedItem.Text),
    new SqlParameter("@description", txtDescription.Text),
    new SqlParameter("@monday", monday),
    new SqlParameter("@tuesday", tuesday),
    new SqlParameter("@wednesday", wednesday),
    new SqlParameter("@thursday", thursday),
    new SqlParameter("@friday", friday),
    new SqlParameter("@saturday" saturday),
    new SqlParameter("@sunday", sunday),
    new SqlParameter("@occurs_at", occurs_at));

The connStringJobs variable takes its value from the web.config file:

<add name="jobs" 

It has a hard coded username and password that also needs to be created in SQL Server as a database user login. My advice is to give admin permissions to itbecause this user will execute all the jobs within the JOB table. This is not the best security approach but if you plan to also run scripts in your own tasks, then you might encounter some permission problems when trying to run external scripts without giving admin rights to that user. If you just run Stored Procedures, then execute permission should be enough on related databases including the JOBS database. Execute permission is a must on the JOBS database.

The SqlHelper class should also be explained because it is not a standard C# class. It's my own collection of SQL tools. It simplifies the connection to a SQL Server and has many other helpful functions and methods that makes coding shorter. If anyone needs the source code for it, just ask me.


  1. Restore the JOBS.bak ( database into your SQL Server database. You can use the Microsoft SQL Server Management Studio Restore wizard for this. Right click on Databases and then click on "Restore database...". Then follow the on screen instructions. After you have successfully restored the database, you will see the above mentioned tables and Stored Procedures inside your JOBS database where you will also find some sample tasks which can be deleted later on in the administration page.
  2. Create a database user login inside the JOBS database and give admin permissions to it. Name it userreader, and for password, give nopass.
  3. Open the jobs.sln ( solution with Visual Studio and modify the two connection strings in the web.config file to meet your needs, then publish it to your site.
  4. Open your browser and navigate to the jobs admin page which you have just published and start creating and managing your own jobs.
  5. Create a job in Microsoft SQL Server Management Studio which contains only one step that runs the following Stored Procedure: [JOBS].[dbo].[begin_jobs], and create a schedule for this job that occurs every day in every 10 seconds.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mihaly Sogorka
Website Administrator
Hungary Hungary
Working with Microsoft Dynamics NAV since 2007.

Comments and Discussions

Questionabove web scheduler is very useful Pin
Member 1127170015-Apr-19 20:08
MemberMember 1127170015-Apr-19 20:08 
PraiseExcellent - KISS Pin
Nosnibor26-Oct-16 8:57
MemberNosnibor26-Oct-16 8:57 
Praisevery useful Pin
kamalkd29-Sep-16 11:40
Memberkamalkd29-Sep-16 11:40 
QuestionExcellent Pin
Luis Fernando Rivera Zapata12-Nov-13 6:16
MemberLuis Fernando Rivera Zapata12-Nov-13 6:16 
GeneralMy vote of 5 Pin
Prasanta_Prince29-Apr-12 19:40
MemberPrasanta_Prince29-Apr-12 19:40 
GeneralMy vote of 5 Pin
souloff14-Sep-11 13:14
Membersouloff14-Sep-11 13:14 
QuestionRun Script Pin
souloff8-Sep-11 20:45
Membersouloff8-Sep-11 20:45 
AnswerRe: Run Script Pin
Mihaly Sogorka9-Sep-11 10:14
MemberMihaly Sogorka9-Sep-11 10:14 
GeneralRe: Run Script Pin
souloff10-Sep-11 8:40
Membersouloff10-Sep-11 8:40 
GeneralRe: Run Script Pin
Mihaly Sogorka11-Sep-11 21:59
MemberMihaly Sogorka11-Sep-11 21:59 
GeneralRe: Run Script Pin
souloff12-Sep-11 0:45
Membersouloff12-Sep-11 0:45 
GeneralRe: Run Script Pin
Mihaly Sogorka12-Sep-11 21:33
MemberMihaly Sogorka12-Sep-11 21:33 
GeneralRe: Run Script [modified] Pin
souloff13-Sep-11 2:10
Membersouloff13-Sep-11 2:10 
GeneralRe: Run Script [modified] Pin
Member 1081982715-May-14 2:51
MemberMember 1081982715-May-14 2:51 
BugExcellent work Pin
souloff8-Sep-11 3:30
Membersouloff8-Sep-11 3:30 
GeneralRe: Excellent work Pin
Mihaly Sogorka8-Sep-11 3:44
MemberMihaly Sogorka8-Sep-11 3:44 
GeneralRe: Excellent work Pin
souloff8-Sep-11 4:39
Membersouloff8-Sep-11 4:39 
QuestionHow do you keep the app running? Making requests from sql server job? Pin
toebens15-Aug-11 6:06
Membertoebens15-Aug-11 6:06 
AnswerRe: How do you keep the app running? Making requests from sql server job? Pin
Mihaly Sogorka15-Aug-11 6:47
MemberMihaly Sogorka15-Aug-11 6:47 
Questiongood one ... Pin
Manish Langa26-Jul-11 19:01
MemberManish Langa26-Jul-11 19:01 
nice ...document

langa manish
QuestionQuartz.Net Pin
Member 342287726-Jul-11 17:16
MemberMember 342287726-Jul-11 17:16 
GeneralMy vote of 5 Pin
Prasanta_Prince24-Jul-11 19:44
MemberPrasanta_Prince24-Jul-11 19:44 
Question1 Question Pin
Xmen Real 22-Jul-11 15:46
professional Xmen Real 22-Jul-11 15:46 
AnswerRe: 1 Question Pin
Mihaly Sogorka22-Jul-11 20:07
MemberMihaly Sogorka22-Jul-11 20:07 
GeneralRe: 1 Question Pin
Xmen Real 22-Jul-11 21:38
professional Xmen Real 22-Jul-11 21:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.