Problems with C # with Task Scheduler Library

I went every time for this and cannot understand what I am doing wrong. I am using the library Microsoft.Win32.TaskScheduler. Every time I look at the code, my task is always zero. Here is the code.

using (SqlConnection myConnection2 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString))
{
    myConnection2.Open();
    SqlCommand cmd2 = new SqlCommand("sp_GetProcessStart", myConnection2);
    cmd2.CommandType = CommandType.StoredProcedure;
    cmd2.Parameters.AddWithValue("@ID", txtTaskID.Text);

    SqlDataReader rdr = cmd2.ExecuteReader();

    if (rdr.HasRows)
    {
        while (rdr.Read())
        {
            string task = rdr["TaskName"].ToString();
            string newName = task.Remove(task.Length - 4, 4);

            using (TaskService ts = new TaskService())
            {
                Task t;
                t = ts.FindTask(newName);
                if (t != null)
                {
                    t.Run();
                }
            }
        }
    }
}

Stored procedure SELECT ID FROM TABLE WHERE ID = @ID. The table has all mine ScheduledTaskswith an identifier assigned to each. However, the table saves them as "Task.job", so I delete the ".job". No matter what I do, t is always null, and I can understand why.

+3
source share
2 answers

I have earned. Come to find out that I am not pulling a server into my variable so that it does not know what task to perform.

0
source

Do not remove the .job extension. It will find your jobs only with the extension .job.

If this does not work, try using

OpenTask("jobname");
0
source

All Articles