Setting Database Connections at Run-time in Iron Speed Designer
Learn how to customize your application to support dynamic database connections.
- Jing Ding, Senior Systems Consultant for The Ohio State University Medical Center

June 2, 2008
Iron Speed Designer V5.X
Introduction
In this article, I will show you how to set a database connection string at application run-time.

In Iron Speed Designer-generated applications, the database connection string is specified at design time. The application is connected to the database as the developer or as a generic user account. For some applications, it is desirable to make the database connection for individual logged in users, whose credentials are only available at application run-time. While Iron Speed Designer does not directly support dynamic database connections, you can customize your application to do so.

Solution
Since retrieving the database connection string from your application’s Web.config file is a common routine for all applications, it is defined in the Base Classes module. Open the BaseClasses2005.vbproj in Visual Studio, and do a global search for “GetConnectionString” in the project. The following five occurrences are found.

The first two occurrences define the method GetConnectionString(), so they are the candidate targets of the work-around. The second GetConnectionString() method places a call to the first GetConnectionString() method. Therefore, there is only one candidate left. The solution seems simple and straightforward.

  1. Store logged in user’s username and password in session variables.

  2. Modify the ApplicationSettings.GetConnectionString() method to replace the username and password retrieved from Web.config with the ones stored in session variables.
Implementation
Step 1: Modify and compile the BaseClasses module
  • Open BaseClasses200x.vbproj in Visual Studio.

  • Open Configuration\ApplicationSettings.vb.

  • Locate the method GetConnectionString().

  • Insert the code block commented as "Get credentials from session if exists".

  • Build the project (in Release configuration).

  • (For existing applications only) Copy the BaseClasses.dll from "Iron Speed Installation root\Designer vx.x.x\BaseClasses\Bin\vs200x" to your application’s Bin folder.

Visual Basic .NET:

Public Function GetConnectionString(ByVal connectionName As String) As String
    'Get the connection string from Web.config
    Dim configString As String = ConfigurationSettings.AppSettings(connectionName)
    Dim connectionString As String
    If Not IsNothing(configString) Then
        'Reformat if necessary by parsing into a DatabaseConnection object
        'and then back into a string
        Dim dbConnection As DatabaseConnection = _
          DatabaseConnection.Parse(connectionName, configString)

        'Get credentials from session if exists
        If Not IsNothing(HttpContext.Current.Session("dbusername_key")) Then
          dbConnection.UserName = _
            CType(HttpContext.Current.Session("dbusername_key"), String)
        End If
        If Not IsNothing(HttpContext.Current.Session("dbpassword_key")) Then
          dbConnection.Password = _
            CType(HttpContext.Current.Session("dbpassword_key"), String)
        End If

        connectionString = dbConnection.ToConnectionString()
    Else
        Dim msg As String = RU.GetErrMsg_MissingConnectionString(connectionName)
        connectionString = msg
    End If
    Return connectionString
End Function

Step 2: Store login user’s credential in session variables
In ..\ApplicationRoot\App_Code\Shared\SignIn_Control.Controls.cs, insert the following code.

C#:

public override void Login(bool bRedirectOnSuccess) {
    HttpContext.Current.Session["dbusername_key"] = UserName.Text;
    HttpContext.Current.Session["dbpassword_key"] = Password.Text;
    base.Login(bRedirectOnSuccess);
}

Visual Basic .NET:

Public Overloads Overrides Sub Login(ByVal bRedirectOnSuccess As Boolean)
    HttpContext.Current.Session("dbusername_key") = UserName.Text
    HttpContext.Current.Session("dbpassword_key") = Password.Text
    MyBase.Login(bRedirectOnSuccess)
End Sub

Rebuild your application. Now your application will connect to the database as the logged in user.

About the Author
Jing Ding has a PhD in Computer Engineering, Bioinformatics and Computational Biology, and an M.S. in Toxicology from Iowa State University. He received his B.S. in biophysics from Fundan University in Shanghai, China. He is a self-taught programmer who "played" with assembly, C and C++ in the 1990s. He took a break from programming from 1997 to 2000. When he picked it up again in 2001, he worked with Java. Jing began working with C# and .NET in 2006.


  Privacy Statement