MySql.Data.MySqlClient.MySqlException: 'GetBytes can only be called on binary or guid columns'

142 Views Asked by At

I'm developing an app in C# with .NET and MySQL database. I need to be able to insert and retrieve images in and out of the database and I have a column named 'Image' of type LONGBLOB for that purpose. The insertion goes well but when I try to retrieve the blob the following error pops up:

GetBytes() can only be called on binary or GUID columns

I'm using the following code to select from the database:

con.Open();

        string query = "select `Image`, `Name`, `Type`, `Price`, `Description` from `product`";
        MySqlCommand cmd = new MySqlCommand(query, con);

        MySqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            long len = reader.GetBytes(1,0,null,0,0);
            byte[] array = new byte[len + 1];
            reader.GetBytes(0, 0, null, 0, 0);
            PictureBox pic = new PictureBox();
            pic.Width = 100;
            pic.Height = 100;
            pic.BackgroundImageLayout = ImageLayout.Stretch;


            flowLayoutPanel2.Controls.Add(pic);
        }
        reader.Close();
        con.Close();

Despite changing the column type into binary and varbinary, I still got the same error.

Does anyone know what I'm doing wrong here?

0

There are 0 best solutions below