Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,916 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,114 people online right now. Registration is fast and FREE... Join Now!




Connection string not initialized

 
Reply to this topicStart new topic

Connection string not initialized, using ADO.NET

mikjall77
16 Dec, 2007 - 03:52 AM
Post #1

New D.I.C Head
*

Joined: 18 Apr, 2007
Posts: 46



Thanked: 1 times
My Contributions
I'm working on an Address Book using ADO.NET (using Access as my database) and trying for extra credit points. The program starts out displaying a Contact Type dataviewgrid and a contacts dataviewgrid. On this screen, you can edit the Contact Types (no problems here). You can then go and edit the individual contacts within textboxes. This screen also displays the calls made (with the call's information) in a grid. This screen also works fine. My next screen, editing the individual calls, is where my problem lays. When I press the "Edit Calls" button, I'm getting the error "The ConnectionString property has not been initialized." I set this up just like my other forms and can't understand why it won't work. I'll post some of the code (hopefully enough without getting this too long). I am writing this in VB 2005. If you need anymore code or more information, please let me know.
CODE
From my Contacts Form

Private Sub btnEditCalls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditCalls.Click
        Dim frmCalls As New CallsForm(sCN)
        frmCalls.ContactID = txtContactID.Text  'push in the show ID
        frmCalls.txtFirstName.Text = txtFirstName.Text
        frmCalls.txtLastName.Text = txtLastName.Text
        frmCalls.ConnString = sCN
        frmCalls.ShowDialog()
        frmCalls.Dispose()
        'refresh contacts
        Call cmgrCalls_PositionChanged(Me, e)
    End Sub

From my Calls Form

Dim m_SCN As String 'connections string
    Dim m_ContactID As Integer  'contact ID
    Dim oCalls As New clsCalls(m_SCN)
    Dim dsCalls As DataSet
    Dim bFormLoaded As Boolean
    Dim WithEvents cmgrCalls As CurrencyManager

Private Sub CallsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'get the episodes for a Show
        dsCalls = oCalls.GetData(m_ContactID)
        Call DoBinding()
        bFormLoaded = True
    End Sub

From my Calls Class

Dim m_sCN As String  'Connection string for use by this class
    Dim dtErrors As New DataTable
    Private m_oDS As DataSet                        '//--- The DataSet to use
    Private WithEvents m_oDA As OleDb.OleDbDataAdapter         '//--- The DataAdapter that links the DataSet to the Connection
    Private m_sClassName As String = "Calls"     '//--- The name of the class
    Private m_oCn As New OleDb.OleDbConnection          '//--- The Connection the database
    Dim sSQL As String = ""
    Dim oSelCmd As OleDb.OleDbCommand

Public Function GetData(ByVal iContactID As Integer) As DataSet
        Dim sSQLOrig As String = ""   'The original SQL used for all records
        Try
            ''--- Create a new DataSet
            sSQLOrig = m_oDA.SelectCommand.CommandText
            m_oDA.SelectCommand.CommandText &= " where ContactID = " & iContactID.ToString
            m_oCn.Open()
            m_oDS = New DataSet
            ''--- Fill the DataSet with the Customers
            m_oDA.Fill(m_oDS, m_sClassName)
            m_oCn.Close()
            ''//--- Return the DataSet
            Return m_oDS
        Catch ex As Exception
            Throw ex
        Finally
            'reset SQL back to generic 'Grab Everything'
            m_oDA.SelectCommand.CommandText = sSQLOrig
        End Try
    End Function


Any help on this is greatly appreciated


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Connection String Not Initialized
16 Dec, 2007 - 12:17 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
We are going to need to see more code.
User is online!Profile CardPM
+Quote Post

mikjall77
RE: Connection String Not Initialized
16 Dec, 2007 - 05:41 PM
Post #3

New D.I.C Head
*

Joined: 18 Apr, 2007
Posts: 46



Thanked: 1 times
My Contributions
QUOTE(jayman9 @ 16 Dec, 2007 - 01:17 PM) *

We are going to need to see more code.

Here it is:
CODE
From the Call button on the Contacts form

Private Sub btnEditCalls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditCalls.Click
        Dim frmCalls As New CallsForm(sCN)
        frmCalls.ContactID = txtContactID.Text  'push in the show ID
        frmCalls.txtFirstName.Text = txtFirstName.Text
        frmCalls.txtLastName.Text = txtLastName.Text
        frmCalls.ConnString = sCN
        frmCalls.ShowDialog()
        frmCalls.Dispose()
        'refresh contacts
        Call cmgrCalls_PositionChanged(Me, e)
    End Sub

From the Calls Form

