Skip to content
Advertisement

How to display an Image on my page from binary code on my

This is the first time I’m doing this.

so I followed this tutorial: https://www.youtube.com/watch?v=YbiSrK4h1Kw

and that’s the code in my ashx.cs file currently-

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace KEY_web_v1
{
/// <summary>
/// Summary description for HandlerImage
/// </summary>
public class HandlerImage : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string connectionString = @"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UserskfirmDesktopKEY_web_v1KEY_web_v1App_DataDatabase1.mdf;Integrated Security=True";
        SqlConnection conn = new SqlConnection(connectionString);
        //SqlConnection conn = new SqlConnection("data source=.\sqlexpress; initial 
        catalog=SlideShow; integrated security=true");//ORIGINAL - "data source=.\sqlexpress;             
        initial catalog=SlideShow; integrated security=true" //I think he's just making a dataset, I can edit later if it doesnt work or add this to the global.asax
        SqlCommand comm = new SqlCommand();
        comm.Connection = conn;

        comm.CommandText = "SELECT * FROM Portfolio WHERE id=5"; //MAKE A LOOP!!!!!!!!!!!!!!!!!!!!
        SqlDataAdapter da = new SqlDataAdapter(comm);
        DataTable dt = new DataTable();

        da.Fill(dt);

        byte[] image = (byte[])dt.Rows.[0][3]; //MAKE A LOOP!!!!!!!!!!!!!!!!!!!!

        context.Response.ContentType = "image/jpeg";
        context.Response.ContentType = "image/jpg";
        context.Response.ContentType = "image/png";

        context.Response.BinaryWrite(image);
        context.Response.Flush();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
}

that’s my HTML code:

<asp:Image ID="Image1" runat="server" ImageUrl="~/HandlerImage.ashx" Width="200" Height="200" />

and that’s my DB table:

CREATE TABLE [dbo].[Portfolio] (
[Image]       NVARCHAR (50)   NULL,
[Description] NVARCHAR (50)   NULL,
[ImageData]   VARBINARY (MAX) NULL,
[id]          INT             IDENTITY (1, 1) NOT NULL,
PRIMARY KEY CLUSTERED ([id] ASC)
);

and that’s my table’s data:

enter image description here

I have 2 problems. one is that I get this error: enter image description here

and the second one is that there’s no image presented:

enter image description here

Advertisement

Answer

Remove the dot between Rows and [0], this will fix the syntax error.

You can set the ContentType of the response only once so in this example it will use the last value used with is PNG.

The image you have saved in the database is a JPG so it will probably not show correctly on the website as it expects a PNG. I recommend you store the image type (JPG, PNG, etc.) in the database as well.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement