Tuesday, 27 September 2011

Dynamics AX [SQL Native Client] Connection is busy with results for another command

Earlier today hit an error when trying to do an insert while looping through a ResultSet returned from stored procedure executed from Dynamics AX. The error was "[SQL Native Client]Connection is busy with results for another command".

A quick fix to it is to close the connection before the INSERT.

============================================================================

Connection       sQLConnection;
Statement        sQLStatement;
str              sql;
TestTableA       testTableA;
RecordInsertList rilTestTableA  = new RecordInsertList(tableNum(TestTableA));
boolean          recordInserted = false;
;

sql = strFmt("EXEC testStoredProc '%1'", dataAreaId);

new SqlStatementExecutePermission(sql).assert();

sQLConnection = new Connection();
sQLStatement  = sQLConnection.createStatement();
resultSet     = sQLStatement.executeQuery(sql);

CodeAccessPermission::revertAssert();

while(resultSet.next())
{
    recordInserted = true;

    testTableA.clear();
    testTableA.Field1 = resultSet.getInt(1);
    testTableA.Field2 = resultSet.getString(2);

    rilTestTableA.add(testTableA);
}

sQLStatement.close(); //Close the connection before the insert else it will
                      //cause the "SQL Native Client]Connection is busy with
                      //results for another command" error

if(recordInserted)
{
    rilTestTableA.insertDatabase();
}
==============================================================================

You might want to consider turning off the row count in stored procedure as well else you'll get an extra result which is the row count.

Field1      Field2
----------- ----------
100001      TestData1
100002      TestData2

(2 row(s) affected)


Use "SET NOCOUNT ON" to turn it off in your stored procedure.
Eg.

CREATE PROCEDURE [dbo].[testStoredProc]

    @DATAAREAID NVARCHAR(4)
AS


SET NOCOUNT ON

SELECT Field1, Field2 FROM SourceTableA

GO


*Updated 02/10/2012
One of my co-worker told me if this issue occur due to running stored procedure, run it without EXEC keyword will resolve it as well. I haven't test this yet but thought I should just update this post.
Eg. EXEC sp_testStoredProc 'param'

Wednesday, 21 September 2011

Classic explorer UP button / status bar free space

If these questions brought you here, you probably need "Classic Explorer":
- Missing UP button in Windows Explorer
- Missing status bar free space in Windows Explorer

In Windows Vista, Windows 7 or Windows Server 2008 / 2008 R2, two of the useful features has been removed from Windows Explorer, they're the "UP" button and "Status bar free space".

Classic Explorer return these features to you, download and install it, the "UP" button and "Free space" will show up the next time you start up Windows Explorer.


BeforeAfter



After installation and restart Windows Explorer, if the features still doesn't shows up, you might need to enable it. Another reason would be when you're still new with it, you might have accidentally & unconciously disabled it.

Follow these steps to enable/show it:
  1. Open Internet Explorer
  2. Right click on the toolbar and click on "Classics Explorer Bar"


  3. It will prompt and ask whether you want to enable the Add-on, check the "ExplorerBHO class" and click "Enable"


If you're on server and the above doesn't seems to works for you, you might want to enable browser extention as well.
  1. Go to Control Panel > Internet Options > Advanced (tab)
  2. Check the option "Enable third-party browser extensions" and click OK

Monday, 19 September 2011

Reset Nokia E71 phone


Lost my HTC Desire last week at Copenhagen, sad case.
Was given a replacement this week - Nokia E71, felt like back to stone-age.
Will temporarily use it till Android Ice-Cream-Sandwich is released.


I found that the menu is not user friendly, for those who look for quick reset of the phone, here's the key:

  1. *#7780#
    Enter that into your phone using the keypad, then it will prompt for confirmation to reset to factory default. This only reset all the settings to default factory settings, it doesn't wipe out the data.
  2. Change to "General" profile
  3. *#7370#
    Enter that into your phone using the keypad, then it will prompt for confirmation to format the phone and wipe out the data on the phone (but not on the memory card). It will restart the phone when it is done.

