How do I use Web.Config transform on my connection strings? - c#

In my current project, I have some connection strings that are valid for local development machines:
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=SSPI"
</connectionStrings>
....
</configuration>
How would I use the Web.Config transforms to convert from this expression to one valid for our production server? The production server one would look something like:
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword"
</connectionStrings>
....
</configuration>
The syntax isn't obvious to me, and I'm completely failing at grokking the page on it.

This works for me but I too have found it to be a bit flakey at times.
You will need to create another file called Web.Config.Release and fill it with the following:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="local" connectionString="Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
<appSettings>
<add key="default_db_connection" value="local" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>

You shouldn't need to create a new file, it should be in the Solution Explorer, expand Web.config, and open Web.Release.config.
Scott Allan has a good video on it here (under Configuration and Deployment > Config Transformations).

Related

ASP.NET Web.Debug and Web.Release file transformations

First of all I know there are several pages about this issue e.g. Web.Config Debug/Release, Web.config Transformation Syntax now generalized for any XML configuration file and Web.config File Transformations. But most of them are outdated and does not mentioned clearly about all of the three files: Web.config, Web.Debug.config, Web.Release.config.
So, assume that I have the following settings for Web.config:
Web.config:
<appSettings>
<add key="ClientId" value="xxxxx"/>
<add key="ClientSecret" value="xxxxx"/>
</appSettings>
And I want to use these settings in debug and release in the following ways:
Web.Debug.config:
<appSettings>
<add key="ClientId" value="ddddd"/>
<add key="ClientSecret" value="ddddd"/>
</appSettings>
Web.Release.config:
<appSettings>
<add key="ClientId" value="rrrrr"/>
<add key="ClientSecret" value="rrrrr"/>
</appSettings>
1) What is the procedures to perform this accurately? I think while debugging and publishing, these settings are used automatically according to my selection Debug or Release in Visual Studio run and publish dialog. Is that true?
2) Should I remove these settings from Web.config after moving to Web.Debug.config and Web.Release.config?
3) What is the Test selection in the Configuration field of the Publish dialog in VS?
Any help would be appreciated.
I would recommend reading an overview of how web.config transforms work:
https://blog.elmah.io/web-config-transformations-the-definitive-syntax-guide/
In general, the Web.*.config files will make changes to the Web.config file depending on the selected publish configuration in Visual Studio. For example, if you want to update/replace a value in a debug publish, your Web.Debug.config file should look like:
<configuration xmlns:xdt="...">
<appSettings>
<add key"ClientId" value="ddddd" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
<add key"ClientSecret" value="ddddd" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
Here is the current Microsoft documentation on how these work:
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/transform-webconfig?view=aspnetcore-3.1

Reading value from Web.Config that uses configSource files

In keeping with the recommendation here we separated our connection string into an xml file. Our web.config:
//old - works
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=.\SQLExpress;Initial Catalog=xxx;Integrated Security=True;"
providerName="System.Data.SqlClient" />
<connectionStrings>
//new - fails
<connectionStrings configSource="connections.config" />
content of connections.config:
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLExpress;Initial Catalog=xxx;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Both files are located in the project root folder. EntityFramework works fine with both solutions. However when I try to obtain the connection string for some tests it fails to retrieve.This is the code that we use to extract the connection string:
string ConnectionString =
ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
Reading the connection string direct from web.config works, when we introduce the ConfigSource it fails. What do we need to make it work
You need to set the Copy to Output Directory property of your configuration file to either Copy always or Copy if newer.
If you don't do this, your ConnectionStrings.config file will not be copied to the output debug/release folder and so will not be found.
Also note that the external file should have the same name as of the section name. Like it should be connectionStrings.config, not connections.config

Setup of the project was unable to open database on another machine. my project is in c# windows application with sqlite database connection

