Vb.net FlowLayoutPanel with MS Access: System.InvalidCastException error.
-
This is my first time that i post here and i hope that topic is correct is placed. Please who can help me? Error message is:
An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll
Additional information:This rows give error (the code was writed for MySQL and i have self changed to MS Access):
Dim len As Long = rd.GetBytes(0, 0, Nothing, 0, 0) Dim array(CInt(len)) As Byte rd.GetBytes(0, 0, array, 0, CInt(len)) pic = New PictureBox Dim ms As New System.IO.MemoryStream(array) Dim bitmap As New System.Drawing.Bitmap(ms) pic.BackgroundImage = bitmap
Full Code:
Imports System.Data.OleDb
Public Class Form1
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim rd As OleDbDataReaderPrivate WithEvents pic As New PictureBox Private Sub Form1\_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Private Sub Button1\_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Dim con As OleDbConnection 'Dim cmd As OleDbCommand 'Dim rd As OleDbDataReader con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\database.accdb;") con.Open() cmd = New OleDbCommand("select \* from MyTable", con) rd = cmd.ExecuteReader MsgBox("connected") con.Close() MsgBox("disconnected") End Sub Sub LoadData() con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\database.accdb;") con.Open() cmd = New OleDbCommand("select \* from MyTable", con) rd = cmd.ExecuteReader While rd.Read Dim len As Long = rd.GetBytes(0, 0, Nothing, 0, 0) Dim array(CInt(len)) As Byte rd.GetBytes(0, 0, array, 0, CInt(len)) pic = New PictureBox Dim ms As New System.IO.MemoryStream(array) Dim bitmap As New System.Drawing.Bitmap(ms) pic.BackgroundImage = bitmap FlowLayoutPanel1.Controls.Add(pic) End While rd.Close() con.Close() End Sub Private Sub Button2\_Click(sender As Object, e As EventArgs) Handles Button2.Click LoadData() End Sub
End Class
-
This is my first time that i post here and i hope that topic is correct is placed. Please who can help me? Error message is:
An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll
Additional information:This rows give error (the code was writed for MySQL and i have self changed to MS Access):
Dim len As Long = rd.GetBytes(0, 0, Nothing, 0, 0) Dim array(CInt(len)) As Byte rd.GetBytes(0, 0, array, 0, CInt(len)) pic = New PictureBox Dim ms As New System.IO.MemoryStream(array) Dim bitmap As New System.Drawing.Bitmap(ms) pic.BackgroundImage = bitmap
Full Code:
Imports System.Data.OleDb
Public Class Form1
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim rd As OleDbDataReaderPrivate WithEvents pic As New PictureBox Private Sub Form1\_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Private Sub Button1\_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Dim con As OleDbConnection 'Dim cmd As OleDbCommand 'Dim rd As OleDbDataReader con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\database.accdb;") con.Open() cmd = New OleDbCommand("select \* from MyTable", con) rd = cmd.ExecuteReader MsgBox("connected") con.Close() MsgBox("disconnected") End Sub Sub LoadData() con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\database.accdb;") con.Open() cmd = New OleDbCommand("select \* from MyTable", con) rd = cmd.ExecuteReader While rd.Read Dim len As Long = rd.GetBytes(0, 0, Nothing, 0, 0) Dim array(CInt(len)) As Byte rd.GetBytes(0, 0, array, 0, CInt(len)) pic = New PictureBox Dim ms As New System.IO.MemoryStream(array) Dim bitmap As New System.Drawing.Bitmap(ms) pic.BackgroundImage = bitmap FlowLayoutPanel1.Controls.Add(pic) End While rd.Close() con.Close() End Sub Private Sub Button2\_Click(sender As Object, e As EventArgs) Handles Button2.Click LoadData() End Sub
End Class
sdfsdfsdfewrew3feff wrote:
This is my first time that i post here
And yet you've apparently been a member for 12½ years, and posted 22 messages. Did you mean this is your first post in the ".NET Core" forum? Or have you managed to grab someone else's account by mistake?
sdfsdfsdfewrew3feff wrote:
select * from MyTable
If you're only reading one column, then only select that one column. If you use
select * from ...
then you're subject to the whims of the database engine as to which order the columns are returned in. You're presumably getting theInvalidCastException
on therd.GetBytes
line, which suggests the first column being returned by your query is not your image column.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
This is my first time that i post here and i hope that topic is correct is placed. Please who can help me? Error message is:
An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll
Additional information:This rows give error (the code was writed for MySQL and i have self changed to MS Access):
Dim len As Long = rd.GetBytes(0, 0, Nothing, 0, 0) Dim array(CInt(len)) As Byte rd.GetBytes(0, 0, array, 0, CInt(len)) pic = New PictureBox Dim ms As New System.IO.MemoryStream(array) Dim bitmap As New System.Drawing.Bitmap(ms) pic.BackgroundImage = bitmap
Full Code:
Imports System.Data.OleDb
Public Class Form1
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim rd As OleDbDataReaderPrivate WithEvents pic As New PictureBox Private Sub Form1\_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Private Sub Button1\_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Dim con As OleDbConnection 'Dim cmd As OleDbCommand 'Dim rd As OleDbDataReader con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\database.accdb;") con.Open() cmd = New OleDbCommand("select \* from MyTable", con) rd = cmd.ExecuteReader MsgBox("connected") con.Close() MsgBox("disconnected") End Sub Sub LoadData() con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\database.accdb;") con.Open() cmd = New OleDbCommand("select \* from MyTable", con) rd = cmd.ExecuteReader While rd.Read Dim len As Long = rd.GetBytes(0, 0, Nothing, 0, 0) Dim array(CInt(len)) As Byte rd.GetBytes(0, 0, array, 0, CInt(len)) pic = New PictureBox Dim ms As New System.IO.MemoryStream(array) Dim bitmap As New System.Drawing.Bitmap(ms) pic.BackgroundImage = bitmap FlowLayoutPanel1.Controls.Add(pic) End While rd.Close() con.Close() End Sub Private Sub Button2\_Click(sender As Object, e As EventArgs) Handles Button2.Click LoadData() End Sub
End Class
sdfsdfsdfewrew3feff wrote:
This is my first time that i post here
No it's not! I remember you from a couple of exchanges about 8 years ago. You're FINALLY providing proper detail to actually help you this time! :)
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
sdfsdfsdfewrew3feff wrote:
This is my first time that i post here
And yet you've apparently been a member for 12½ years, and posted 22 messages. Did you mean this is your first post in the ".NET Core" forum? Or have you managed to grab someone else's account by mistake?
sdfsdfsdfewrew3feff wrote:
select * from MyTable
If you're only reading one column, then only select that one column. If you use
select * from ...
then you're subject to the whims of the database engine as to which order the columns are returned in. You're presumably getting theInvalidCastException
on therd.GetBytes
line, which suggests the first column being returned by your query is not your image column.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Sorry, Richard. I was forget and it long time ago. When i select a column than i get new error "From".
Additional information: The FROM clause contains a syntax error.
-
sdfsdfsdfewrew3feff wrote:
This is my first time that i post here
No it's not! I remember you from a couple of exchanges about 8 years ago. You're FINALLY providing proper detail to actually help you this time! :)
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakHi Dave Kreskowiak, i was forget and you me not. Long time ago i have download opensource projects. Welcome back Dave and how are you?
-
Sorry, Richard. I was forget and it long time ago. When i select a column than i get new error "From".
Additional information: The FROM clause contains a syntax error.
The only queries you've shown are
select * from MyTable
, which wouldn't produce that error. Perhaps your real table name is a reserved word?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
This is my first time that i post here and i hope that topic is correct is placed. Please who can help me? Error message is:
An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll
Additional information:This rows give error (the code was writed for MySQL and i have self changed to MS Access):
Dim len As Long = rd.GetBytes(0, 0, Nothing, 0, 0) Dim array(CInt(len)) As Byte rd.GetBytes(0, 0, array, 0, CInt(len)) pic = New PictureBox Dim ms As New System.IO.MemoryStream(array) Dim bitmap As New System.Drawing.Bitmap(ms) pic.BackgroundImage = bitmap
Full Code:
Imports System.Data.OleDb
Public Class Form1
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim rd As OleDbDataReaderPrivate WithEvents pic As New PictureBox Private Sub Form1\_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Private Sub Button1\_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Dim con As OleDbConnection 'Dim cmd As OleDbCommand 'Dim rd As OleDbDataReader con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\database.accdb;") con.Open() cmd = New OleDbCommand("select \* from MyTable", con) rd = cmd.ExecuteReader MsgBox("connected") con.Close() MsgBox("disconnected") End Sub Sub LoadData() con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\database.accdb;") con.Open() cmd = New OleDbCommand("select \* from MyTable", con) rd = cmd.ExecuteReader While rd.Read Dim len As Long = rd.GetBytes(0, 0, Nothing, 0, 0) Dim array(CInt(len)) As Byte rd.GetBytes(0, 0, array, 0, CInt(len)) pic = New PictureBox Dim ms As New System.IO.MemoryStream(array) Dim bitmap As New System.Drawing.Bitmap(ms) pic.BackgroundImage = bitmap FlowLayoutPanel1.Controls.Add(pic) End While rd.Close() con.Close() End Sub Private Sub Button2\_Click(sender As Object, e As EventArgs) Handles Button2.Click LoadData() End Sub
End Class
sdfsdfsdfewrew3feff wrote:
"select * from MyTable"
Using the asterisk means the database is free to return the columns (all columns) in any order that it wants. So if your table as more than one column then you have no idea at all what the first column actually is. Thus if you assume that the first column is of a certain type like a varchar but you use it like an int, and code it that way, then you will get errors. Very like a cast error. Your SQL should look like the following.
select col1, col2, col3 from MyTable
If you only want one column then of course only specify those.