Dynamic Database

This public forum is for user-to-user discussions of ASP.NET Maker. Note that this is not support forum.

Dynamic Database

Postby aspmaker_fan » Thu Jun 22, 2017 8:36 pm

"It depends on when you set your $_SESSION['section'] variable. Before the user logs in, you do not know who he/she is. So when the user logs in, you are still using the default database. After login, you can set your session variable (e.g. using User_Valdiated server event), then you can use Database_Connecting server event to check your session variable and change the connection info accordingly for subsequent pages."

The above clipping is from another post. How do we really implement this in asp.net? Is there any sample code for this? How do we connect another database after a user is validated.

Kind regards
Posts: 12
Joined: Sat Apr 05, 2014 9:46 pm

Re: Dynamic Database

Postby motfs » Fri Jun 23, 2017 11:03 am

It is exactly the same. To set Session variable, use this in the User_Validated server event:

// check the user, use rs["<UsernameField>"]
ew_Session["<variable>"] = <Value>; // Replace <variable> with actual variable name

Then change the "Connstr" in the Database_Connecting server event based on the Session value. Read the Server Events and Client Scripts -> Database_Connecting in help file for more detail.
Posts: 710
Joined: Tue Sep 08, 2015 4:55 pm

Re: Dynamic Database

Postby aspmaker_fan » Wed Oct 04, 2017 5:58 am

Tried with the settings below but getting 'Null Ref exception'.

// Database Connecting event
public override void Database_Connecting(ref string Connstr) {
string _dbname_ = ew_Session["_Dbname"].ToString();
// Check Info["id"] for database ID if more than one database
Connstr = "Persist Security Info=False;Data Source=localhost;Initial Catalog=" + _dbname_ + ";User Id=xxxxxx;Password=xxxxxxx";


public virtual void User_Validated(DbDataReader rs) {

ew_Session["_Dbname"] = rs["DBName"];

login using the sql table:

SELECT [CompanyID]
FROM [EMaster].[dbo].[Company]

NullReferenceException: Object reference not set to an instance of an object.
AspNetMaker2017.Models.Project+cConnection.Database_Connecting(ref string Connstr) in ewevent.cs
string _dbname_ = ew_Session["_Dbname"].ToString();
AspNetMaker2017.Models.Project+cConnectionBase.OpenConnection() in aspxfn.cs
Database_Connecting(ref connstr);
AspNetMaker2017.Models.Project+cConnectionBase.Init(string dbid) in aspxfn.cs
Conn = OpenConnection();
AspNetMaker2017.Models.Project+cConnectionBase..ctor(string dbid) in aspxfn.cs
AspNetMaker2017.Models.Project+cConnection..ctor(string dbid) in ewevent.cs
public cConnection(string dbid) : base(dbid)
Posts: 12
Joined: Sat Apr 05, 2014 9:46 pm

Re: Dynamic Database

Postby Webmaster » Thu Oct 05, 2017 9:19 am

Your code always use:

>string _dbname_ = ew_Session["_Dbname"].ToString();
>Connstr = "Persist Security Info=False;Data Source=localhost;Initial Catalog=" + _dbname_ + ";User Id=xxxxxx;Password=xxxxxxx";

Be reminded that ew_Session["_Dbname"] does not have value before the user is logged in, so in the server event you should check if the ew_Session["_Dbname"] has value first, e.g.

if (ew_Session["_Dbname"] != null) {
... your code...
Posts: 10734
Joined: Fri Aug 06, 2004 5:59 pm

Return to General Discussion (ASP.NET Maker)