none
The ConnectionString property has not been initialized... RRS feed

  • Question

  • I'm having a very strange problem with this error message:

     

    "The ConnectionString property has not been initialized."

     

    I am using Visual Studio 2008 and targetting the .NET Framework 3.5 in all projects and connecting to SQL Server 2000 database.

     

    I get this error message when trying to open a connection to the server.  However, if I set a breakpoint and step through the code, everything works just fine and no errors are encountered.  If I remove the breakpoint, this error pops up again on the SqlConnection.Open call.  Here is the relevant code:

     

    Code Snippet

    Using DbConnections.ArcConnection

    DbConnections.ArcConnection.Open() '-- ConnectionString Error Here!

     

    Dim command As SqlCommand = DbConnections.ArcConnection.CreateCommand

    command.CommandText = SQL_SELECTPOSORDER

    command.Parameters.AddWithValue("OrderID", orderId)

    Dim reader As SqlDataReader

    Try

    reader = command.ExecuteReader(CommandBehavior.CloseConnection)

    Do While reader.Read

    order.OrderID = reader.GetInt32(reader.GetOrdinal("OrderID"))

    Loop

    reader.Close()

    Catch ex As Exception

    Throw

    Finally

    command.Dispose()

    End Try

    End Using

     

     

    Here are the relevant properties and methods from the DbConnections object:

     

    Code Snippet

    Private Shared _arcConnection As SqlConnection

    Public Shared ReadOnly Property ArcConnection() As SqlConnection

    Get

    If _arcConnection Is Nothing Then

    _arcConnection = New SqlConnection(GetConnectionString(ARC_CONNECTIONSTRINGNAME))

    End If

    Return _arcConnection

    End Get

    End Property

     

    Private Shared Function GetConnectionString(ByVal connectionStringName As String) As String

     

    Dim machineStrings As ConnectionStringSettingsCollection

    Dim connectionString As String

     

    machineStrings = ConfigurationManager.ConnectionStrings

    connectionString = machineStrings.Item(connectionStringName).ConnectionString

    Return connectionString

    End Function

     

     

    If I evaluate _arcConnection after getting the "not initialized" error, _arcConnection is NOT nothing, but the ConnectionString property of _arcConnection is nothing. 

     

    Again, if I set a break point and step through code, everything works as expected.  The connection string (with appropriate connectionstring name value) do exist in the machine.config and it is all correct.  This code works fine as long as I have a breakpoint on the SqlConnection.Open call and step through it.

     

    Any help or suggestions for troubleshooting this problem would be greatly appreciated.

     

     

    Friday, February 1, 2008 8:23 PM

Answers

  • Code Snippet

    DbConnections.ArcConnection.Open() '-- ConnectionString Error Here!

     

    Dim command As SqlCommand = DbConnections.ArcConnection.CreateCommand

     

     

     

    You're opening a connection then opening and creating again.

     

    Comment out the first line and you should be fine.

     

    Adam

    Friday, February 1, 2008 8:29 PM

All replies

  • Code Snippet

    DbConnections.ArcConnection.Open() '-- ConnectionString Error Here!

     

    Dim command As SqlCommand = DbConnections.ArcConnection.CreateCommand

     

     

     

    You're opening a connection then opening and creating again.

     

    Comment out the first line and you should be fine.

     

    Adam

    Friday, February 1, 2008 8:29 PM
  • That was exactly the problem.  I had just moved the DbConnections class to its own project when this problem started happening.  Here is the new version of my code:

     

    Code Snippet

    Using Command As SqlCommand = DbConnections.ArcConnection.CreateCommand

    DbConnections.ArcConnection.Open()

    Command.CommandText = SQL_SELECTPOSORDER

    Command.Parameters.AddWithValue("OrderID", orderId)

    Dim reader As SqlDataReader

    Try

    reader = Command.ExecuteReader(CommandBehavior.CloseConnection)

    Do While reader.Read

    order.OrderID = reader.GetInt32(reader.GetOrdinal("OrderID"))

    Loop

    reader.Close()

    Catch ex As Exception

    Throw

    End Try

     

    Return order

    End Using

     

     

    Thanks for you help!
    Friday, February 1, 2008 9:16 PM