Can I specify a connection string in the web.debug.config only? - asp.net

I have a group of apps that inherit their connection string from a web.config in the root directory of IIS. This means that I don't need to specify a connection string in the root directory of the app, but I do need to specify a connection string when debugging locally.
My question is, how can I set a connection string in web.debug.config that only is used when I'm debugging?

You could specify the connectionstring in the normal web.config and then in the web.release.config add a transform that simply removes the connection string all together.
That way it should exist in the debug one but not in the web.release.config.
Of course this assumes you are using those configs with transforms and not doing a simple copy/paste of the code when you deploy
MSDN has a good example of this
The following example shows how to select all the connection string
add elements in the development Web.config file. In the deployed
Web.config file, only the first connection string element is removed.
<configuration xmlns:xdt="...">
<connectionStrings>
<add xdt:Transform="Remove" />
</connectionStrings>
</configuration>
EDIT: I guess alternatively you could also create a transform in web.debug.config that adds it while in debugging, which might help to keep it out of the original web.config if you aren't appying transforms when deploying

Keep your Debug connection string in Web.Config and replace the release connection string in Web.Release.Config using the locator as show below
<connectionStrings>
<add name="DefaultConnection" connectionString="Release Connections tring" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="XPath(configuration/connectionStrings)"/>
</connectionStrings>

Related

What does <clear /> signify when specifying a connectionstring?

This answer to another question states:
Do not forget to clear the connectionStrings first:
<connectionStrings>
<clear />
<add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
... interesting. What does that do?
In .Net config files are inherited, so your applications config will inherit settings from your machines config.
The <clear/> tag will remove any inherited connection strings and thereby avoids confusion and potential problems.
In ASP.Net you may have several inherited connection strings, so this is very common there.
The element removes all sections and section groups from your application that were defined earlier in the current configuration file or at a higher level in the configuration file hierarchy.
http://msdn.microsoft.com/en-us/library/aa903345(v=vs.71).aspx
so for example, if this was a child config file and the parent config file had some settings... you may not want them being inherited so you specify the clear flag to clear it and then use your settings.

Difference between connection strings in App Settings and connectionStrings configuration setting

I was wondering what is the difference in having connection strings in 2 app settings and connectionstrings? Just wanted to know technically in Visual Studio 2008 for windows authentication!
<appSettings>
add key="ConnectionString" value="Server=198.57.2.70;Database=SalesTracking;Integrated Security=SSPI" />
</appSettings>
<connectionStrings>
<add name="ConnectionString" connectionString="DataSource=198.57.2.70;InitialCatalog=SalesTracking;IntegratedSecurity=True;" />
</connectionStrings>
Thanks!!
The difference is that the connectionString section gives you strongly typed access to your connection strings through the ConfigurationManager class. It's meant for connection strings specifically.
AppSettings is meant to store general settings. You can use it to store connection strings, but I recommend not doing that since there is a specific element for it in connectionStrings

Not able to uploade website on server

I am try to uploading website.I don't know how configure database(web.config file) on uploading time.I am using Somee.com , anyone familiar with somee.com Or know how to configure database the please help me.
webconfig file code
<configuration>
<connectionStrings>
<add name="abc" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Jai-Ganesh\Documents\Visual Studio 2010\WebSites\testinmg\App_Data\Database.mdf;Integrated Security=True;User Instance=True"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<customErrors mode="RemoteOnly" ></customErrors>
</system.web>
</configuration>
Note: I don't know what is path of the dataSource in server.
Link of the page:
[http://www.imureditor.somee.com/add_value.aspx][1]
first you need to contact your hosting provider for connection string. They will give you the ip address (you can also the host name) which you can then configure it.
See the exmaple below;
<add name="connectStringName" connectionString="Data Source=somee.com;User ID=yourusernameforDB;password=yourpasswordforDB;Initial Catalog=yourDBName;" />
OR take a real example below;
<add name="KenSchoolConnectionString" connectionString="Password=1234;Data Source=192.168.1.1;Integrated Security=False;Initial Catalog=KenMISSchoolDB;User ID=sa" />
OR
Go to connectionstrings.com
Your connection string references your local SQL Server instance and tries to attach a local file; this connection string will need to be changed to reflect the setup on the server, so you'll need to specify the Data Source, Catalog, User ID and Password - Integrated Security won't work, and attaching *.mdf files from App_Data probably won't either.
Your service provider is responsible for giving you the credentials needed for this, we can't guess them.

Connection Strings

Could you help me understand what this piece of code does in simple English? This is a beginner. Thank you in advance.
<connectionStrings>
<add name="BalloonShopConnection" connectionString="Server=(local)\Sql➥
Express; Database=BalloonShop; User=balloonshop; Password=ecommerce" ➥
providerName="System.Data.SqlClient" />
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Server=(local)\SqlExpress;➥
Database=BalloonShop; User=balloonshop; Password=ecommerce" providerName=➥
"System.Data.SqlClient" />
</connectionStrings>
This is from a tutorial. I covered everything up to first half of the book but this seems strange.
The web.config for your app is only part of the configuration settings your app gets. The total configuration is a combination of your web.config as well as the machine.config file, and settings defined in IIS. This line:
<remove name="LocalSqlServer"/>
implies that the there is a connection string named LocalSqlServer defined elsewhere that you may be getting from somewhere other than your web.config. So in your web.config they are explicitly removing that other LocalSqlServer connection string you would otherwise get, and replacing it with the one defined below that line. That change only affects your application. This is explained here: http://weblogs.asp.net/jgalloway/archive/2012/01/17/10-things-asp-net-developers-should-know-about-web-config-inheritance-and-overrides.aspx
If the remove tag wasn't there, and that connection string was also defined higher up the chain, your app would use the one defined higher up, and ignore the one defined in your web.config (which can be quite confusing!). That's why the remove tag is needed.
See also:
Avoid web.config inheritance in child web application using inheritInChildApplications
You add connection string which names BalloonShopConnection. Your sql server names (local)\SqlExpress; Your databese is BalloonShop user is balloonshop so you add second one which is very similar :)

Different connection string for each publish profile in VS2010

Is it possible to change connection string (or just server host) relying on selected web publish profile? Maybe using Web.config transform or someway else?
I mean for profile "Test" change connection string "MyConnString" (in published Web.config) to "Data Source='example.com,14333;..." and for profile "Production" - to "Data Source=./SQLExpress;..."
This is exactly what web config transforms were created for. The link you provided in your post has a walkthrough of doing this specifically for connection strings.
To start with the transforms, right-click your web.config file in the project explorer and choose "Add Config Transforms". Assuming that you have ConfigA and ConfigB in your solution configuration, there will be two new files added, Web.ConfigA.config and Web.ConfigB.config.
If you open these new files, they'll be pretty empty except for a bunch of comments. They actually contain a connection string example in them that you can use though - it looks like this:
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
Uncomment this section, and change the "name" property to the name of the connection string in the base web.config file. Set the "connectionString" property to the actual value that you want to use for ConfigA. So, like this:
<connectionStrings>
<add name="myConnectionString"
connectionString="Data Source=ConfigASqlServer;Initial Catalog=ConfigADatabase;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
Repeat the process for the Web.ConfigB.config file, with the desired connection string for ConfigB.
Now when you use the Publish command in visual studio, it will automatically transform the base web.config file, and set the "connectionString" attribute to whatever configuration you're in when you publish.

Resources