8

I am new in the web development world and I would like to create a variable in the web.config file so that I can use it in the .NET portion of the web.api

I found the following tutorials on how to do that :

Setting up connection string in ASP.NET to SQL SERVER

And

http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config

I have the following question , I don t have a database to connect the string to(I will only use it in the web config so that I can easily change the string without having to go through code . so assuming that I am using it in the following way :

<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />

What should I have in the connectionString and the providerName ?

Community
  • 1
  • 1
user1415780
  • 1,153
  • 5
  • 16
  • 33

3 Answers3

27

If I understand what you want to do, it sounds like you don't want to use a connection string at all. Instead, use the Application Settings sections of your web.config file. For example

<configuration>
  <system.web> ... </system.web>
  <appSettings>
    <add key="MyAppSetting" value="A test value." />
  </appSettings>
</configuration>

This can then be used in your code by getting the value of

System.Configuration.ConfigurationManager.AppSettings["MyAppSetting"]

(C#) or

System.Configuration.ConfigurationManager.AppSettings("MyAppSetting")

(VB)

See MSDN for more information, or just search online for "asp.net AppSettings".

Kara Potts
  • 1,062
  • 9
  • 15
1

If you don't have a database to connect to (which is what I understood from your question), then you don't even need to have the <connectionStrings> section in your Web.config. That section is only needed if you are going to connect to a database.

If you do use a database, then the connectionString varies depending on several factors such as type of authentication, database product (MS SQL Server, MySQL), type of driver (ODBC, .NET), etc.

The "Provider Name" will depend on the database product that you are using. For example for SQL Server is "System.Data.SqlClient"

You can look at this site for a comprehensive list of database products and connection strings appropriate for each product for different authentication types, drivers used, etc.

Icarus
  • 63,293
  • 14
  • 100
  • 115
0

For ASP.NET 4.5 Application I'm using appSettings for email configuration. I'm also using connectionStrings

appSettings needs to be included before connectionStrings not before configSections

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="ContactEmail0" value="service@davincispainting.com" />
    <add key="ContactEmail1" value="estimate@davincispainting.com" />
  </appSettings>
  <connectionStrings>
    <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-DAV3-20150302043828.mdf;Initial Catalog=aspnet-DAV3-20150302043828;Integrated Security=True" providerName="System.Data.SqlClient" />-->
    <!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=*****;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />-->
    <!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=Davincis3;User ID=*****;Password=*****;" providerName="System.Data.SqlClient" />-->
    <!--<add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLSERVEREXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />-->
    <add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLEXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />
  </connectionStrings>
...
Paul
  • 974
  • 2
  • 13
  • 37
  • `appSettings needs to be included before connectionStrings not before configSections` - maybe for you it does for some reason, but I have an app that works perfectly fine where appSettings is below connectionStrings. Pretty sure the order doesn't matter. – sǝɯɐſ Jul 08 '15 at 14:07