In case you need the default security key/pin, it is: 12345



Friday, 9 September 2011

Swap ThinkPad Fn & Ctrl key


For those who hate the position of the Fn & Ctrl key on the new ThinkPad, follows this steps to swap them.

  • Boot up your machine
  • Press the blue ThinkVantage button
  • Press F1 into BIOS
  • Goto Config > Keyboard and Mouse > Fn and Ctrl Key Swap
  • Change it to “Enabled”
  • Hit “F10” to save and exit

Wednesday, 7 September 2011

Windows Server 2008, interact with session 0 UI (net start ui0detect)

net stop ui0detect
net start ui0detect

====================

In Windows Server 2003, you can connect to console session of the server by 'mstsc.exe /console', but this has been deprecated in Windows Server 2008. So, what happens if you have some Windows services which sometimes prompt up dialogue at the console session? If you’re new to Windows Server 2008 and coming from 2003, you might stuck for a while thinking how you can get to those dialogue, worry not, it has a built-in capability which enables you to view and to interact with the session 0 UI from your session.

“if the service that is associated with an application tries to display UI elements in session 0, a built-in capability in Windows Server 2008, Windows Server 2008 R2 and in Windows Vista enables you to view and to interact with the session 0 UI from your session”.

Let’s get straight to our scenario.

One of our client is using Dynamics AX2009, although AX2009 runs batch within AOS, but there’re certain batch job won’t works that way. Eg. A batch job which instantiate Excel file, read and write to it.
It has to be run using the legacy batch framework. We use FireDaemon to setup AX client to run as a Windows service. In Windows Server 2003, the AX client will start up at session 0, if you need to interact with this AX client, you can connect to it through 'mstsc.exe /console'. But the session 0 has been made non-interactive in Windows Server 2008 and the AX client runs in that session, so how do we connect to it when we need to look at the AX client?

This is what we do:
1. Logon to the server
2. Run this command in command prompt
    (you’ll need to start the command prompt in elevated mode – right click, Run as administrator)

    IF “Interactive Services Detection” is disabled
    (enable and start it)
   - sc config UI0Detect start= auto
   - net start ui0detect

    IF “Interactive Services Detection” is enabled/manual
    (restart it)
   - net stop ui0detect
   - net start ui0detect

What it actually do is to start the UI0Detect service and prompt the user if there's a dialogue in session 0. The prompt allow user to switch from current session into the "special desktop" of the session 0 which only has interaction with the application who trigger the dialogue.

"Interactive Services Detection", this is the "UI0Detect.exe"

Running the "net stop ui0detect" and "net start ui0detect" will prompt up a window allowing you to switch to the session 0 UI. Click on the "Show me the message"

When you're done, just click on "Return now" to return to your normal desktop

Example of the UI in session 0. In this session, you only see that application itself, there won't be any Start menu, taskbar, & etc.

More references at:








Wednesday, 31 August 2011

Open document handling (DocuView) for other datasource/table than caller

Sometimes there's requirement to open document handling from one form to shows the documents for other datasource/table.


Eg. From Purchase order form, open the document handling of the vendor (VendTable) instead of for the purchase order (PurchTable).


It can be achieved with the following 4 steps:
  1. Add the related table as datasource in the form
  2. Add a boolean global variable to 'ClassDeclaration'
  3. Overwrite the method 'docCursor'
  4. Add a button and overwrite the 'clicked' method

1. Add the related table as datasource in the form




2. Add a boolean global variable to 'ClassDeclaration'
class FormRun extends ObjectRun
{
    boolean      vendDoc;
}


3. Overwrite the method 'docCursor'
public Common docCursor()
{
    Common docCursor;
    ;


    docCursor = super();


    if(vendDoc)
        docCursor = vendTable;
    
    return docCursor;
}


