☰
Current Page
Main Menu
Home
Home
Editing switching-databases-via-code
Edit
Preview
H1
H2
H3
default
Set your preferred keybinding
default
vim
emacs
markdown
Set this page's format to
AsciiDoc
Creole
Markdown
MediaWiki
Org-mode
Plain Text
RDoc
Textile
Rendering unavailable for
BibTeX
Pod
reStructuredText
Help 1
Help 1
Help 1
Help 2
Help 3
Help 4
Help 5
Help 6
Help 7
Help 8
Autosaved text is available. Click the button to restore it.
Restore Text
#Switching databases via code ##Database Level Access You can set the database access level on a per-user basis using different connection strings by simply adding logic that changes the connection string based on logic you provide. For instance, take the code sample below: ```sql public static void InitializeReporting() { AdHocSettings.CurrentUserName = GetUserName(); AdHocSettings.CurrentUserTenantID = GetUserTenantID(); AdHocSettings.CurrentUserIsAdmin = IsAdmin(AdHocSettings.CurrentUserName); if(AdHocSettings.CurrentUserIsAdmin) { AdHocSettings.SqlServerConnectionString = "server=(local);database=Administration;Trusted_Connection=True"; } else { AdHocSettings.SqlServerConnectionString = "server=(local);database=Northwind;Trusted_Connection=True"; } } ``` This is a simple method that will connect the user to the database called "Administration" if that user is an admin. You can, however, also switch databases on the fly during runtime. ##Using the Query String To dynamically change your connection string via a query string parameter, you can add code like this to the Report Designer, Report List, and Report Viewer. If a "db" parameter is found, it will change the connection string. ```csharp protected override void OnInit(EventArgs e) { string db = Request.QueryString["db"]; if (db != null) Izenda.AdHoc.AdHocSettings.SqlServerConnectionString = "server=(local);Trusted_Connection=True;database=" + db; base.OnInit(e); } ``` ##Custom Button This sample page can be used to create a drop down & button to dynamically change the database connection string during runtime. Simply add this new page to the folder where your reporting website exists. When the database from the dropdown is selected and the button is clicked, the connection string associated to that database is selected. The application will now use the updated data source information. ```csharp <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { NameValueCollection Connections = new NameValueCollection(); Connections["Northwind"] = "server=(local);database=Northwind;Trusted_Connection=True"; Connections["Administration"] = "server=(local);database=Administration;Trusted_Connection=True"; foreach (string key in Connections.AllKeys) { ddlConnections.Items.Add(new ListItem(key, Connections[key])); } } protected void btnChange_Click(object sender, EventArgs e) { Izenda.AdHoc.AdHocSettings.SqlServerConnectionString = ddlConnections.SelectedValue; } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:dropdownlist ID="ddlConnections" runat="server"></asp:dropdownlist><br /><br /> <asp:Button ID="btnChange" runat="server" Text="Select DB" OnClick="btnChange_Click" /> </div> </form> </body> </html> ``` You can now choose either the Northwind or Adminstration database connections to pull data sources from.
Uploading file...
Header
 **This documentation is for the legacy Izenda 6 product. Documentation for the new Izenda 7 product can be found at [[https://www.izenda.com/docs/|https://www.izenda.com/docs/]]**
Sidebar
 --- * [[Izenda 7 Series Documentation|https://www.izenda.com/docs/]] * [[Release Notes]] * [[Upgrade Best Practices|FAQ/Izenda-Update-Best-Practices]] * [[FAQ]] * [[Tutorials]] * [[The Izenda API|/API/AdHocConfig]] * [[The AdHocSettings class|/API/AdHocSettings]] * [Developer Links and Guides](/Guides/Developer-Links-and-Guides) * [[Known Issues]] --- * <a href="http://www.izenda.com" rel="nofollow" target="_blank">Izenda Website</a> * [Product Video](https://www.youtube.com/watch?v=X3-yWFq0w5A) * <a href="http://www.izenda.com/blog" rel="nofollow" target="_blank">Izenda Blog</a> * <a href="http://www.izenda.com/company/" rel="nofollow" target="_blank">About Us</a> * <a href="http://www.izenda.com/contact-us/" rel="nofollow" target="_blank">Contact Us</a> --- * [Guide To Report Design](/Guides/ReportDesign) * [Making Custom Forms Video](http://www.youtube.com/watch?v=5b2axJlgdFs) --- * [[Acknowledgements]]
Edit message:
Cancel