I have inserted app.config file in my project.
Please help me out to overcome from this problem.
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="RConnString" connectionString="Data Source=C:\Users\user\Documents\Visual Studio 2010\Projects\Pathology lab\Pathology lab\bin\Debug\Pathology.sqlite"
providerName="System.Data.Sqlite" />
<add name="Pathology_lab.Properties.Settings.PathologyConnectionString"
connectionString="data source="C:\Users\user\Documents\Visual Studio 2010\Projects\Pathology lab\Pathology lab\bin\Debug\Pathology.sqlite;"
providerName="System.Data.SQLite" />
<add name="screen_shot.Properties.Settings.PathorecordConnectionString"
connectionString="data source="C:\Users\user\Desktop\screen shot\screen shot\bin\Debug\Pathorecord.db""
providerName="System.Data.SQLite" />
</connectionStrings>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
try to use this connection string
try to write connection string in |DataDirectory|/Databasename.db
And call App.domain () method

Issues when installing AspxCommerce onto GoDaddy Servers

I have been up all night trying to figure it out and when I try and install AspxCommerce onto my GoDaddy server. I have ran across multiple issues when trying to install this, all of the answers have been found after long research although for This last issue I am getting Configuration Error saying that I do not have any connection to 'SageFrameConnectionString'. Does anybody have any thoughts on how I should go about doing this including the necessary steps to get there?
PS: Everything works great when installed locally..
web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<roleManager enabled="true" defaultProvider="SageFrameSqlRoleProvider">
<providers>
<clear />
<add connectionStringName="SageFrameConnectionString" applicationName="SageFrame" name="SageFrameSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add applicationName="SageFrame" name="SageFrameWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</roleManager>
</system.web>
</configuration>
connectionstring.config:
<?xml version="1.0"?>
<connectionStrings>
<clear />
<add name="SageFrameConnectionString" connectionString="Data Source=;Initial Catalog=;Integrated Security=False;Persist Security Info=False;User ID=;Password=;Connect Timeout=120" providerName="System.Data.SqlClient" />
</connectionStrings>
Actually it is not a AspxCommerce problem. It's a GoDaddy server problem.
GoDaddy doesnt allow roles unless you use LocalSqlServer as your membership provider connection string.For GoDaddy you have couple of change in AspxCommerce .
In connectionstring.config file :
<connectionStrings>
<clear />
<add name="LocalSqlServer" connectionString="Data Source=.;Initial Catalog=YourCatlog;Integrated Security=False;Persist Security Info=False;User ID="ID";Password="PSWD";Connect Timeout=120" providerName="System.Data.SqlClient" />
</connectionStrings>
And In library go to SageFrame.Common\Setting\SystemSetting.cs on Declaration section :
replace
public static string SageFrameConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SageFrameConnectionString"].ToString();
with
public static string SageFrameConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString();
Note : Don't forget to rebuild library.

Connection String Problem

I'm new to Visual Studio and MySQL. I'm creating a Login Page and connect it with MySQL. But whenever I add a connection string. I always got an error.
Can anybody tell me what's the problem in here?
Please.
This is my config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=timekeeping;uid=root;pwd=admin;Option=3;"/>
</appSettings>
<connectionStrings>
<add name="ConnectionString" connectionString="Database=timekeeping;uid=root;pwd=admin;Option=3;" />
</connectionStrings>
</configuration>
I'm not entirely familiar with MySQL connections, but your config file should look more like this
<configuration>
<appSettings>
<add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;"/>
</appSettings>
<connectionStrings>
<add name="ConnectionString" connectionString="Database=timekeeping;uid=root;pwd=admin;Option=3;" />
</connectionStrings>
</configuration>
You probably have a syntax error in your App.config file.
Check the InnerException for details.
This usually means there is a problem with your web.config file. Probably a tag out of place, not closed, or otherwise jacked.
http://msmvps.com/blogs/kevinmcneish/archive/2010/01/06/fixing-quot-configuration-system-failed-to-initalize-quot-exception.aspx
<configuration>
<appSettings>
<add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=timekeeping;uid=root;pwd=admin;Option=3;"/>
</appSettings>
</configuration>
But whenever I add a connection
string. I always got an error.
Your web.config or app.config configuration is not legal in its current form - this could be because of a typo, i.e an unclosed tag, otherwise malformed XML or a section that is unknown or nested in a section where it doesn't belong.

Categories

Resources