Dim m_SCN As String 'connections string
    Dim m_ContactID As Integer  'show ID
    Dim oCalls As New clsCalls(m_SCN)
    Dim dsCalls As DataSet
    Dim bFormLoaded As Boolean
    Dim WithEvents cmgrCalls As CurrencyManager
    Dim iMaxSeason As Integer

    Friend WriteOnly Property ConnString() As String
        Set(ByVal value As String)
            m_SCN = value
        End Set
    End Property

    Friend WriteOnly Property ContactID() As Integer
        Set(ByVal value As Integer)
            m_ContactID = value
        End Set
    End Property

    Private Sub CallsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'get the episodes for a Show
        dsCalls = oCalls.GetData(m_ContactID)
        Call DoBinding()
        bFormLoaded = True
    End Sub

    Public Sub New(ByVal ConnString As String)
        m_SCN = ConnString
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Private Sub DoBinding()
        cmgrCalls = CType(Me.BindingContext(dsCalls.Tables(0)), CurrencyManager)
        'Take care of defaults
        If dsCalls.Tables(0).Columns("CallDate").DefaultValue Is System.DBNull.Value Then
            dsCalls.Tables(0).Columns("CallDate").DefaultValue = Now
        End If
        If dsCalls.Tables(0).Columns("CallTime").DefaultValue Is System.DBNull.Value Then
            dsCalls.Tables(0).Columns("CallTime").DefaultValue = False
        End If

        'bind the controls
        grdCalls.DataSource = dsCalls.Tables(0)
        cboCallID.DataSource = dsCalls.Tables(0)
        cboCallID.DisplayMember = "CallID"
        cboCallID.ValueMember = "CallID"
        txtCallID.DataBindings.Add("Text", dsCalls.Tables(0), "CallID")
        txtContactID.DataBindings.Add("Text", dsCalls.Tables(0), "ContactID")
        dtpCallDate.DataBindings.Add("Text", dsCalls.Tables(0), "CallDate")
        dtpCallTime.DataBindings.Add("Text", dsCalls.Tables(0), "CallTime")
        txtSubject.DataBindings.Add("Text", dsCalls.Tables(0), "Subject")
        txtNotes.DataBindings.Add("Text", dsCalls.Tables(0), "Notes")

    End Sub

    Private Sub Unbind()
        'unbind controls to refresh the data
        cmgrCalls = Nothing
        cboCallID.DataSource = Nothing
        txtCallID.DataBindings.Clear()
        txtContactID.DataBindings.Clear()
        dtpCallDate.DataBindings.Clear()
        dtpCallTime.DataBindings.Clear()
        txtSubject.DataBindings.Clear()
        txtNotes.DataBindings.Clear()
        grdCalls.DataSource = Nothing
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        'add a new record
        cmgrCalls.AddNew()
        Dim rw As DataRowView
        rw = cmgrCalls.Current
        rw.Item("ContactID") = m_ContactID
        dtpCallDate.Focus()
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        'go to first record
        cmgrCalls.Position = 0
    End Sub

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        'go to previous record
        cmgrCalls.Position -= 1
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        'go to next record
        cmgrCalls.Position += 1
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        'go to previous record
        cmgrCalls.Position = cmgrCalls.Count - 1
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        'delete an episode
        cmgrCalls.EndCurrentEdit()
        Dim rwCalls As Data.DataRowView = cmgrCalls.Current
        rwCalls.Delete()
    End Sub

    Private Sub btSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSave.Click
        Dim dsChanges As DataSet
        Dim sMsg As String
        Dim dtErrors As DataTable
        Dim oCalls As New clsCalls(m_SCN)
        cmgrCalls.EndCurrentEdit()
        dsChanges = dsCalls.GetChanges 'Get all rows that have been changed
        slblMsg.Text = oCalls.SaveData(dsChanges, dtErrors)
        If Not dtErrors Is Nothing Then
            'no errors
            dsCalls.AcceptChanges()
        Else
            'show the users the error rows
        End If
        'reload data
        Call Unbind()
        dsCalls.Clear()
        dsCalls = oCalls.GetData
        Call DoBinding()

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'cancel a change
        cmgrCalls.CancelCurrentEdit()

    End Sub

    Private Sub btnCancelAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelAll.Click
        'Cancel all changes
        cmgrCalls.CancelCurrentEdit()
        dsCalls.RejectChanges()
    End Sub

    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
        Me.Close()
    End Sub
End Class

From the Calls Class itself

