VS Tools - Getting the UserID/SQLServer/Database

I've written on this in the past, but I've fine tuned my approach. My older approach had trouble with the WinForms test harness that I added to some VS Tools projects to test forms. This works better and doesn't have that drawback.

The issue is that we need the currently logged on user and the SQL Server/Database to run stored procedures from within VS Tools.

You can't go looking for it too early in the GpAddIn.vb class, the code might run before the user has logged in and selected a database.

This article detail the approach

How to get the UserID/Database/SqlServer in VS Tools.

We need to wait a little bit to go looking for these items, GpAddIn.vb might Initialize before the user has logged in and selected a database. Too, what if the user changes databases while logged in?

First, here is the App class that I bring into every project:

 

Public Class App
    Public Shared AppName As String
    Public Shared StartupPath As String = CurDir()
    Public Shared SQLServer As String
    Public Shared Database As String
    Public Shared UserID As String
End Class

Now, here is a sample GpAddIn.vb class. Note that we set an event handler for the Toolbar.OpenAfterOriginal event. When the toolbar opens, we get the UserID/SQLServer/Database and write them to the App class. They're easily referenced by the application then.

In production code I put the toolbar handler in a separate class; but it works for the demo a little easier this way. 

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports Microsoft.Dexterity.Bridge
Imports Microsoft.Dexterity.Applications
Imports FPCommon
 
Public Class GPAddIn
    Implements IDexterityAddIn
 
    ' IDexterityAddIn interface
 
 
    Sub Initialize() Implements IDexterityAddin.Initialize
 
        'register an event handler for the Toolbar.OpenAfterOriginal event
        AddHandler Dynamics.Forms.Toolbar.OpenAfterOriginal, AddressOf ToolbarOpenAfterOriginal
 
    End Sub
 
    Sub ToolbarOpenAfterOriginal(ByVal sender As ObjectByVal e As System.EventArgs)
        App.AppName = "MyAppName"
        App.Database = Dynamics.Globals.IntercompanyId.Value
        App.UserID = Dynamics.Globals.UserId.Value
 
        'get the server
        Dim backup As Microsoft.Dexterity.Applications.DynamicsDictionary.SyBackupRestoreForm
        backup = Microsoft.Dexterity.Applications.Dynamics.Forms.SyBackupRestore
        App.SQLServer = backup.Functions.GetServerNameWithInstance.Invoke
    End Sub
End Class

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences