ASP.NET Connection String Problem RRS feed

  • Question


     I currently have a problem with asp.net web applications using database connections on my server, but they work fine on my test PC.


    The error I get is: The connection name 'ConnectionString1' was not found in the applications configuration or the connection string is empty.

    I can't work out how this is working in my test environment but not on my live.

    I've created a simple new project that is simply a table with a select statement to an oracle database (connection details replaced with X's):


    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="testbdaccess._Default" debug="true" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
        <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
                ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>" SelectCommand='SELECT * FROM "APU_FEEDBACK_MESSAGES"'>


    <?xml version="1.0"?>
            <add name="ConnectionString1" connectionString="Data Source=XXXXXX_XXXXXX.XXX.XXX.XX;Persist Security Info=True;User ID=XXXX_X;Password=XXXXXX;Unicode=True"
                providerName="System.Data.OracleClient" />
                Set compilation debug="true" to insert debugging 
                symbols into the compiled page. Because this 
                affects performance, set this value to true only 
                during development.
                Visual Basic options:
                Set strict="true" to disallow all data type conversions 
                where data loss can occur. 
                Set explicit="true" to force declaration of all variables.
            <compilation debug="true" strict="false" explicit="true" />
                    <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" />
                The <authentication> section enables configuration 
                of the security authentication mode used by 
                ASP.NET to identify an incoming user. 
            <authentication mode="Windows" />
                The <customErrors> section enables configuration 
                of what to do if/when an unhandled error occurs 
                during the execution of a request. Specifically, 
                it enables developers to configure html error pages 
                to be displayed in place of a error stack trace.
            <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
                <error statusCode="403" redirect="NoAccess.htm" />
                <error statusCode="404" redirect="FileNotFound.htm" />

    The error:

    Server Error in '/testdbaccess' Application.

    The connection name 'ConnectionString1' was not found in the applications configuration or the connection string is empty.
    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 connection name 'ConnectionString1' was not found in the applications configuration or the connection string is empty.

    Source Error:
    Line 12:         <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
    Line 13:         </asp:GridView>
    Line 14:         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
    Line 15:             ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>" SelectCommand='SELECT * FROM "APU_FEEDBACK_MESSAGES"'>
    Line 16:         </asp:SqlDataSource>

    Source File: E:\Applications\WebApps\live_pages\testdbaccess\Default.aspx    Line: 14

    Stack Trace:

    [InvalidOperationException: The connection name 'ConnectionString1' was not found in the applications configuration or the connection string is empty.]
       System.Web.Compilation.ConnectionStringsExpressionBuilder.GetConnectionString(String connectionStringName) +2955101
       ASP.default_aspx.__BuildControlSqlDataSource1() in E:\Applications\WebApps\live_pages\testdbaccess\Default.aspx:14
       ASP.default_aspx.__BuildControlform1() in E:\Applications\WebApps\live_pages\testdbaccess\Default.aspx:10
       ASP.default_aspx.__BuildControlTree(default_aspx __ctrl) in E:\Applications\WebApps\live_pages\testdbaccess\Default.aspx:1
       ASP.default_aspx.FrameworkInitialize() +39
       System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +40
       System.Web.UI.Page.ProcessRequest() +86
       System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +18
       System.Web.UI.Page.ProcessRequest(HttpContext context) +49
       ASP.default_aspx.ProcessRequest(HttpContext context) +29
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +154
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64


    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

    Any help would be great as this is driving me mad!!

    • Edited by mentalmike85 Thursday, July 23, 2009 9:50 AM corrected formating
    Thursday, July 23, 2009 9:44 AM

All replies

  • Hi

    If there is a difference in the aspx code and the web.config file when it comes to the connectionstring name this error would show.
    This doesn't seem to be the case here.

    The error would also show if the section is empty which it could be if the wrong web.config is checked.
    Most likely your application on the test machine is reading from a different web.config that doesn't have your connection string entry.

    Download Process Monitor from here:

    Set a filter to be "Path" "Ends with" "web.config" and add it to the filter list. Start the monitoring, run your application. 
    This should reveal where the application is reading the web.config from.

    This posting is provided "AS IS" with no warranties.
    Thursday, July 23, 2009 1:24 PM
  • Hi, thanks for the reply.

    I have run the monitor and there are a few entries for the correct web.config file to start with and then there are loads that are linking to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config

    Any idea on why it is looking here as well? I thought it should look at the local one first or is there something configured wrong on the server?

    Thursday, July 23, 2009 2:46 PM