Connecting Tech Pros Worldwide Forums | Help | Site Map

Error when I add job to a scheduler in SQL Server 2005 programatically

Member
 
Join Date: Jul 2007
Posts: 37
#1: Nov 17 '08
Hi,
I am developing one web application where I am adding a job to sql server 2005 scheduler programatically. When I run code i m able to add job to schedular but it does not execute on time.
If I create the same job manually, job executes on time.

When saw differences between these two jobs I found out that
Job created manually has following additional value: When I click on 'Job Referencing a Schedule' i see Reference between Schedule and Job created but
but Job created Programatically gives an error as 'Unknown property Id (Microsoft.SqlServer.SmoEnum)'

I have wasted to much of time on this so please help me to resolve this error.

My code is as follows:
Expand|Select|Wrap|Line Numbers
  1. Server srv = default(Server);
  2.         SqlConnectionInfo connInfo = new SqlConnectionInfo("(local)", "sa", "1234");
  3.         connInfo.DatabaseName = "uhl_50";
  4.         Microsoft.SqlServer.Management.Common.ServerConnection svrConn = new ServerConnection(connInfo);
  5.  
  6.         srv = new Server(svrConn);
  7.  
  8.  
  9.         JobServer jbSrv = srv.JobServer;
  10.  
  11.         Job jb = new Job(jbSrv, "Test_Job5"); 
  12.  
  13.  
  14.  
  15.         jb.Create(); 
  16.  
  17.         JobStep jbstp = default(JobStep); 
  18.         jbstp = new JobStep(jb, "Test_Job_Step5");
  19.         jbstp.SubSystem = AgentSubSystem.TransactSql ;
  20.         jbstp.Command = @"insert into uhl_50.dbo.jobtest values ('kal')"; 
  21.         jbstp.OnSuccessAction = StepCompletionAction.QuitWithSuccess; 
  22.         jbstp.OnFailAction = StepCompletionAction.QuitWithFailure; 
  23.         //Create the job step on the instance of SQL Agent. 
  24.         jbstp.Create(); 
  25.         //Define a JobSchedule object variable by supplying the parent job and name arguments in the constructor. 
  26.         JobSchedule jbsch = default(JobSchedule); 
  27.         jbsch = new JobSchedule(jb, "Test_Job_Schedule5"); 
  28.         //Set properties to define the schedule frequency, and duration. 
  29.         jbsch.FrequencyTypes = FrequencyTypes.OneTime; 
  30.  
  31.         TimeSpan ts1 = default(TimeSpan); 
  32.         ts1 = new TimeSpan(18, 18, 0); 
  33.         jbsch.ActiveStartTimeOfDay = ts1; 
  34.  
  35.         System.DateTime d = default(System.DateTime); 
  36.         d = new System.DateTime(2008, 11, 17); 
  37.         jbsch.ActiveStartDate = d; 
  38.  
  39.         jbsch.Create();
  40.         jbsch.Refresh();

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Nov 17 '08

re: Error when I add job to a scheduler in SQL Server 2005 programatically


I have been scratching my head trying to figure out how to add a "job" to sql server 2005 with the sql server management studio (I have management studio express, is that why?)
I figure I could add a job using their wizzard, then right click on the object and chose to script the creation to the clipboard....
But I don't even see a place where cron jobs would be stored?
Member
 
Join Date: Jul 2007
Posts: 37
#3: Nov 18 '08

re: Error when I add job to a scheduler in SQL Server 2005 programatically


Now I could able to create job in schedular but it is not running at time where I have set the time to run. If i run it manually it runs. but does not run on scheduled time.
Member
 
Join Date: Jul 2007
Posts: 37
#4: Nov 18 '08

re: Error when I add job to a scheduler in SQL Server 2005 programatically


Now I could able to add job to schedular and it runs on its own on scheduled time. Two this which i was missing in above code were.

1) Job.ApplyToTargetServer("ServerName");
2) Job.Alter();

both of these function you need to write at the end of above code.

best luck..
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#5: Nov 18 '08

re: Error when I add job to a scheduler in SQL Server 2005 programatically


Quote:

Originally Posted by Plater

I have been scratching my head trying to figure out how to add a "job" to sql server 2005 with the sql server management studio (I have management studio express, is that why?)
I figure I could add a job using their wizzard, then right click on the object and chose to script the creation to the clipboard....
But I don't even see a place where cron jobs would be stored?

Plater, does express have SQL Server Agent?

If it does, then check this out - that'll help you do it manually...

http://msdn.microsoft.com/en-us/library/ms190268.aspx
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#6: Nov 18 '08

re: Error when I add job to a scheduler in SQL Server 2005 programatically


I assume you've come across this article:

http://msdn.microsoft.com/en-us/library/ms135739.aspx


[Edit] Scrap that...
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#7: Nov 18 '08

re: Error when I add job to a scheduler in SQL Server 2005 programatically


Quote:

Originally Posted by balabaster

Plater, does express have SQL Server Agent?

Best I can tell the answer to that is "no"
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#8: Nov 18 '08

re: Error when I add job to a scheduler in SQL Server 2005 programatically


Quote:

Originally Posted by Plater

Best I can tell the answer to that is "no"

Well I'm going to go with you can't create jobs manually with SQL Server Express then as they're in the SQL Server Agent :oP

I had a play around with this earlier and I couldn't figure out what was going wrong. I came across a couple of articles the pointed to this being a known problem that Microsoft had a fix for - but it pertains to SQL Server 2008, not 2005. So I'm not sure if that bug was a carry over from 2005 or if it wasn't supposed to exist in 2005. I've not been able to figure out if there's a fix for this as yet. I'll keep digging though.
Reply