4. Add a button and overwrite the 'clicked' method
void clicked()
{
    super();


    vendDoc = true;


    if (!infolog.docu().isDocuViewSet())
        infolog.docu().note(element);
    else
        infolog.docu().setActive();


    vendDoc = false;
}

Wednesday, 6 July 2011

Client/local printer not available at Terminal Services Session/RDP

There’re several different reasons when local printer is not available at Terminal Services Session, below are my check list, hope it helps.


#1. Printer redirection is disabled at Terminal Server
First thing to check is whether the server has disabled the printer redirection.
Also ensure it enable the use of user settings.


Path: Control Panel > Administrative tools > Terminal Services Configuration


Check group policy
Path: Start > Run > gpedit.msc > Local Computer Policy > Computer Configuration > Administrative Templates > Windows Components > Terminal Services > Client/Server data redirection
Ensure the printer redirection is not disabled.


Reference:
http://social.technet.microsoft.com/Forums/en/winserverTS/thread/619d78a1-dc47-45cf-a078-e9fba492dd1f
http://social.technet.microsoft.com/Forums/en/winserverTS/thread/4ebf9a34-85f3-4b91-9750-48124b9885ba


#2. Local resources (Printer) is not checked in the RDP connection settings
Ensure the “Printers” checkbox is checked under the “Local devices and resources” group.


#3. Printer driver is not available at Terminal Server
In certain cases, the issue was caused by printer driver is not available at the server, you might want to install it there.


Reference: http://technet.microsoft.com/en-us/library/cc758836(WS.10).aspx


#4. Printer is not using COM, LPT, or USB port
Refer to more details at http://support.microsoft.com/kb/302361.

Monday, 13 June 2011

Candidate for EntireTable CacheLookup on Dynamics AX

When deciding which table is suitable for EntireTable caching in Dynamics AX, the rule is the 128k size limit (http://msdn.microsoft.com/en-us/library/bb314630(v=AX.50).aspx). With the note in MSDN - "Avoid using EntireTable caches for large tables because once the cache size reaches 128 KB the cache is moved from memory to disk. A disk search is much slower than an in-memory search.", so, how do we decide which table should be cached? The easiest way is to look at the page count of the table itself. In SQL Server, 1 page == 8kb, which means 128k == 16 pages. With the help of 'sys.dm_db_index_physical_stats' we can list out all the tables that is less than or equal 16 pages, these will be the candidate for further analysis. Do not change them all to EntireTable CacheLookup, there're further analysis required.


USE <databasename>
GO
SELECT OBJECT_NAME(object_id),
       [object_id],
       index_id,
       index_level,
       page_count,
       avg_fragmentation_in_percent
  FROM sys.dm_db_index_physical_stats(DB_ID('<databasename>'),NULL,NULL,NULL,DEFAULT)
 WHERE page_count  > 0
   AND page_count <= 16
GO


If the system is still in implementation phase, most likely the data is quite crappy, insufficient, or with just a small set of test records, they're not helping in decision making. With the list of tables returned from the above query, do further analysis and ask yourself:
- Is the table potentially growing larger over time?
- Are the data quite static? (avoid doing EntireTable cache on small table, but has high change rate)
- Is the table (unique) index correctly set? (Eg. to the ::find() method or other query's unique criteria)

There's one thing that I'm not quite sure yet, that is whether the 128k limit is per index or per table.
You can test it out by capturing the trace and look at it with Trace Parser.


*NOTE: The query took around 30 minutes to run on my system (~500GB database), you might want to choose a better timing to run it.


To run the query against one single table, replace the second parameter to the object Id of the table.


From: sys.dm_db_index_physical_stats(DB_ID('<databasename>'),NULL,NULL,NULL,DEFAULT)
To: sys.dm_db_index_physical_stats(DB_ID('<databasename>'),OBJECT_ID('<tablename>'),NULL,NULL,DEFAULT)