2005/09/16

Is this job still executing?

It is easy to use Enterprise Manager to check whether a job is still executing or not.

But I needed to get this information programmatically.

This is a small stored procedure that tells whether a job is running or not.
CREATE PROCEDURE dbo.IsJobRunning
(
  @job_name varchar(255),
  @is_job_running int OUTPUT
)
AS
  BEGIN
    DECLARE @job_id uniqueidentifier 
    
    CREATE TABLE #xp_results 
    (
      job_id uniqueidentifier NOT NULL,
      last_run_date int NOT NULL,
      last_run_time int NOT NULL,
      next_run_date int NOT NULL,
      next_run_time int NOT NULL,
      next_run_schedule_id int NOT NULL,
      requested_to_run int NOT NULL, -- BOOL
      request_source int NOT NULL,
      request_source_id sysname COLLATE database_default NULL,
      running int NOT NULL, -- BOOL
      current_step int NOT NULL,
      current_retry_attempt int NOT NULL,
      job_state int NOT NULL
    )
    
    SELECT @job_id = sj.job_id 
    FROM msdb.dbo.sysjobs sj
    WHERE sj.name = @job_name
    
    INSERT INTO #xp_results
    EXEC master.dbo.xp_sqlagent_enum_jobs 1, ''

    SELECT @is_job_running = running
    FROM #xp_results
    WHERE job_id = @job_id
  END
After a few days of struggling I think this is the best way to do it.

2005/09/15

How to insert multiline text when Form.AcceptButton is set?

I've just re-discovered it - I don't like re-discovering things. I have spent an hour trying to solve this problem and once I've figured out how to do it, I've realized that I had had exactly the same problem about a month ago and I had already worked it out back then.

Anyway, let's describe the problem first:
1. There is a form - myForm.
2. There is a TextBox (or RichTextBox) control on myForm - myText. There is also a Button - myButton - on the same form.
3. The AcceptButton property of myForm is set to myButton.
4. The myText.Multiline is set to true. However, pressing Enter key does not insert a newline, but activates myButton instead.

The solution is simple:
1. In myText.GotFocus (or myText.Enter) event set myForm.AcceptButton to Nothing (VB) or null (C#).
2. In myText.LostFocus (or myText.Leave) event set myForm.AcceptButton back to myButton.

2005/08/14

Why can't I decrypt a string I've just encrypted?

If you are using one of .NET CryptoService providers (like DESCryptoServiceProvider or RijndaelManaged), there's a trap that you can easily fall in (I did).

To encrypt data (a string converted previously into a byte array), you create a CryptoStream that reads bytes from this array, transforms them, and then writes encrypted data to a stream.

Then you can use ToArray() method of the stream to obtain encrypted bytes.

And what do you do next?

You run something like:
  Encoding.Unicode.GetString(cipherBytes)

And you have a problem. The encrypted string is encoded in Unicode. That's fine as long as you want to write it into a file or store in a database, but sometimes you want to be able to enter it using the keyboard.

To solve this problem use:
  Convert.ToBase64String(cipherBytes)

This way the encrypted string will be encoded with ASCII characters only.

If you want to know more, read this article.

2005/06/25

DTS packages & jobs

I created a simple DTS package, I executed and it worked just fine.

Then I scheduled it and tried to execute the job. The job failed. It consisted only of one step which executed the package, so I was pretty speechless.

I opened job's history. It looked pretty awful (many lines of text without newline characters), but I managed to find a few occurrences of 'Access denied' phrase.

Then I remembered that there was something like 'Script file directory' textbox in 'Copy SQL Server Objects Task' window. I also realized that the domain account that I use to run SQL Server Agent service has very limited permissions.

I changed the path in 'Script file directory' textbox to one that SQL Server Agent could access and this time the job executed.