SQL: Microsoft Dynamics NAV "kill" an idle session

One issue with applications that use a concurrent license model is the dreaded “idle user”. This is the user that had gone to lunch, to a meeting or even home for the and didn’t “logout” of the application. Under the concurrent license model this user does take up a user session, which prevents other users from accessing the application if the maximum number of seats has been reached or lead to the purchase of additional and unnecessary user licenses due to the perception of not enough seats.

Microsoft Dynamics NAV is an example of application that uses a concurrent license model and is also one application that people often ask “How can I disconnect an idle user?” While there isn’t a magic “kill idle” user setting there are a several options available. A few of these options are:

  • If the users are using Remote Desktop you can set an idle timeout to force a disconnect or manually force a disconnect of idle remote users
  • Manually remove idle users from the Microsoft Dynamics NAV session list
  • Kill a user’s SQL Server connection

Let’s jump into the last option that is listed above. In Microsoft SQL server there isn’t an “idle time” value defined for a user, however there is a last_batch value that indicates the last time the users session sent an execution to the SQL server. With that information we can easily create (and schedule) a script that can compare the current date and time to the last_batch value to see how long a user has been “idle” and kill their session if it is over a certain amount of time (script should be adjusted for your implementation):

set nocount on;
set ansi_padding on;
set ansi_nulls on;
set quoted_identifier on;

-- declare variables.
declare @spid as varchar(20)
declare @loginname as varchar(255)
declare @kill as int
declare @cmd as varchar(max)

-- set the variable @kill to 1 to actually perform the “kill or set it to 0 to see a list of users that
-- would be killed
set @kill = 0

declare users insensitive scroll cursor
            -- NAV Db Name    
            (db_name(dbid) = N'CRONUS')
            and (program_name = N'Microsoft Dynamics NAV Classic client')
            -- number of seconds idle -- remember this does not mean there isn't a long job running
            -- 3600 is 1 hour
            and (datediff(ss, last_batch, getdate()) > 3600)

open users
while (1=1)
            fetch next from users into @spid, @loginname;
            if @@fetch_status <> 0 break;
            set @cmd = 'kill ' + @spid
            if @kill = 1 
              exec (@cmd)
              print @cmd + N' ' + @loginname;

close users;
deallocate users;



One thing to point out is that a user may be idle because their session is performing a lengthy operation and they have not had any session activity while they are awaiting results for their command. There tends to be some disconnect and excitement when someone is in the middle of some operation and their session is killed.

It should be evident that there is a risk when abnormally terminating a user’s session. There is the risk of data loss or corruption so be certain to understand the environment that you’re in and the implications of terminating a user’s session in a not so elegant way (i.e. properly logging off).

SQL: Check if a File Exists

There may be a case where a SQL table contains a field that references a filename that is stored on the file system. The file could be an image (for example in the case of a web application that displays images) or another document that is linked from an application. To avoid errors it may be necessary to verify the existence of the file (i.e. the file exists). Fortunately, this is easily done with the xp_fileexist command. The xp_fileexist command returns an integer result that indicates if the file exists. The usage of the command is:
EXECUTE xp_fileexist [, OUTPUT]
The following script shows and example of looping through a set of records and validating, with xp_fileexist, that a file exists. If it doesn’t exist it updates the field with another value (if you are referencing images it could be a default image filename).
set nocount on;
set ansi_padding on;
set ansi_nulls on;
set quoted_identifier on;

-- declare variables.
declare @item as varchar(255)
declare @filename as varchar(255)
declare @filepath as varchar(255)
declare @cmd as varchar(1024)
declare @result as int

-- declare a cursor for storing the records in the table.
declare items insensitive scroll cursor
		,[File Name]
		,[File Path]
	from  [Table1]
	order by 
		[File Name]

-- open the cursor.
open items

-- loop through all the indexes.
while (1=1)
		fetch next from items into @item, @filename, @filepath;
		if @@fetch_status <> 0 break;
		set @filepath = @filepath + @filename
		exec master.dbo.xp_fileexist @filepath, @result OUTPUT
		if (@result = 0) -- image does not exist
				--print (@result)			
				-- Update the record to point to a generic image
				set @cmd = 'UPDATE [Table1] SET [File Name] = ''NA.jpg'' WHERE [Item] = ''' + @item + ''''
				-- print (@cmd)
				exec (@cmd)

-- close cursor
close items;
deallocate items;


Microsoft Dynamics NAV: Activity Button Images (RTC)

When adding a new Activity Button to a page in the Role Tailored client (RTC) the default image is basic and a bit boring. The image property of the action is used to specify the image that is displayed for the action. Where is the list of images? Microsoft was kind enough to place a list of the images on MSDN (http://msdn.microsoft.com/en-us/library/dd568709.aspx).

Activity Button Image Library

Installing Windows 2012 Server in Hyper V

In order to become familiar with Windows Server 2012 and ensure application compatibility I decided to install Windows Server 2012 within a Hyper V machine. Virtual Machines are wonderful. The ability to create, snapshot, and restore “machines” without having to having all this hardware is wonderful.

I downloaded the Windows Server 2012 ISO from MSDN and attached it to DVD Drive media of a newly created virtual machine. I booted the machine and started down the road of a Windows Server installation.

Windows Server 2012 Installation Screen

The process started ok, however once I started the installation I received a driver error:

"A media driver your computer needs is missing. This could be a DVD, USB, or Hard disk driver. If you have a CD, DVD, or USB flash drive with the driver on it, please insert it now."

Windows Server 2012 Installation Error

I reviewed all of the settings, hardware configurations and even verified that Windows Server 2012 runs within Hyper V. Everything checked out and I started the install again and received the same error. Needless to say it was a bit frustrating. The physical server is only 5 months old and I have installed several other test servers, from ISO files, running in Hyper V without an issue. I was pretty comfortable with the hardware configurations and even verified the ISO file was valid.

After several failed attempts to load the server from the ISO file I decided to burn the ISO to a DVD. I then loaded the DVD into the server and attached the physical DVD drive media to the virtual machine. This time the installation process started and finished without an issue.

It seems that the installation was not able to process/cache the full tree of files and using a physical disk did the trick.

Microsoft Dynamics NAV: Excel Automation AddPicture

The ability to use Automation Objects has added to the power and flexibility of Microsoft Dynamics NAV.  Automation can be used to integrate NAV business logic with external processes and extend the functionality of the product, such Microsoft Office.  This is easily demonstrated through the use of Table 370, Excel Buffer.  Using the “Excel Buffer” table you can easily create Excel Workbooks with Dynamics NAV data.

One thing that is not included in the “Excel Buffer” table, which could be useful, is the option to insert a picture into a worksheet.  Fortunately, the Excel Automation control exposes the Shapes.AddPicture method than can be easily extended into the Dynamics NAV “Excel Buffer” table by creating a function for XlWrkSht.Shapes.AddPicture(filename,1,1,Left,Top,Width,Height);


This method can be implemented several ways and it is easy enough to add a field ImageName to the table.  The CreateSheet code can then be altered to add an image to the current sheet based on the value entered in the field. 


Reading Stuff

Information in this document subject to change without notice.
All Software source code published is for demonstration and knowledge sharing purposes only. The Code is supplied "as is" without warranty as to result, performance or merchantability. Use at your own risk.
The opinions expressed herein are the opinions of the author and do not reflect those of any other entity.