tilgå connectionstring fra web.config fil..
Jeg har problemer med at få connection string fra web.config fil til at fungere med aspx siden. Når jeg prøver på at køre filen, får jeg følgende fejl meddelelse:Server Error in '/reclaes' Application.
-----------------------------------------------------------
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.
min web.config fil ser sådan ud:
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings>
<add name="DbConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" />
<authentication mode="Forms" />
</system.web>
</configuration>
ASPX.vb fil ser sådan her ud:
Imports System
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class ConnString : Inherits Page
Protected Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
Dim sqlConn As SqlConnection
Dim sqlCmd As SqlCommand
Dim strConnection As String
strConnection = System.Configuration.ConfigurationManager.AppSettings("DbConnectionString")
sqlConn = New SqlConnection(strConnection)
sqlCmd = New SqlCommand("SELECT * FROM adresser", sqlConn)
sqlConn.Open()
Dim reader As SqlDataReader = sqlCmd.ExecuteReader
While reader.Read
Response.Write(reader.GetString("fornavn"))
End While
sqlConn.Close()
'Response.Write(System.Configuration.ConfigurationManager.AppSettings("DbConnectionString"))
End Sub
End Class
Hvad gør jeg forkert?
