huBelial
asked on
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
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
the issue is that your code does not link the conn object to the cmd object ...
ASKER
I get this error on line 29:
Value of type 'System.Data.OleDb.OleDbCo nnection' cannot be converted to 'String'.
Value of type 'System.Data.OleDb.OleDbCo
try like belwo
conn = new OleDbConnection("Provider= Microsoft. ACE.OLEDB. 12.0;Data Source=C:\Image\Student.ac cdb;Persis t Security Info=False;")
conn = new OleDbConnection("Provider=
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
ASKER
I got the same error on the same line.
31: "cmd = new OleDbCommand(conn)"
31: "cmd = new OleDbCommand(conn)"
try like this
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCom mand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCom
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
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.ac cdb;Persis t Security Info=False;")
conn.Open()
Dim folder As String = "C:\Image\"
ListFolders(folder)
counter = counter + 1
cmd = new OleDbCommand()
cmd.Connection = conn
End Sub
conn = new OleDbConnection("Provider=
conn.Open()
Dim folder As String = "C:\Image\"
ListFolders(folder)
counter = counter + 1
cmd = new OleDbCommand()
cmd.Connection = conn
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you
Open in new window