Connection string updated on server when publishing - c#

i'm trying to get my connection string values updated on production server during publish. I'm trying the following with no results:
<connectionStrings>
add name="DataConnect" connectionString="Server=Pepe;Database=Oyeti;Integrated Security=SSPI;" xdt:Transform="Replace" />
and also added the xdt:Locator, but i don't think it's the correct way:
<connectionStrings>
add name="DataConnect" connectionString="Server=Pepe;Database=Oyeti;Integrated Security=SSPI;" xdt:Transform="Replace" xdt:Locator="Match(name)" />
What i'm not seeing there?
thanks!

You can add the xdt:Transform="SetAttributes" xdt:Locator="Match(name) attributes to the connectionString tag in your transformation file (Web.Release.config) to transform the values in your primary config file (Web.config). As an alternative, you can also specify connection strings in your publish profile.
Although the default transform file contains an example that shows how
to update a connection string, in most cases you do not need to set up
connection string transformations, because you can specify connection
strings in the publish profile. You'll do that in the deploy to IIS
and deploy to production tutorials.
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/web-config-transformations

Related

Common database name Entity Framework

I use Entity Framework 6 code-first in a Windows application to connect to a SQL Server database. How could I edit my code to enable me or any app user to change the database name without getting any errors?
You can set the connection string separately in the configuration file and then use ConfigurationManager class to read strings.
But to change only the database name without other errors, you must ensure that other settings are the same. Otherwise, you must modify the corresponding code or settings according to the actual situation.
For example: add in the configuration file (config)
<connectionStrings>
<add name="MyDbContext" connectionString="Data Source=myServer;Initial Catalog=myDatabase;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Then call it like this:
string connectionString = ConfigurationManager.ConnectionStrings["MyDbContext"].ConnectionString;
For detailed examples, please refer to documentation.

Where to find the location of IIS manager connection strings

I was getting an error saying that one of my connection string in my application's web.config file was already defined.
I checked in the IIS settings and when I checked the connection string property it was there already with Entry Type : Inherited.
So I went up the chain and went all the way up to the root of the localhost and checked the connection strings there.
I found a bunch of connection strings there as well.. but they're also all with Entry Type Inherited..
I checked the web.config file inside the wwwroot folder but didn't find any connection strings defined in there..
Where could these connection strings be coming from?...
Configuration files in .NET are inherited in the following order:
systemroot\Microsoft .NET\Framework\versionNumber\CONFIG\Machine.config
systemroot\Microsoft .NET\Framework\versionNumber\CONFIG\Web.config (ASP.NET only)
(application directory)\Web.config
So the connection strings that show up as "inherited" are specified in either of the upper two files.
Reference: MSDN: ASP.NET Configuration File Hierarchy and Inheritance
If you don't want to alter the machine-wide configuration, you can <clear /> them from being inherited in your application's configuration as explained in What does <clear /> signify when specifying a connectionstring?:
<connectionStrings>
<clear />
<add name="LocalSqlServer" connectionString="..." />
</connectionStrings>

C# Web API connectionstrings web.config not being read

This is my first question here, so please be gentle (and provide constructive criticism as to how I can improve my question) :)
I'm trying to connect to an Oracle Database in my Web API project.
This project has an app.config and a web.config.
I don't know why I have both, but it came with that when setting up the (empty asp.net with Web API) project.
This is my code to connect to the database and get the connection string, which is also where I get the NullReferenceException:
// create a connection to the database
using (var con = new OracleConnection(ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString))
And I have included the appropriate using-statements:
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
using System.Data;
using Oracle.ManagedDataAccess.Types;
My web.config file is as follows:
<configuration>
<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<!-- Customize these connection alias settings to connect to Oracle DB -->
<dataSource alias="ALIAS" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=IPOFSERVER)(PORT=PORTOFSERVER))(CONNECT_DATA=(SERVICE_NAME=SERVICENAME)));User Id=USERNAME;Password=PASSWORD;" />
</dataSources>
</version>
</oracle.manageddataaccess.client>
<connectionStrings>
<clear/>
<add name="Oracle" connectionString="Data Source=ALIAS;" providerName="Oracle.ManagedDataAccess.Client"/>
</connectionStrings>
</configuration>
Things I've tried so far:
Using System.Web.Configuration.WebConfigurationManager instead
Checking references etc.
Adding AppSettings key and value and try to read that from the web.config file. Also returns null.
I'm not getting any errors other than the runtime NullReferenceException.
I hope this is enough information to suggest a solution. If not, please let me know and I'll provide extra info.
Try the following. It is within System.Configuration:
var con = ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString;
Also, you may want to try without the clear/> in your config.
I've only used an app.config file when I have a console application project in my solution for testing. I store my connection strings in the web.config like so:
<connectionStrings>
<add name="NAME" connectionString="Server=Server;Initial Catalog=TABLE_NAME;User ID=ID;Password=PASSWORD;Application Name='WEBSITE'" />
</connectionStrings>
Then I reference it in my code like so:
protected const string ConnectionString = #"NAME";
Turns out it was a very simple fix, as is usually the case.
As mentioned I have an app.config AND a web.config.
In the web.config are the Oracle Client settings, so because of that and prior experience I figured the connection string should be there too.
When defining it in the app.config however, it works!
Confusing with 2 config files though, as one would expect a connection string to be defined in a web.config, not app.config.
Bottom line: ConnectionStrings should be in the app.config.
That fixed it for me.

Duplicate Connection String Error

I have a website deployed in a server. One day it threw an error saying the connection string name is already added. I checked the web.config file and it has only one entry in that name. I removed the entry from the config. Now the website worked well and fetched data from database.
Note: When I changed the name of the config file it show error.
I think, the issue is – the connectionstring part is cached in memory. Is it so? How can we overcome this unwanted behavior?
Config Files in Source Code
Release Config
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
Debug Config
<system.web>
</system.web>
REFERENCES:
Issue with unwanted connection string appearing in my published web config
.NET 2.0 App.Config connection strings includes unwanted SQLExpress default
That is not the issue - when you change the web.config file, the IIS process gets reset, so there can't be any caching involved.
What is more probable is that there is either a parent or child directory with a web.config file that contains the same connection string name - this is causing the error.
You can solve that in several ways:
Ensure there is only one web.config in the correct scope with the connection string name
Use the remove element:
<connectionStrings>
<remove name="theConnectionString" />
<add name="theConnectionString" ... />
<connectionStrings>
Refer Encrypted config file does not apply “remove” tag in connectionStrings for a related question

Reading Hibernate Properties from Web.config

The C# project I'm working on uses nHibernate and the connection string is in the web.config as a property of a Hibernate element. I need to read the connection string in the installer to get a connection manually without using Hibernate. I know I can use configManager.connectionStrings, but as the connection string is already defined in the Hibernate portion of web.config I don't want to copy it again into the connectionStrings element. So how can I access this?
You could put the connection string in the <connectionStrings /> section of the web.config and then have NHibernate get it from there. In the NHibernate settings, remove the <connection.connection_string> property and replace it with <connection.connection_string_name> supplying the name from the <connectionStrings> section. See here for details.
<hibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.connection.connection_string" value="${local}"/>
</hibernate>
<connectionStrings>
<add name="local" connectionString="server=(local);database=db;Uid=username;Pwd=password;"/>
</connectionStrings>
This makes it available in your ConfigurationManager, but only referenced once.

Categories

Resources