Here are my way of finding the current database server and database name for both Classic Client and RTC Client.
[code htmlscript=”false”]IF ISSERVICETIER THEN BEGIN

IF ISCLEAR(DomDoc) THEN
CREATE(DomDoc);

DomDoc.load(APPLICATIONPATH + ‘CustomSettings.config’);
DomNode :=
DomDoc.selectSingleNode(
‘//appSettings/add[@key=”DatabaseServer”]’);
MyServerName := DomNode.attributes.item(1).text;

DomNode :=
DomDoc.selectSingleNode(
‘//appSettings/add[@key=”DatabaseName”]’);
MyDatabaseName := DomNode.attributes.item(1).text;

END ELSE BEGIN
MyServer.SETRANGE("My Server",TRUE);
MyServer.FINDFIRST;
MyServerName := MyServer."Server Name";

MyDatabase.SETRANGE("My Database",TRUE);
MyDatabase.FINDFIRST;
MyDatabaseName := MyDatabase."Database Name";
END;[/code]
The Global Variables are

Name DataType Subtype Length
DomDoc Automation ‘Microsoft XML, v6.0’.DOMDocument
DomNode Automation ‘Microsoft XML, v6.0’.IXMLDOMNode
MyServerName Text 50
MyDatabaseName Text 50

10 thoughts on “Find current Server and Database Name

  1. This does not work when RTC is started using custom config in shortcut startup parameters.

    1. Yes it does. This reads from the server config file, not the client config file.

  2. Stephen says:

    Nice solution.. thanks !

  3. Nico says:

    Hello what tables do the MyServer and MyDatabase point to? could you add them to your setup list. Thank you for the great help

    1. Server is table 2000000047 and Database is table 2000000048

      1. Nico says:

        Fantastic!! Thank you

  4. Tõnu says:

    First a thanks and then a small update/remark (4 years later :)) for 2009 users/coders
    P.S The errors on display below are very optional and extremely rare.

    [code language=”C/AL”]

    //create automation
    IF ISCLEAR(_XmlDoc) THEN
    IF NOT CREATE(_XmlDoc) THEN //false, false – onserver and reuse
    ERROR(‘Cannot Create XML service (Error in GetNavDbServerAndDbName (…))’);

    _XmlDoc.load(APPLICATIONPATH + ‘Microsoft.Dynamics.Nav.Server.exe.config’);

    _XmlDocNode :=
    _XmlDoc.selectSingleNode(‘//configuration/appSettings’);

    //check if XmlDoc was found – if not path to single node cannot be found
    IF ISCLEAR(_XmlDocNode) THEN
    ERROR(‘Cannot Find Server Exe Config file (Error in GetNavDbServerAndDbName (…))’);

    //’CustomSettings.config’;
    _ClientConfigFileNameWithPath := _XmlDocNode.attributes.item(0).text;

    //check if path is local (like std. or full)
    IF STRPOS(_ClientConfigFileNameWithPath,’\’) = 0 THEN
    _ClientConfigFileNameWithPath := APPLICATIONPATH + _ClientConfigFileNameWithPath;

    _XmlDoc.load(_ClientConfigFileNameWithPath);

    //code continues like Gunnar’s

    [/code]

  5. Christian says:

    Thank you for publishing this solution, it works fine if if no instances are used.
    When run on a instance it always returns the configuration of the service.
    It seems there is no way to find out if the application runs on a instance and therefore load the configuration from the instance directory.
    Do you have any idea?

    Kind regards,
    Christian

    1. You can try

      IF ServerFile.Exists(APPLICATIONPATH + ‘Instances\’ + ActiveSession.”Server Instance Name” + ‘\CustomSettings.config’) THEN
      XMLDoc.Load(APPLICATIONPATH + ‘Instances\’ + ActiveSession.”Server Instance Name” + ‘\CustomSettings.config’)
      ELSE
      XMLDoc.Load(APPLICATIONPATH + ‘CustomSettings.config’);

Leave a Reply

%d bloggers like this: