"Connection has not been initialised"

Shaddix

Member
Joined
May 11, 2007
Messages
5
Programming Experience
Beginner
I'm trying to pick up ADO.NET and VB.NET again, but I'm getting an error when I try to execute a piece of code I've written I get the error "Connection has not been initialised"

VB.NET:
        Dim strValue As String

        Dim cnnDatabase As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Tim\Documents\F1TB\F1TB\F1TB.accdb")
        Dim ocmdCommand As New OleDb.OleDbCommand
        Dim dtrDataReader As OleDb.OleDbDataReader

        ocmdCommand.CommandText = "SELECT Team FROM tblTeam"

        cnnDatabase.Open()
        dtrDataReader = ocmdCommand.ExecuteReader

        strValue = dtrDataReader.Item("Team")

        dtrDataReader.Close()
        cnnDatabase.Close()

the line of code visual studio gives the error on is:

VB.NET:
dtrDataReader = ocmdCommand.ExecuteReader
 
You have created a connection object and command object but have not connected the connection to the command.

VB.NET:
        Dim cnnDatabase As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Tim\Documents\F1TB\F1TB\F1TB.accdb")
        Dim ocmdCommand As New OleDb.OleDbCommand
        Dim dtrDataReader As OleDb.OleDbDataReader

        [COLOR="Red"][B]ocmdCommand.Connection = cnnDatabase[/B][/COLOR]
        ocmdCommand.CommandText = "SELECT Team FROM tblTeam"
        cnnDatabase.Open()

        dtrDataReader = ocmdCommand.ExecuteReader
        strValue = dtrDataReader.Item("Team")

        dtrDataReader.Close()
        cnnDatabase.Close()
 
thanx for the fast reply, that helps to solve the problem more or less :)
but now the program is stuck on the rule

strValue = dtrDataReader.Item("Team")

it says there is no data in the column/row

first I thought the error would be in the querry, but when I tried to execute the querry in MS Acces, it worked
 
VB.NET:
        Dim cnnDatabase As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Tim\Documents\F1TB\F1TB\F1TB.accdb")
        Dim ocmdCommand As New OleDb.OleDbCommand
        Dim dtrDataReader As OleDb.OleDbDataReader

        [COLOR="SeaGreen"]' Assign connection to command object.[/COLOR]
        [COLOR="Red"][B]ocmdCommand.Connection = cnnDatabase[/B][/COLOR]
        ocmdCommand.CommandText = "SELECT Team FROM tblTeam"
        cnnDatabase.Open()

        dtrDataReader = ocmdCommand.ExecuteReader
        
        [COLOR="SeaGreen"]' Call Read before accessing data.[/COLOR]
[COLOR="Red"]        While dtrDataReader.Read()
            strValue = dtrDataReader.Item("Team").ToString
            MsgBox(strValue)
        End While[/COLOR]

        dtrDataReader.Close()
        cnnDatabase.Close()
 
Though its a bit more of a nuisance than using Access, I'd still advocate followign the advice given in the DW2 link in my signature for the ACE db.. Start with the section Creating a Simple Data APp

If youre getting back into VB, you should be looking to make a fresh start with learning the latest, recommended ways of doing things. All this messing about with datareaders is rather old hat..
 
Back
Top