Solved

How to pass connection string from web.config to data access project?

Posted on 2010-09-02
10
872 Views
Last Modified: 2012-05-10
Hi,

I created a data access project, which access database and holds a number of strong typed dataset. When I created dataset in this project, it created a settings.setting file, which reads connection string from app.config file of this project.

However, I created another web project. this project is supposed to consume the data retrieved from data access project. this web project is having connection string in web.config.

my question is how to pass the connection string in web.config from web project to data access project. there is no reason to save the same connection string in different places. keeping connection string in web.config is no doubt easier to modify in deployment.

thanks
0
Comment
Question by:viola123
  • 5
  • 3
  • 2
10 Comments
 
LVL 6

Assisted Solution

by:nipunu
nipunu earned 200 total points
ID: 33593856
You can directly access the web.config connection string like following

Webconfig

<connectionStrings>
            <clear/>
            
            <add name="TestConnection1" connectionString="User ID=abc;Password=1234;Initial Catalog=databasename;Data Source=machinename"/>            
      </connectionStrings>

Accessing it from anywhere in the project

SqlConnection dataSourceConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnection1"].ConnectionString);
0
 
LVL 6

Author Comment

by:viola123
ID: 33593912
hi, nipunu:

thanks, i am aware I can access the connection string of web.config within this web project.

My question is: How to make my data access project use the connection string in web.config of web project.

Cheers
0
 
LVL 27

Accepted Solution

by:
nmarun earned 300 total points
ID: 33593994
To test the theory that your data access project does actually accesses the web.config located in the web application, I did this.

Create a  ASP.NET web application
Create a Class Library (to mock the data access project)
Added the reference System.Configuration to the Class Library project
Added the Class Library project as a reference to the web application.

Added a static method to the Class1 of the Class Lib:

public static string GetCacheString()
{
    return ConfigurationManager.AppSettings["OutputCachePath"];
}

I call this in my default.aspx.cs page's page_load method:

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string OutputCachePath = Class1.GetCacheString();
    }
}

I do see that the value actually came from the web.config file (as there's no app.config file in the class lib project).

Arun
0
 
LVL 27

Expert Comment

by:nmarun
ID: 33593999
In fact, even if the app.config exists in the class library, ConfigurationManager.AppSettings actually accesses the web.config file only.

Arun
0
 
LVL 6

Author Comment

by:viola123
ID: 33594094
hi, nmarun:

thanks, i think we are getting close. Yes, you demonstrated we can access web.config connection string from data access project.

However, my data access project is holding all strong typed datasets, these datasets are based on the connection in settings.setting, which comes from the app.config of data access project.

How should we use that web.config connection string to update the connection in settings.setting file, rather than read it from app.config?

Thanks
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 6

Expert Comment

by:nipunu
ID: 33594204
0
 
LVL 6

Expert Comment

by:nipunu
ID: 33594207
Whenever you create a new dataset , don't let visual studio make new connction string but choose the one u want and have created in web.config file. check the first link from the above set
0
 
LVL 6

Author Comment

by:viola123
ID: 33602564
Hi, nipunu:

I read all the links in your last comment but did not work out a way for my situation.

what I need is: get the connection string from web.config and use it in data access project for my datasets.

in those links, the first link actually tries to duplicate the web.config connection string in data access project, rather than use web.config connection string in DAL. the second link did not end up with a final answer. other links are not quite related to my problem.

I want to keep using my current strong typed datasets in DAL and need the web.config connection string to tell those datasets where to read data. yes, I understand we can get the web.config connection string in DAL project, but since those datasets are auto-generated, i have nowhere to add any code to specify which connection string i want to use.

thx
0
 
LVL 6

Author Comment

by:viola123
ID: 33678711
Hi,

sorry about the late response. yes, I finally manage to resolve this issue by changing the .desinger.cs file of each strong typle dataset. in that file, I can assign connection string from a self-implemented method.

Thanks for all of you and I will split the point for all involved experts.
0
 
LVL 6

Author Closing Comment

by:viola123
ID: 33678719
i finally figured out the solution in my way but comments above did help
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Split in Javascript 5 31
Adware on IIS hosted asp.net website 1 20
Variable Event ? 3 23
ASP.net show message while code is running 3 11
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now