I have this connection in my aspx file:
Dim strConnection As String = "server=MARINERHOME; database=Northwind; " & _
"integrated security=true"
It works great.
----------------------------------------------------------
I'm trying to move it to the web.config file like so:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<customErrors mode="Off" />
<compilation debug="true"/>
</system.web>
<appSettings>
<add key="NWind" value="server=marinerhome; database=Northwind; integrated security=true;" />
</appSettings>
</configuration>
and call it from the aspx file like so:
<script language="
VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
Dim strConnection As String = ConfigurationSettings.AppSettings("NWind")
Dim objConnection As New SqlConnection(strConnection)
Dim strSQL As String = "SELECT FirstName, LastName, Country " & _
"FROM Employees;"
Dim objCommand As New SqlCommand(strSQL, objConnection)
objConnection.Open()
Response.Write("ServerVersion: " & objConnection.ServerVersion & _
vbCRLF & "Datasource: " & objConnection.DataSource & _
vbCRLF & "Database: " & objConnection.Database)
dgNameList.DataSource = objCommand.ExecuteReader()
dgNameList.DataBind()
objConnection.Close()
End Sub
</script>
This is the result I get when trying to call the aspx file from a browser.
The ConnectionString property has not been initialized.
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.InvalidOperationException: The ConnectionString property has not been initialized.
Source Error:
Line 30: Dim objCommand As New SqlCommand(strSQL, objConnection)
Line 31:
Line 32: objConnection.Open()
Line 33:
Line 34: Response.Write("ServerVersion: " & objConnection.ServerVersion & _
I've tried the common mistakes with no luck. Can someone please help.
Thanks!
Rich