Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The code is fully correct. When I click debug, the result shown in error message says:
The problem is:
ERROR
'Invalid attempt to GetBytes on column 'class' 
The GetBytes function can only be used on columns of type Text, NText, or Image.'


What I have tried:

VB
Con.Open()
       Dim cmd As New SqlCommand_
       ("select class,name,price,imge from pos_restuarant", Con)
       dr = cmd.ExecuteReader
       FlowLayoutPanel1.AutoScroll = True
       FlowLayoutPanel1.Controls.Clear()

       While dr.Read

           Dim len As Long = dr.GetBytes(0, 0, Nothing, 0, 0)

           Dim array(CInt(len)) As Byte
           dr.GetBytes(0, 0, array, 0, CInt(len))

           pic = New PictureBox
           pic.Width = 120
           pic.Height = 150
           pic.BackgroundImageLayout = ImageLayout.Stretch
           pic.Tag = dr.Item("class").ToString

           lblname = New Label
           lblname.ForeColor = Color.White
           lblname.BackColor = Color.DodgerBlue
           lblname.TextAlign = ContentAlignment.MiddleCenter
           lblname.Dock = DockStyle.Top
           lblname.Font = New Font("Segoe UI", 8, FontStyle.Bold)
           lblname.Tag = dr.Item("class").ToString

           lblprice = New Label
           lblprice.ForeColor = Color.White
           lblprice.BackColor = Color.DarkOrange
           lblprice.Dock = DockStyle.Bottom
           lblprice.TextAlign = ContentAlignment.MiddleCenter
           lblprice.AutoSize = False
           lblprice.Font = New Font("Segoe UI", 12, FontStyle.Bold)
           lblprice.Tag = dr.Item("class").ToString

           Dim ms As New System.IO.MemoryStream(array)
           Dim bitmap As New System.Drawing.Bitmap(ms)
           pic.BackgroundImage = bitmap
           lblname.Text = dr.Item("name").ToString
           lblprice.Text = dr.Item("price").ToString

           pic.Controls.Add(lblname)
           pic.Controls.Add(lblprice)
           FlowLayoutPanel1.Controls.Add(pic)

           AddHandler pic.Click, AddressOf Selectimg_Click
           AddHandler lblname.Click, AddressOf Selectimg_Click
           AddHandler lblprice.Click, AddressOf Selectimg_Click
       End While
       dr.Dispose()
       Con.Close()
Posted
Updated 14-Sep-23 8:27am
v5
Comments
Member 15627495 19-Aug-23 11:23am    
in your loop; just keep dr.item.... as dynamic statements.

and use a 'template' for each Controls you will use.

           model_pic = New PictureBox
           model_pic.Width = 120
           model_pic.Height = 150
           model_pic.BackgroundImageLayout = ImageLayout.Stretch

           model_lbl = new  Label
           model_lbl.ForeColor = Color.White
           model_lbl.BackColor = Color.DodgerBlue
           model_lbl.TextAlign = ContentAlignment.MiddleCenter
           model_lbl.Dock = DockStyle.Top
           model_lbl.Font = New Font("Segoe UI", 8, FontStyle.Bold)
           

While dr.Read

           Dim len As Long = dr.GetBytes(0, 0, Nothing, 0, 0)

           Dim array(CInt(len)) As Byte
           dr.GetBytes(0, 0, array, 0, CInt(len))

           pic = model_pic
           pic.Tag = dr.Item("class").ToString

           lblname = model_lbl
           lblname.Tag = dr.Item("class").ToString
// same for all purposes, there are constants/static values, keep them before the loop.
// .....

           AddHandler pic.Click, AddressOf Selectimg_Click
           AddHandler lblname.Click, AddressOf Selectimg_Click
           AddHandler lblprice.Click, AddressOf Selectimg_Click
       End While
 

The error message is pretty explicit:
Error
Invalid attempt to GetBytes on column 'class'. The GetBytes function can only be used on columns of type Text, NText, or Image.

So look at your DB definition, and find out what type the class column actually is.
 
Share this answer
 
This is not hard to understand. You told GetBytes to return the size of the first column in the data, which is NOT a text or image column. You have to tell it which column, starting from 0, contains the image data.

Looking at the SELECT statement you wrote, the image data is in column 3, not 0. So, you GetBytes call would look like this:
VB.NET
Dim len As Long = dr.GetBytes(3, 0, Nothing, 0, 0)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900