I am developing a program in C # with .NET and MySQL databases. I need the user image to be visible once the user is logged in, for which I have a LONGBLOB type column called 'Image'. The login process was good, but the following error occurred
Connection must be valid and open.
I'm using the following code to select from the database:
MySqlCommand command1 = new MySqlCommand("select `user Image` from `userdb` where `username` = @img", con);
command1.Parameters.Add("@img", MySqlDbType.Blob);
command1.Parameters["@img"].Value = Admin;
byte[] img = (byte[])command1.ExecuteScalar();
if (img == null)
{
pic_profilePic.Image = null;
}
else
{
var DATA = (byte[])command1.ExecuteScalar();
var stream = new MemoryStream(DATA);
pic_profilePic.Image = Image.FromStream(stream);
}
Despite changing the column type into binary and varbinary, I still got the same error.
Does anyone know what I'm doing wrong here?