Please enable JavaScript to view this site.

Navigation: Advanced topics > Events > Global events

Event: BeforeConnect

Scroll Prev Next More

Description

This event occurs before the database connection is established. You can modify the database connection parameters in the BeforeConnect event.

 

The event already contains the current connection code in the description. For example:

 

GlobalVars.ConnectionStrings["Cars_at_localhost"] = "Server=localhost;Database=cars;User Id=root;Password=root";

 

Copy-paste this string to the event code and change the Server, Database, User ID, and Password parameters to change the database the app connects to.

 

This event can be useful, for example, when you build a SaaS app where each client has a separate database. You can use BeforeConnect to switch between the databases for different users.

Syntax

public XVar BeforeConnect()

Applies to pages

All pages.

Example

Let's say we have an app where the current connection code looks like this:

 

GlobalVars.ConnectionStrings["livedemo_at_localhost"] = "Server=localhost;Database=livedemo;User Id=root;Password=";

 

And you want to switch the database from the livedemo to cars.

 

The resulting BeforeConnect code looks like this:

 

GlobalVars.ConnectionStrings["livedemo_at_localhost"] = "Server=localhost;Database=cars;User Id=root;Password=";

See also:

Connecting to the database

Database API: SetConnection()