First of all, i have 13 image buttons in a page and I defined their ids like image_1, image_2, image_3….
In my database, I hold image IDs and images like in photo.
All image buttons are empty in visual studio designer rn.
So here’s my code:
protected void PlaceImages()
{
string query = "SELECT imageID, image FROM images WHERE imageID BETWEEN 1 AND 13";
using (MySqlConnection connection = ConnectDB())
{
connection.Open();
using (MySqlCommand cmd = new MySqlCommand(query, connection))
{
using (MySqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
int imageID = Convert.ToInt32(dr["imageID"]);
byte[] imgData = (byte[])dr["image"];
string base64String = Convert.ToBase64String(imgData, 0, imgData.Length);
string controlID = "image_" + imageID;
ImageButton imgButton = (ImageButton)FindControl(controlID);
imgButton.ImageUrl = "data:image/png;base64," + base64String;
}
}
}
}
}
I get this error: imgButton was null.
in this row: imgButton.ImageUrl = “data:image/png;base64,” + base64String;
How can I solve this problem?
I tried to put this row “imgButton.ImageUrl = “data:image/png;base64,” + base64String;” in an if statement to check if its not null. But this time, nothing changed in my image buttons.