Public Class clsCalls
    Dim m_sCN As String  'Connection string for use by this class
    Dim dtErrors As New DataTable
    Private m_oDS As DataSet                        '//--- The DataSet to use
    Private WithEvents m_oDA As OleDb.OleDbDataAdapter         '//--- The DataAdapter that links the DataSet to the Connection
    Private m_sClassName As String = "Calls"     '//--- The name of the class
    'Private sCN As String   'conncection String
    Private m_oCn As New OleDb.OleDbConnection          '//--- The Connection the database
    Dim sSQL As String = ""
    Dim oSelCmd As OleDb.OleDbCommand
    'Dim oInsCmd As OleDb.OleDbCommand
    'Dim oUpdCmd As OleDb.OleDbCommand
    'Dim oDelCmd As OleDb.OleDbCommand

    Public Sub New(ByVal sConn As String)
        m_sCN = sConn
        m_oCn.ConnectionString = m_sCN
        '---------------------------------------------------------
        '--- Create and set up the DataAdapter
        '---------------------------------------------------------
        m_oDA = New OleDb.OleDbDataAdapter
        With m_oDA
            .SelectCommand = CreateDASelectCommand()
            '.UpdateCommand = CreateDAUpdateCommand()
            '.DeleteCommand = CreateDADeleteCommand()
            '.InsertCommand = CreateDAInsertCommand()
        End With
        Dim cmdBld As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(m_oDA)

    End Sub

    Private Function CreateDASelectCommand() As OleDb.OleDbCommand
        Dim sSQL As String      'string with SQL
        Dim cmd As New OleDb.OleDbCommand(sSQL, m_oCn) 'command object
        '---------------------------------------------------------
        '--- Set up the SELECT Command
        '---------------------------------------------------------
        sSQL = "select CallID, ContactID, CallDate, CallTime, Subject, Notes from tblCalls"   'SQL to use
        'sSQL &= " where ShowID = " & iShowID
        cmd.CommandText = sSQL
        cmd.CommandType = CommandType.Text  'this is SQL rather than a stored procedure
        Return cmd 'return the full command
    End Function

    Public Function SaveData(ByVal oDS As DataSet, ByRef dtErrors As DataTable) As String
        Dim sMsg As String
        Dim lRecsAffected As Long
        '---------------------------------------------------------
        '//--- Save the data
        '---------------------------------------------------------
        Try
            '--- Open the conection manually
            m_oCn.Open()
            '--- Make all database changes
            lRecsAffected = m_oDA.Update(oDS, m_sClassName)
            '--- Set the message for the user
            sMsg = lRecsAffected & " Record(s) Were Updated"
        Catch e As Exception
            sMsg = "Records were not updated" & vbCrLf & e.Message.ToString()
        Finally
            '--- Close the connection that we manually opened
            m_oCn.Close()
            SaveData = sMsg
        End Try

    End Function

    '//------------------------------------------------------------------------------------------------------------
    '// Public Method
    '// Overloaded:        Yes
    '// Parameters:        None
    '// Return Value:    DataSet
    '// Purpose:        Retrieves all episodes.
    '//------------------------------------------------------------------------------------------------------------
    Public Function GetData() As DataSet
        ''--- Create a new DataSet
        m_oCn.Open()
        m_oDS = New DataSet
        ''--- Fill the DataSet with the Customers
        m_oDA.Fill(m_oDS, m_sClassName)
        m_oCn.Close()
        ''//--- Return the DataSet
        Return m_oDS
    End Function

    '//------------------------------------------------------------------------------------------------------------
    '// Public Method
    '// Overloaded:        Yes
    '// Parameters:        ContactID
    '// Return Value:    DataSet
    '// Purpose:        Retrieves all calls for a given contact.
    '//------------------------------------------------------------------------------------------------------------
    Public Function GetData(ByVal iContactID As Integer) As DataSet
        Dim sSQLOrig As String = ""   'The original SQL used for all records
        Try
            ''--- Create a new DataSet
            sSQLOrig = m_oDA.SelectCommand.CommandText
            m_oDA.SelectCommand.CommandText &= " where ContactID = " & iContactID.ToString
            m_oCn.Open()
            m_oDS = New DataSet
            ''--- Fill the DataSet with the Customers
            m_oDA.Fill(m_oDS, m_sClassName)
            m_oCn.Close()
            ''//--- Return the DataSet
            Return m_oDS
        Catch ex As Exception
            Throw ex
        Finally
            'reset SQL back to generic 'Grab Everything'
            m_oDA.SelectCommand.CommandText = sSQLOrig
        End Try

    End Function

    Friend Function GetNumOfCalls(ByVal ContactID As Integer) As Integer
        'Input - the ID of the show we want to count episodes for
        'output - the number of episodes for a given show
        Dim iReturn As Integer  'variable to stash return information
        Dim sSQL As String
        sSQL = "select count(CallID) from tblCalls where ContactID = " & ContactID
        Dim cn As New OleDb.OleDbConnection(m_sCN)
        Dim cmd As New OleDb.OleDbCommand(sSQL, cn)
        cn.Open()
        Dim reader As OleDb.OleDbDataReader = cmd.ExecuteReader()
        While reader.Read()
            iReturn = reader.GetInt32(0)
        End While
        reader.Close()
        cn.Close()
        'cleanup
        reader = Nothing
        cmd.Dispose()
        cn.Dispose()
        Return iReturn  'return the couont of episodes
    End Function
End Class


I've also added the entire file just in case




Attached File(s)
Attached File  ContactManager.zip ( 394.29k ) Number of downloads: 23
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Connection String Not Initialized
16 Dec, 2007 - 06:38 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Where do you supply the connection string to the ConnString property?

In fact, I don't see anywhere on any of your classes where you set the OleDBConnection to a database connection string. I see you creating the object, but you never give it the connection string.

Perhaps you need to review some ADO.NET basics. You might start with the following tutorial.

http://www.dreamincode.net/forums/showtopic33908.htm
http://www.java2s.com/Tutorial/VB/0480__Da...essdatabase.htm
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 02:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month