Load Counter
motorcycle helmets

Monday 11 November 2013

How to Display Picture From Database to Picture Box Control

Here I've given a very simple code snippet with you can display image from database.


private void DisplayPictureFromDB()
{

SqlConnection cnMyPro = new SqlConnection();
 
string qryMyPro = "SELECT Description FROM ProductInfo";
if (cnMyPro.State != ConnectionState.Open)
cnMyPro.Open();

SqlCommand cmdMyPro = new SqlCommand(qryMyPro, cnMyPro);
SqlDataReader sdrMyPro =cmdMyPro.ExecuteReader();
sdrMyPro.Read();

if (sdrMyPro.HasRows)
{

byte[] MyImage = (byte[])(sdrMyPro[2]);
if (MyImage == null)
picDescription.Image = null;

else
{

MemoryStream msMyPro = new MemoryStream(MyImage);
picDescription.Image =Image.FromStream(msMyPro);
 
}
}
}

1 comment:

Unknown said...

Well Done. But This process will increase the weight of your DB to future maintenance. Better to use file repository as like How a mail server deal its attachments with the mail.