user1696632
user1696632

Reputation: 23

Website is not taking connection string from web.config. it is searching for app.config

I have project with 3 tier structure DataAccessLayer, BusinessLogicLayer and Website.

In my DataAccessLayer I have used F# library project with connectionstring accessing from app.config file

F# code -

type dbSchema = SqlDataConnection<"","MyConnection">

let connectionString = System.Configuration.ConfigurationManager.ConnectionStrings.               ["MyConnection"].ConnectionString 

App.config code -

  <connectionStrings>
      <add name="MyConnection" connectionString="Data Source=MyServer;Initial   Catalog=MyDB;Persist Security Info=True;User ID=sa;Password=xyz;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

Now I have given the reference of this dll in website BusinessLogic and Website project.

I am calling a function of BusinessLogicLayer to get data -

var MyDataList = BusinessLogic.GetAllData().ToList();

Now the issue is, the website is searching connectionstring in app.config file instead of web.config. I want it to take connectionstring from web.config

Upvotes: 1

Views: 558

Answers (3)

spikej
spikej

Reputation: 129

This is an old question, but it still comes up before this one in a search: F# Type Provider for SQL in a class

In your F# Data Access, add in a function like this

let getDbContext connString = 
    match connString with
    | "" -> Sql.GetDataContext()
    | _ -> Sql.GetDataContext(connString)

Then in the C# code you can call getDbContext and pass in the connection string from web.config at runtime.

Note that the F# Data Access layer will still need to have the connection string provided there for the compiler to do it's job.

Upvotes: 0

somesh
somesh

Reputation: 3578

You can fetch the connection string by using below line of code -

using System;
using System.Configuration;

var connectionString=ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

Here is a reference : Connection String from App.config

Upvotes: 0

Batavia
Batavia

Reputation: 2497

you can have either a web.config for a web project or an app.config for an application.

as alternative you can load in custom configuration files see also http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx

Upvotes: 1

Related Questions