Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Vb.net FlowLayoutPanel with MS Access: System.InvalidCastException error.

Vb.net FlowLayoutPanel with MS Access: System.InvalidCastException error.

Scheduled Pinned Locked Moved .NET (Core and Framework)
graphicshelpcsharpdatabase
7 Posts 4 Posters 53 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sdfsdfsdfewrew3feff
    wrote on last edited by
    #1

    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 OleDbDataReader

    Private 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

    Richard DeemingR D J 3 Replies Last reply
    0
    • S sdfsdfsdfewrew3feff

      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 OleDbDataReader

      Private 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

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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 the InvalidCastException on the rd.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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      S 1 Reply Last reply
      0
      • S sdfsdfsdfewrew3feff

        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 OleDbDataReader

        Private 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

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          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 the InvalidCastException on the rd.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

          S Offline
          S Offline
          sdfsdfsdfewrew3feff
          wrote on last edited by
          #4

          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.

          Richard DeemingR 1 Reply Last reply
          0
          • D Dave Kreskowiak

            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

            S Offline
            S Offline
            sdfsdfsdfewrew3feff
            wrote on last edited by
            #5

            Hi Dave Kreskowiak, i was forget and you me not. Long time ago i have download opensource projects. Welcome back Dave and how are you?

            1 Reply Last reply
            0
            • S sdfsdfsdfewrew3feff

              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.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              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

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              • S sdfsdfsdfewrew3feff

                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 OleDbDataReader

                Private 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

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups