ExecuteReader: Connection property has not been initialized.

I have this app that reads images from a folder.  Well, I'm trying to add on to this by using the image file names to query the database and display additional information.  I keep getting this one particular error:

ExecuteReader: Connection property has not been initialized.

at line 97:
"reader = cmd.ExecuteReader"

Can someone take a look?


Thank you
Imports System.IO
Imports System.Data.OleDb

Public Class Form1

    Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim reader As OleDbDataReader
    Dim IDholder As String
    Dim IDholder2 As String

    Private Index As Integer = -1
    Private Extract As Integer
    Private FolderCount As Integer
    Private FileName As String
    Private ImageFileInfos As New List(Of FileInfo)
    Private intLoop As Integer = 0
    Private trackImage As Integer = 0
    Dim counter As Integer = 1


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Image\Student.accdb;Persist Security Info=False;"
        conn.Open()
        Dim folder As String = "C:\Image\"
        ListFolders(folder)
        counter = counter + 1

    End Sub
    Private Sub ListFolders(ByVal RootFolder As String)
        FlowLayoutPanel1.AutoScroll = True
        FlowLayoutPanel1.Controls.Clear()

        For Each Di As DirectoryInfo In New DirectoryInfo(RootFolder).GetDirectories
            Dim rb As New RadioButton
            rb.Text = Di.Name
            rb.Tag = Di
            AddHandler rb.CheckedChanged, AddressOf rb_CheckedChanged
            FlowLayoutPanel1.Controls.Add(rb)
        Next
        If FlowLayoutPanel1.Controls.Count > 0 Then
            CType(FlowLayoutPanel1.Controls(0), RadioButton).Checked = True
        End If
    End Sub

    Private Sub rb_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        trackImage = 0
        Dim rb As RadioButton = CType(sender, RadioButton)
        If rb.Checked Then
            LoadFolder(CType(rb.Tag, DirectoryInfo).FullName)
        End If
    End Sub

    Private Sub LoadFolder(ByVal FolderPath As String)

        FolderCount = System.IO.Directory.GetFiles(FolderPath).Length()
        'MsgBox(FolderCount)

        Index = -1
        PictureBox1.Image = Nothing
        ImageFileInfos.Clear()
        ImageFileInfos.AddRange(New DirectoryInfo(FolderPath).GetFiles("*.jpg"))
        btnNext.PerformClick()
    End Sub


    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If trackImage < FolderCount Then
            trackImage = trackImage + 1
        End If
        Label1.Text = trackImage & "/" & FolderCount
        If ImageFileInfos.Count > 0 Then
            If ((ImageFileInfos.Count - 1) = Index) Then
                'MsgBox("Stop")
                Return
            End If
            Index = Index + 1
            'If Index = Index + 1 Then
            '  Index = 0
            '   MsgBox("In")
            'End If
            Using fs As New FileStream(ImageFileInfos(Index).FullName, FileMode.Open)
                'MsgBox(Index)
                'MsgBox(ImageFileInfos(Index).Name)
                Extract = InStr(ImageFileInfos(Index).Name, ".")
                FileName = Mid(ImageFileInfos(Index).Name, 1, Extract - 1)
                'Getting the first 9 characters of the I
                IDholder2 = Mid(FileName, 1, 9)
                PictureBox1.Image = Image.FromStream(fs)

            End Using
            cmd.CommandText = "SELECT Student_ID, Last_Name, First_Name, Student_Picture FROM Student WHERE Student_ID = " & IDholder2
            'MsgBox(reader(3))
            reader = cmd.ExecuteReader
            cmd = conn.CreateCommand()
            reader.Close()
            conn.Close()
            Me.Text = Path.GetFileNameWithoutExtension(ImageFileInfos(Index).Name)
        End If


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ImageFileInfos.Count > 0 Then

            If (Index = 0) Then
                'MsgBox("Last Picture")
                Return
            End If
            Index = Index - 1
            'If Index = Index + 1 Then
            '  Index = 0
            '   MsgBox("In")
            'End If
            Using fs As New FileStream(ImageFileInfos(Index).FullName, FileMode.Open)
                PictureBox1.Image = Image.FromStream(fs)
                trackImage = trackImage - 1
                Label1.Text = trackImage & "/" & FolderCount
            End Using
            Me.Text = Path.GetFileNameWithoutExtension(ImageFileInfos(Index).Name)
        End If
    End Sub
End Class

Open in new window

huBelialAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
change your initialization a bit
Imports System.IO
Imports System.Data.OleDb

Public Class Form1

    Dim conn As New OleDbConnection
    Dim cmd As OleDbCommand
    Dim reader As OleDbDataReader
    Dim IDholder As String
    Dim IDholder2 As String

    Private Index As Integer = -1
    Private Extract As Integer
    Private FolderCount As Integer
    Private FileName As String
    Private ImageFileInfos As New List(Of FileInfo)
    Private intLoop As Integer = 0
    Private trackImage As Integer = 0
    Dim counter As Integer = 1


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Image\Student.accdb;Persist Security Info=False;"
        conn.Open()
        Dim folder As String = "C:\Image\"
        ListFolders(folder)
        counter = counter + 1
        cmd = new OleDbCommand(conn)
    End Sub

...

Open in new window

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
the issue is that your code does not link the conn object to the cmd object ...
0
huBelialAuthor Commented:
I get this error on line 29:

Value of type 'System.Data.OleDb.OleDbConnection' cannot be converted to 'String'.
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Pratima PharandeCommented:
try like belwo

   conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Image\Student.accdb;Persist Security Info=False;")
Imports System.IO
Imports System.Data.OleDb

Public Class Form1

    Dim conn As  OleDbConnection
    Dim cmd As OleDbCommand
    Dim reader As OleDbDataReader
    Dim IDholder As String
    Dim IDholder2 As String

    Private Index As Integer = -1
    Private Extract As Integer
    Private FolderCount As Integer
    Private FileName As String
    Private ImageFileInfos As New List(Of FileInfo)
    Private intLoop As Integer = 0
    Private trackImage As Integer = 0
    Dim counter As Integer = 1



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Image\Student.accdb;Persist Security Info=False;")
        conn.Open()
        Dim folder As String = "C:\Image\"
        ListFolders(folder)
        counter = counter + 1
        cmd = new OleDbCommand(conn)
    End Sub

Open in new window

0
huBelialAuthor Commented:
I got the same error on the same line.


31: "cmd = new OleDbCommand(conn)"
0
Pratima PharandeCommented:
try like this

Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection
0
Pratima PharandeCommented:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Image\Student.accdb;Persist Security Info=False;")
        conn.Open()
        Dim folder As String = "C:\Image\"
        ListFolders(folder)
        counter = counter + 1
        cmd = new OleDbCommand()
        cmd.Connection = conn
    End Sub
0
Carl TawnSystems and Integration DeveloperCommented:
Your problem is with this small block:
cmd.CommandText = "SELECT Student_ID, Last_Name, First_Name, Student_Picture FROM Student WHERE Student_ID = " & IDholder2
'MsgBox(reader(3))
reader = cmd.ExecuteReader
cmd = conn.CreateCommand()

Open in new window

You need to call conn.CreateCommand() first in that sequence. So it should be:
cmd = conn.CreateCommand()
cmd.CommandText = "SELECT Student_ID, Last_Name, First_Name, Student_Picture FROM Student WHERE Student_ID = " & IDholder2
'MsgBox(reader(3))
reader = cmd.ExecuteReader

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
huBelialAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.