Wednesday, March 28, 2012

Web.config Help Needed.

Newbie problem!

I have the following in my web.config file:

<configuration>

<appSettings>

<add key="ConnectionString" Value="database string that works when I have it directly in the asp page" />

</appSettings>

</configuration>

The following is my page:

 

<%@dotnet.itags.org. Page Language="VB" %>

<%@dotnet.itags.org. import Namespace="System.Data.SqlClient" %>

<%@dotnet.itags.org. import namespace="System.Configuration" %>

<script runat="server">

Sub Page_Load

Dim conPubs As SqlConnection

Dim strinsert as string

Dim cmdinsert as sqlcommand

conPubs = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

strinsert = "Insert Users (Name, Email, Choice) values ('Kathy','TrueEmail','Yes')"

cmdinsert = New SqlCommand (strinsert, conPubs)

conPubs.Open()

cmdinsert.executenonquery()

conpubs.close()

End Sub

</script>

<html>

<head>

</head>

<body>

<pre>Connection Opened!</pre>

</body>

</html>

From this I get the error page:

Server Error in '/' Application.

Runtime Error

Its like the web.config is not being read properly? Or is there something I have missed? Where should the web.config be located? Do I need to compile it at all?

Without seeing the full runtime exception, it's kind of hard to say. However, I think it's case sensitive, so it should be value instead of Value. Web.config should be located in the root of the site.

HTH,
Ryan


The web.config file should be in the root folder, along with Web.sitemap and Default.aspx. For example let' s say that the website is in C:\Inetpub\wwwroot\Site, in Site should be web.config.
Is always read properly by ASP.NET engine. XML pages are not compiled they are parsed (checked if there is a closing tag, the code coresponds to XML Schema: the tags and their attributes are defined) .
My suggestion: Check the connection string again and the way you defined it in web.config .

0 comments:

Post a Comment