Asked by:
how to save and retrieve images from mysql database

Question
-
User1441157511 posted
i am having slight difficulty retrieving images from the database;
the following lines of code explain how i uploaded to d database
protected void process_Click(object sender, EventArgs e)
{
try
{byte[] passport1 = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile image1 = FileUpload1.PostedFile;
image1.InputStream.Read(passport1, 0, (int)FileUpload1.PostedFile.ContentLength);
int length1 = passport1.Length;MySqlCommand insert = new MySqlCommand("insert into usoft.admintable (Passport) values (@img1)", conn);
param[0] = new MySqlParameter("@img1", MySqlDbType.Blob, length1);
param[0].Value = image1;
insert.Parameters.Add(param[0]);
read = insert.ExecuteReader();
read.Close();}
catch (MySqlException mysqlex)
{
ClientScript.RegisterStartupScript(this.GetType(), "validation", "<script language='javascript'>alert('" + mysqlex.Message + "', \n\n '" + mysqlex.Source + "', \n\n '" + mysqlex.StackTrace + "')</script>");
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "validation", "<script language='javascript'>alert('" + ex.Message + "', \n\n '" + ex.Source + "', \n\n '" + ex.StackTrace + "')</script>");
}
}and i'm retrieving it using these lines of code
protected void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
MySqlConnection conn = new MySqlConnection(connection);
MySqlCommand select = new MySqlCommand("select Passport from usoft.admintable where Name = '" + name.selecteditem.ToString() + "'", conn);
MySqlDataReader read;
conn.Open();
read = select.ExecuteReader();
while (read.Read())
{
byte[] passportImage1 = (byte[])(read["Passport"]);
if (ListBox.SelectedIndex.Equals(0))
{
if (passportImage1 == null)
{
adminImage.ImageUrl = null;
}
else
{
MemoryStream ms = new MemoryStream(passportImage1);
adminImage.ImageUrl = "data:image/jpeg;base64," + Convert.ToString(passportImage1);
}
}
read.Close();
conn.Close();
}
catch (MySqlException mysqlex)
{
ClientScript.RegisterStartupScript(this.GetType(), "validation", "<script language='javascript'>alert('" + mysqlex.Message + "', \n\n '" + mysqlex.Source + "', \n\n '" + mysqlex.StackTrace + "')</script>");
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "validation", "<script language='javascript'>alert('" + ex.Message + "', \n\n '" + ex.Source + "', \n\n '" + ex.StackTrace + "')</script>");
}
}yet, the image doesn't display, instead, it displays the default error image icon. it is of the jpeg format and has a 25B size in the database. i need some assistance please
Tuesday, February 3, 2015 7:49 PM
All replies
-
User-271186128 posted
Hi EasyHero,<!--?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" /--><o:p></o:p>
As for this issue, because I didn’t install MySQL database in my machine, so I can’t test your code on my side. But, from your code, I suggest you could try to use Convert.ToBase64String() method to convert the passportImage1. The following code is used to display the binary image from SQL database, you could refer to it.<o:p></o:p>
byte[] bytes = (byte[])GetData("SELECT Data FROM tblFiles WHERE Id =" + id).Rows[0]["Data"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
Image1.ImageUrl = "data:image/png;base64," + base64String;For more details, please see: http://www.aspsnippets.com/Articles/Display-image-from-database-in-Image-control-without-using-Generic-Handler-in-ASPNet.aspx
Best Regards,
Dillion<o:p></o:p>Tuesday, February 3, 2015 10:19 PM -
User-1601169661 posted
Try this code for retrieveing image from database...
SqlCommand command = new SqlCommand("select img from imgtable where id=2", cnn);
byte[] image = (byte[])command.ExecuteScalar();
stream.Write(image, 0, image.Length);Source:
http://net-informations.com/csprj/dataset/cs-retrieve-image.htm
Insert image in a database source...
http://net-informations.com/csprj/dataset/cs-insert-image.htm
William
Thursday, February 5, 2015 3:06 AM