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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sachinkale123
    New Member
    • Jul 2007
    • 37

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

    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.SqlS erver.SmoEnum)'

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

    My code is as follows:
    [code=cpp]
    Server srv = default(Server) ;
    SqlConnectionIn fo connInfo = new SqlConnectionIn fo("(local)", "sa", "1234");
    connInfo.Databa seName = "uhl_50";
    Microsoft.SqlSe rver.Management .Common.ServerC onnection svrConn = new ServerConnectio n(connInfo);

    srv = new Server(svrConn) ;


    JobServer jbSrv = srv.JobServer;

    Job jb = new Job(jbSrv, "Test_Job5" );



    jb.Create();

    JobStep jbstp = default(JobStep );
    jbstp = new JobStep(jb, "Test_Job_Step5 ");
    jbstp.SubSystem = AgentSubSystem. TransactSql ;
    jbstp.Command = @"insert into uhl_50.dbo.jobt est values ('kal')";
    jbstp.OnSuccess Action = StepCompletionA ction.QuitWithS uccess;
    jbstp.OnFailAct ion = StepCompletionA ction.QuitWithF ailure;
    //Create the job step on the instance of SQL Agent.
    jbstp.Create();
    //Define a JobSchedule object variable by supplying the parent job and name arguments in the constructor.
    JobSchedule jbsch = default(JobSche dule);
    jbsch = new JobSchedule(jb, "Test_Job_Sched ule5");
    //Set properties to define the schedule frequency, and duration.
    jbsch.Frequency Types = FrequencyTypes. OneTime;

    TimeSpan ts1 = default(TimeSpa n);
    ts1 = new TimeSpan(18, 18, 0);
    jbsch.ActiveSta rtTimeOfDay = ts1;

    System.DateTime d = default(System. DateTime);
    d = new System.DateTime (2008, 11, 17);
    jbsch.ActiveSta rtDate = d;

    jbsch.Create();
    jbsch.Refresh() ;[/code]
    Last edited by Frinavale; Nov 17 '08, 03:12 PM. Reason: added [code] tags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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?

    Comment

    • sachinkale123
      New Member
      • Jul 2007
      • 37

      #3
      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.

      Comment

      • sachinkale123
        New Member
        • Jul 2007
        • 37

        #4
        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.ApplyToTarg etServer("Serve rName");
        2) Job.Alter();

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

        best luck..

        Comment

        • balabaster
          Recognized Expert Contributor
          • Mar 2007
          • 798

          #5
          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...

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            I assume you've come across this article:




            [Edit] Scrap that...

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Originally posted by balabaster
              Plater, does express have SQL Server Agent?
              Best I can tell the answer to that is "no"

              Comment

              • balabaster
                Recognized Expert Contributor
                • Mar 2007
                • 798

                #8
                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.

                Comment

                Working...