Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place connection string in web.config

My web.config looks like this :

<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="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
  </system.web>
  <runtime>

When I add my connection string just below <configuration> I get an error saying that only one <configSections> element is allowed. Where should I put my connection string?

like image 391
jason Avatar asked Nov 12 '14 08:11

jason


3 Answers

Just place it inside <configuration> right after </configSections> f.e.

<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>
    <connectionStrings>
        <add name="DefaultConnection" connectionString="blablabla" providerName="System.Data.SqlClient" />
    </connectionStrings>    
    <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
    <system.web>
        <compilation debug="true" targetFramework="4.5.1" />
        <httpRuntime targetFramework="4.5.1" />
    </system.web>
    ...
like image 134
huysentruitw Avatar answered Oct 08 '22 07:10

huysentruitw


Connection strings can be added any where in configuration in such a way it should be a child of configuration.
Its recommended that it should be placed after all tags so it remains visible if you need to change it in future.

    <configuration>
 <connectionStrings>
   <add name="defaultConn" 
        connectionString="Server=SERVER; Database=DbName; User Id=userid; password= password"
        providerName="System.Data.SqlClient" />
 </connectionStrings>
</configuration>
like image 23
Shan Khan Avatar answered Oct 08 '22 08:10

Shan Khan


<connectionStrings>
      <add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>

Read more here and here.

like image 1
Amir Popovich Avatar answered Oct 08 '22 07:10

Amir Popovich