Storing MySql Connection string in web.config

cpopham

Junior Contributor
Joined
Feb 18, 2004
Messages
273
I attempting to store my MySql connection string in my web.config file and I keep getting an error. My web.config file looks like the following:

Code:
<configuration>

  <connectionStrings>
    <add name="TestIt" connectionString="Server=localhost;user id=MyUser;password=MyPW;database=test;"
         providerName="mysql.data.mysqlclient"/>
  </connectionStrings>
    
	<system.web>
    <sessionState timeout="60" cookieless="AutoDetect" regenerateExpiredSessionId="true" />
    <compilation debug="true" strict="false" explicit="true">
			<assemblies>
				<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
				<add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
		<pages>
			<namespaces>
				<clear/>
				<add namespace="System"/>
				<add namespace="System.Collections"/>
				<add namespace="System.Collections.Specialized"/>
				<add namespace="System.Configuration"/>
				<add namespace="System.Text"/>
				<add namespace="System.Text.RegularExpressions"/>
				<add namespace="System.Web"/>
				<add namespace="System.Web.Caching"/>
				<add namespace="System.Web.SessionState"/>
				<add namespace="System.Web.Security"/>
				<add namespace="System.Web.Profile"/>
				<add namespace="System.Web.UI"/>
				<add namespace="System.Web.UI.WebControls"/>
				<add namespace="System.Web.UI.WebControls.WebParts"/>
				<add namespace="System.Web.UI.HtmlControls"/>
			</namespaces>
		</pages>

		<authentication mode="Forms"/>
  </system.web>
</configuration>

My code that ues the connectionstring looks like the following:
Code:
        connMyTCO.ConnectionString = "<%$ connectionStrings:TestIt %>"

Now the connection worked fine until I added it to the web.config file. After adding it to the web.config file I get the following error:
Code:
Index was outside the bounds of the array. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error: 


Line 17:     Private Sub OpenDB()
Line 18:         'connMyTCO = New MySqlConnection
Line 19:         connMyTCO.ConnectionString = "<%$ connectionStrings:TestIt %>"
Line 20:         Try
Line 21:             connMyTCO.Open()
 

Source File: C:\WebSites\TCO2\App_Code\DataTier.vb    Line: 19

Any Ideas?

Thank you, Chester
 
Check out the following posting: http://www.xtremedotnettalk.com/showthread.php?t=97111

This is the cmd in vb.net that I use to get the connection string from the web.config file:
Code:
Private Shared connectionString As String = ConfigurationManager.ConnectionStrings (1).toString()
If you have more that one connection string in the web.config, just change the number 1 to the correct number i.e. 2, 3, 4, etc.

Mike55.
 
Back
Top