473,480 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Datalayer to use the right connection string base on URL of page request

pod
298 Contributor
Data layer to use the right connection string based on URL of page request

On my computer I am developing a 3 tier Web application which I publish as two different versions, one for production and one for development purposes: IMP-PROD for production and IMP-DEV for developement. Both connect to their own database: respectively IMPDB-PROD and IMPDB-DEV.

I have one Server with IIS and MS SQL for both Web applications. Each web app have the same hosting url but with a different sub folder name (similar to virtual folders).

I would like the Data Layer to auto detect its URL as to connect to the right database.

The web.config files contains both connection strings,
and I have a Public Class in the data layer that reads those strings

From the datalayer I can detect the CultureInfo but I cannot get session variables which I could use to detects the URL.

Can someone offer me suggestions?


Thanks

P:oD


Expand|Select|Wrap|Line Numbers
  1. ----- presentation layer -------
  2. ------- web.config ------
  3. <add name="SqlConnIMP-PROD" 
  4.          connectionString="data source=(local)\sqlexpress;Database=IMP;Trusted_Connection=True"
  5.          providerName="System.Data.SqlClient"/>
  6. <add name="SqlConnIMP-DEV"
  7.          connectionString="data source=(local)\sqlexpress;Database=IMP-DEV;Trusted_Connection=True"
  8.          providerName="System.Data.SqlClient"/>
  9.  
  10. -------- data layer ----------
  11. Public Class DatabaseHelper
  12.  
  13.     Friend Shared Function GetSqlConnIMP() As SqlConnection
  14.         Dim RightConnectionstring As String = "SqlConnIMP-PROD"
  15.         'Good Code needed here
  16.         If DevEnvironment Then
  17.             RightConnectionstring = "SqlConnIMP-DEV"
  18.         End If
  19.  
  20.         Return New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings(RightConnectionstring).ConnectionString)
  21.     End Function
  22.  
Sep 19 '14 #1
3 1323
Frinavale
9,735 Recognized Expert Moderator Expert
Use #IF DEBUG to determine if your build is for debugging purposes. If it is, use your development connection string, otherwise use the production connection string.

-Frinny
Sep 22 '14 #2
pod
298 Contributor
:) That is exactly what I started to use after posting my question, and it's working very well except for one section: the SqlProfileProvider in web.config requires to know which connections string to use.
But, I just found out that the web.config can have some sub scripts: web.release.config and web.debug.config... I am reading up on it right now
Sep 22 '14 #3
pod
298 Contributor
Found one post that explains it in simple terms Scott's Blog (some posts are so confusing)

Although the Match and Replace do not work exactly as they say, they at least works at the parent level:
I wanted to replace only one connection string but it replaces all strings but without touching the other configurations... good enough for me

Thank you all, I hope this helps someone else out there


P:oD
Sep 22 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
3214
by: AMC | last post by:
Hi, I'm using an include file to store the connection string to a database. Whenever I try to reference that string to open a connection in the page that includes the file I get the error 'empy...
3
2126
by: Varun | last post by:
I have a solution which contains windows,web,mobile presentations. Now i want to share the connection string in the web.config file. i want to write a method in a common layer where i can retrieve...
0
1268
by: Varun | last post by:
Hi Nick Thank you for ur reply I didnt get the clear idea how i can have connection string under datalayer, for both windows and web forms. Please can you give me a clear idea. And yes i am using...
2
6474
by: rmartin | last post by:
I need to conect my software from a remote client to a server, my standard connection: Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=;Initial Catalog=MyDb;Data...
18
2605
by: WStoreyII | last post by:
I am making a asp page that will run on my computer by iis. But the problem is the database in on a web server. I can not seem to get the connection to work for it though here his the connection...
2
4200
by: hfk0 | last post by:
Hi, I have a simple asp.net application running ok on my WinXP development server with SQL Server Express 2005 installed locally. After moving to the live server (Win 2000 server with MSDE 2000...
6
3427
by: Arsalan Ahmad | last post by:
Hi all, I am creating a website in which in an Item detail page there are a number of web controls (7 or 8) and what is happening that inside each of control's Page_Load() function I am creating...
0
1546
by: Hongbo | last post by:
Hi, I have a test server with Windows 2003 Standard Server. SQL Server 2000 Standard Edition was installed earlier as default instance. My ASP.Net web site works fine with the connection...
1
1720
by: FelmenM | last post by:
Hi, I would like to know what's the right connection string on vb 2005. I would like to connect to a database from a db2 server(version 9). I can connect to a db2 server using a db2 client...
10
2292
by: jimjim | last post by:
HI, We have shifted our database from sqlserver2003 to sqlserver2005. 1. connection strings stored in web.config <!-- Machine name to connection string keys --> <add key="*777*"...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.