Platform: Windows 11 Pro x64, VS Studio 2019 Community x64, Mysql. I m trying to read a BLOB field into a byte array (.webp image to be later converted for PictureBox) . I ran into some issues with that. It compiles Ok but at run time it fails on the first rdr.GetBytes statement which I have marked in the code below:
The error triggered on the statement marked in code is: MySql.Data.MySqlClient.MySqlException: 'Invalid attempt to access a field before calling Read()' . I am afraid that I don't understand the cause of that error at all. I have used parametrized query, but I have temporarily removed it for sake of clarity. I searched on the web for a possible solution, but I have found that most of the examples are very similar or almost the same as what I am doing. Well, I don't doubt that the debugger is correct, but reading through the docs did not help me so far
code portion:
public void showImage(int rowNr)
{
string fileName = rowitems[rowNr].pictName;
int bufferSize = 1024;
byte[] picture = new byte[bufferSize];
long bytesRead, startIndex = 0;
MySqlConnection conn = new MySqlConnection(connStr);
MySqlCommand cmd = new MySqlCommand("SELECT PictureName FROM Categories WHERE PictureName='?s';", conn);
MySqlParameter picNameParam = new MySqlParameter("?picName", MySqlDbType.VarChar, 20);
picNameParam.Value = fileName;
cmd.Parameters.Add(picNameParam);
conn.Open();
MySqlDataReader rdr = cmd.ExecuteReader();
----> bytesRead = rdr.GetBytes(0, startIndex, picture, 0, bufferSize);
while (bytesRead == bufferSize)
{
startIndex += bufferSize;
bytesRead = rdr.GetBytes(0, startIndex, picture, 0, bufferSize);
}
// convert the picture byte array from a webp format to PictureBox bitmap
Imazen.WebP.SimpleDecoder decoder = new Imazen.WebP.SimpleDecoder();
var bitmap = decoder.DecodeFromBytes(picture, picture.Length);
pictureBox1.Image = bitmap;
}
The error triggered on the statement marked in code is: MySql.Data.MySqlClient.MySqlException: 'Invalid attempt to access a field before calling Read()' . I am afraid that I don't understand the cause of that error at all. I have used parametrized query, but I have temporarily removed it for sake of clarity. I searched on the web for a possible solution, but I have found that most of the examples are very similar or almost the same as what I am doing. Well, I don't doubt that the debugger is correct, but reading through the docs did not help me so far