Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Share Connection String 2

Status
Not open for further replies.

Crazyec

Programmer
Joined
Aug 28, 2001
Messages
80
Location
HK
Hi all, I have a question on sharing the connection string in web.config file.

In web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<sessionState mode="InProc" cookieless="false" timeout="20" />
<pages enableViewState="true" />
<compilation debug="true"/>
<customErrors mode="Off"/>
</system.web>

<connectionStrings>
<add name="cnn1" connectionString="Server=Server3;;uid=sa;password=123456;Database=db1;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

In my page:
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Web.Security " %>
<script runat="server">

Dim myconnection As SqlConnection

Sub Save (Sender As Object, E as EventArgs)
myconnection = New SqlConnection("Server=Server3;;uid=sa;password=123456;Database=db1;"
")

...
End Sub

I wanna use the connection string in Web.Config
what should I do in the page file?
Thank you.

 
Crazyec - take a look at thread855-924229.
 
thx, actually I have read it and tried it

In Web.config, I add:
<appSettings>
<add key="cnn1" value="Server=server3;uid=sa;password=123456;database=db1;" providerName="System.Data.SqlClient" />
</appSettings>

In the page file, I add:
<%@ Import Namespace="System.Web" %>
:
:
Sub Save (Sender As Object, E as EventArgs)
:

myconnection = New SqlConnection(ConfigurationSettings.AppSettings["cnn1"])
:
End Sub

Finally, I get the following error.

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30311: Value of type 'System.Collections.Specialized.NameValueCollection' cannot be converted to 'String'.

Source Error:

Line 57: myconnection = New SqlConnection(ConfigurationSettings.AppSettings["cnn1"])



Is there any thing I missing?
 
In your "myconnection = " code, change the"[" and ":]" to "(" and ")"

Code:
myconnection = New SqlConnection(ConfigurationSettings.AppSettings[b]([/b]"cnn1"[b])[/b])

Jim
 
Thank you, Jim.

It works!

Eric
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top