Find current Server and Database Name

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

12 Replies to “Find current Server and Database Name”

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

  2. 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]

  3. 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’);

  4. Thanks for the solution.But how to get ip address of server. My concern is wanted to open a corresponding Page from report. Its working fine when i access from server machine but when i access from client machine its not working. What would be the issue. I think the problem is because of Server Computer name instead of IP Address. How can i get IP Address of server? Thanks.

    Regards,
    Priya

Leave a Reply