I am getting The ConnectionString property has not been initialized.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • enliven60411
    New Member
    • Dec 2009
    • 10

    #1

    I am getting The ConnectionString property has not been initialized.

    Code:
    Public Sub CreateNewPersonRestrictions()
    
            Dim cmd As New SqlCommand
            Dim conn As New SqlConnection
    
            'Declare datareader
            Dim NewPersonRestrictionDataReader As SqlDataReader
    
            'to initalize the connection of datareader to demograchics
            conn.Open()  --error here
    
            NewPersonRestrictionDataReader = cmd.ExecuteReader
    
            Try
    
                ' Get Logged-in User Name and the CreateNewPersonValue
                Dim ctUser As New sqlctDemographics.ctDemographics
    
                Dim NewPerson As Boolean
                Dim ctr As Control
                NewPersonRestrictionDataReader = ctDemographics.GetCreateNewPersonRestrictions 'Changed ctUser.GetCreateNewPersonRestrictions
                ' check whether there's a Record
                If NewPersonRestrictionDataReader.Read Then 'reading first (and only) record
                    'Set the datareader create newperson value to the newperson variable
                    NewPerson = NewPersonRestrictionDataReader.Item("CreateNewPerson")
                    'Check the new person value
                    If NewPerson = False Then
                        'The value is false, disable the buttons in the startup form.
                        For Each ctr In CallingForm.Controls
                            If ctr.Name = "btnNewEntry" = True Then
                                ctr.Enabled = False
                            End If
                            If ctr.Name = "btnNewNonSSNEntry" = True Then
                                ctr.Enabled = False
                            End If
                        Next
                    End If
    
                End If
            Catch ex As Exception
                MsgBox(ex)
    
                'close the username data reader
                NewPersonRestrictionDataReader.Close()
    
            Finally
    
                ' close the datareaders in case they're still open 
                If Not (NewPersonRestrictionDataReader Is Nothing) Then
                    NewPersonRestrictionDataReader.Close()
                End If
    
            End Try
    
        End Sub
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    You have not set up a connection string for your conn object. Note lines 4 and 6 in the code below from MSDN help on the SQLConnection class:

    Originally posted by MSDN
    Example
    [Visual Basic, C#, C++] The following example creates a SqlCommand and a SqlConnection. The SqlConnection is opened and set as the Connection for the SqlCommand. The example then calls ExecuteNonQuery , and closes the connection. To accomplish this, the ExecuteNonQuery is passed a connection string and a query string that is a Transact-SQL INSERT statement.

    [Visual Basic]
    Code:
    Public Sub InsertRow(myConnectionString As String)
        ' If the connection string is null, use a default.
        If myConnectionString = "" Then
            myConnectionString = "Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;"
        End If
        Dim myConnection As New SqlConnection(myConnectionString)
        Dim myInsertQuery As String = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')"
        Dim myCommand As New SqlCommand(myInsertQuery)
        myCommand.Connection = myConnection
        myConnection.Open()
        myCommand.ExecuteNonQuery()
        myCommand.Connection.Close()
    End Sub 'SelectSqlClientSrvRows
    -Stewart

    Comment

    • enliven60411
      New Member
      • Dec 2009
      • 10

      #3
      Thanks for the help.

      Comment

      Working...