Environment variables in ASP.NET web.config file - asp.net

I was wondering why can't I use a custom environment variable in an ASP.NET web.config file like so?
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="ConnectionName" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename='%MyProjectsFolder%\WebAppName\App_Data\Database1.mdf';User Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I checked through Start -> Run that the file path to .mdf file is valid.
When I run my C# code to connect to database I get the following error:
An attempt to attach an auto-named database for file
%MyProjectsFolder%\WebAppName\App_Data\Database1.mdf failed. A
database with the same name exists, or specified file cannot be
opened, or it is located on UNC share.

Environment variables may not be used in a config file.

You can use: DataDirectory like this:
“Data Source = |DataDirectory|\Mydb.sdf”
And you can change where DataDirectory points: "To set the DataDirectory property, call the AppDomain.SetData"
http://social.msdn.microsoft.com/Forums/en/sqlce/thread/dc31ea59-5718-49b6-9f1f-7039da425296
Caveat, I've never tried this.

I don't suppose you've tried:
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename='~/App_Data/Database1.mdf';User Instance=true"

Related

Web.config connectionString transforms not working when using an external config file

Ok, I am using an external config file for my connection strings so each individual developer can have their own strings while developing. Normally, each dev has a different environment but we all publish to the same servers via web.release.config transforms.
However, when I publish from VS, it's not transforming from the web.release.config for the conn strings. I think it's because if you have the configSource attribute set to use an external config it ignores the transform.
Here's my web.config:
<connectionStrings configSource="userConn.config" />
And here's my userConn.config:
<?xml version="1.0"?>
<connectionStrings>
<add name="DefaultConnection"
providerName="System.Data.SqlClient"
connectionString="Data Source=XXXX;Initial Catalog=XXXX;user id=XXXX;password=XXXX;" />
<add name="ExtVariablesEntities"
providerName="System.Data.EntityClient" connectionString="metadata=res://*/Models.XXXX.csdl|res://*/Models.XXXX.ssdl|res://*/Models.ExtVariables.msl;provider=System.Data.SqlClient;provider connection string="data source=XXXX;initial catalog=XXXX;user id=XXXX;password=XXXX;MultipleActiveResultSets=True;App=EntityFramework;"" />
</connectionStrings>
After publishing, opening the actual web.config that made it to the server, it still has:
<connectionStrings configSource="userConn.config" />
Is there a workaround for this? I've had this setup before I just don't remember what the trick is.
Just an FYI that I was able to solve this by following this article: https://jshowers.com/simple-web-config-transforms-for-configuration-elements-that-use-configsource-and-external-files/
You end up with 3 custom config files - whatever you want to call them for dev, test and prod lets say. Then you will have 3 web config files dev, test and prod (these are your web config transform files) where you simply say:
<appSettings xdt:Transform="Replace" configSource="path.to.custom.config.file.depending.on.env">

Have connection strings as well as common config source in asp.net or wcf websites

Hi I am looking for a solution where I have some common connection strings which I have
a common connections strings file but also need some specific add and remove connection strings in some sites...but do not see a way to do it...
<configuration>
<connectionStrings configSource="connections.config">
<remove name="AppConnectionString" />
<add name="AppConnectionString" connectionString="data source=(local);initial catalog=xyz;integrated security=True;MultipleActiveResultSets=True;" />
<connectionStrings/>
</configuration>
With file connections.config containing
<connectionStrings>
<add name="name" connectionString="conn_string" providerName="System.Data.SqlClient" />
<add name="name2" connectionString="conn_string2" providerName="System.Data.SqlClient" />
</connectionStrings>
Thanks,
Bala
If this does not work as you have expressed it above (which is a shame in my opinon), you have a number of other options:
Include the common connection strings in the connection string config, and then use the application settings config for the app-specific connection strings.
As an alternative, use a custom configuration file for the app-specific connection strings and load it in for each app.
When you build your apps inject the correct connection string keys into the config file using something like powershell. This script can run as a post build task in your build script.
Otherwise, when you package or stage your release, ask the release team to run the script as a manual step (or add it into your deployment script).

Can't figure out web.debug.config vs. web.config substituation in VS2010

Can someone point me to what am I doing wrong here?
I'm trying to set up an ASP.NET web app project to compile with two versions of web.config file for Release and Debug builds. So for simplicity sake, here's my web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="WhyMicrosoftSucksSoMuch" connectionString="" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
And then I do the following in web.debug.config:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="WhyMicrosoftSucksSoMuch"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename='C:\FilePath\Database1.mdf';User Instance=true"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(connectionString)" />
</connectionStrings>
</configuration>
If I publish it under Debug configuration the resulting web.config looks good, but when I try to run my project from VS2010 also under Debug configuration I get an error when my logic attempts to access database:
The ConnectionString property has not been initialized.
So what's the trick here?
PS. And please don't point me to this document. I tried reading it several times but I get a headache from so much superfluous information. I guess MS doesn't know what brief is.
When running it under debug mode it doesn't apply any transformations.
It only applies them during publishing. You can put your debug connection string in the main web.config, and add your production connection string to the web.release.config
also, you probably will want to use
xdt:Transform="Replace"

Web.config Transform on another configuration file

Can you do a web.config transforms on another file like connections.config, nlog.config, app.config in the same way? I can't seem to find an answer.
<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<add name="ApplicationServices" connectionString="Data Source=ConnectionString" xdt:Transform="SetAttributes(mode)"/>
</connectionStrings>
I have created a Visual Studio add in for just this. It's called SlowCheetah and you can find it at http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5.

ASP.NET Attempt to attach auto-named a database failed?

Parser Error Message: An attempt to
attach an auto-named database for file
D:\Projects\Damnation\Damnation.Website\Damnation.Website.Tracker\BugNET_WAP\App_Data\aspnetdb.mdf
failed. A database with the same name
exists, or specified file cannot be
opened, or it is located on UNC share.
Why could this be happening? My connection string doesn't use |DataDirectory|, it is:
<connectionStrings>
<add name="BugNET" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=dnmain;Integrated Security=True;"/>
</connectionStrings>
I get this error when I try to load default.aspx, after installing BugNET.
Apparently, and certainly for some obscure reason. There is a mistery connection string coming from... nowhere.
Problem solved adding a <clear/> tag.
<connectionStrings>
<clear/>
<add name="BugNET" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=dnmain;Integrated Security=True;"/>
</connectionStrings>
file
D:\Projects\Damnation\Damnation.Website\Damnation.Website.Tracker\BugNET_WAP\App_Data**aspnetdb.mdf**
failed.
This is the ASP.NET standard membership system here - not your "regular" database. There must be a second connection string somewhere that references this aspnetdb.mdf and tries to attach it from a file.
Search your solution for this file name - it must be somewhere!

Resources