Visual Studio .NET - ConfigurationManager.connectionStrings = Object reference not set to an instance of an obj

Asked By peleg k on 09-Aug-07 06:35 PM

i work on winxp with Net2 ver 2.0.50727 in the IIS (under ASP.NET)
in my web.config file i have :
    <add name="local_con" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=False;User Id=xxx;Password=xxxx;database=db1"
   providerName="System.Data.SqlClient" />
when i use on my pc this code :
<%@ Page Language="vb" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Configuration" %>

<script runat="server">

    Sub Page_Load(sender As Object, e As EventArgs)
    Dim conLath As SqlConnection
    Dim cmdLath As SqlCommand
    Dim rdrLath As SqlDataReader
    Dim connString As String = ConfigurationManager.connectionStrings("local_con").ConnectionString
    Using myConnection As New SqlConnection(connString)
                Catch Obj As Exception
                End Try
                'Create a SqlCommand instance
                Dim myCommand As New SqlCommand("select * from tbl1", myConnection)
                Dim reader As SqlDataReader = myCommand.ExecuteReader()
            End Using
    End Sub

everything works great!
when  i move my project to win2003 server i recive the next error : (the ASP.NET version there is the same!)
Server Error in '/mySite' Application.

Object reference not set to an instance of an object.
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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 12:     Dim cmdLath As SqlCommand
Line 13:     Dim rdrLath As SqlDataReader
Line 14:     Dim connString As String = ConfigurationManager.connectionStrings("local_Con").ConnectionString

Source File: C:\Inetpub\wwwroot\mySite\try2connec2.aspx    Line: 14

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   ASP.try2connec2_aspx.Page_Load(Object sender, EventArgs e) in C:\Inetpub\wwwroot\mySite\try2connec2.aspx:14
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

any idea why this can happend?
thnaks in advance

Deepak Kumar replied to peleg k on 02-Apr-12 08:09 AM
just use it:

ConfigurationManager.connectionStrings["local_con"].ConnectionString   instead of

//ConfigurationManager.connectionStrings("local_con").ConnectionString  \ /