0

How to an image which is retrieved from MYSQL database.

To display a text in textbox we use textbox.TEXT --> We use .TEXT to display text

Likewise how to display an Image retrieved from MYSQL database??

Image1.[___] = dr[0].[_____];

What to be filled in the above blanks??...

I used blob to store image.

1
  • you need to have an extra page or handler/action that gets the image. image1.src would be then something like /Home/Image?id=1001 and in the HomeController a method Image(int id) and a binary ActionResult. In that method you retrieve the image a sshown in this link: codeproject.com/KB/aspnet/image_asp.aspx Commented Feb 25, 2011 at 21:33

2 Answers 2

2

You can do it by creating a controller to serve up your image like and then in your view you can just add the call inline:

<img src='<%= Url.Action( "GetImage", "Image", new { id = yourImageIdHere } ) %>' /> 

public class ImageController
{
    public ActionResult GetImage(int id)
    {
        var image = GetTheRawBinaryImageFromTheDB();
        return File(image, "image/jpg" );
    }
 } 
Sign up to request clarification or add additional context in comments.

Comments

1

Add a Generic Handler to your Web Forms application and in the ProcessRequest method, you need to query the database for the relevant file/binary data. You would pick the right image via a querystring, usually. The following code shows how you can query the database and return the binary data:

using (DbConnection conn = new DbConnection(connect))
{
  if (context.Request.QueryString["id"] != null)
  {
    DbCommand cmd = new DbCommand(qry, conn);
    cmd.Parameters.AddWithValue("", context.Request.QueryString["id"]);
    conn.Open();
    using (DbDataReader rdr = cmd.ExecuteReader())
    {
      if (rdr.HasRows)
      {
        rdr.Read();
        context.Response.AddHeader("content-disposition", "attachment; filename=" + rdr["FileName"]);
        context.Response.ContentType = rdr["MimeType"].ToString();
        context.Response.BinaryWrite((byte[])rdr["Blob"]);
      }
    }
  }
}

You need to change the DbConnection, DbCommand and DbDataReader to the type that your provider likes (Odbc or MySql) and then point the ImageUrl property of your Image control to the HttpHandler:

Image1.ImageUrl = "MyHandler.ashx?id=" + whatever the image id is.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.