Page 1 of 1

Dynamic Database

Posted: Thu Jun 22, 2017 8:36 pm
by aspmaker_fan

"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
Zam


Re: Dynamic Database

Posted: Fri Jun 23, 2017 11:03 am
by motfs

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.


Re: Dynamic Database

Posted: Wed Oct 04, 2017 5:58 am
by aspmaker_fan

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]
,[UserName]
,[Password]
,[DBName]
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
+
Init(dbid);
AspNetMaker2017.Models.Project+cConnection..ctor(string dbid) in ewevent.cs
+
public cConnection(string dbid) : base(dbid)


Re: Dynamic Database

Posted: Thu Oct 05, 2017 9:19 am
by Webmaster

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...
}