1

I'm using DataList with SqlDataSource which everything is preset. Picture is successful stored into database(Image is stored into Database in byte), in TestQuery the image is viewable and all title and description is showing up in the web page just the image didn't show up.

Picture Here

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource" RepeatColumns="3" RepeatDirection="Horizontal">
    <ItemTemplate>
        <asp:Image ID="Image1" runat="server" Height="200px" ImageUrl='<%# Eval("image") %>' Width="200px" />
        <br />
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("title") %>'></asp:Label>
        &nbsp;<br />
        <asp:Label ID="Label2" runat="server" Text='<%# Eval("desc") %>'></asp:Label>
        <br />
    </ItemTemplate>
</asp:DataList>


<asp:SqlDataSource ID="SqlDataSource" 
                   runat="server" 
                   ConnectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\SnorDB.mdf;
                   Integrated Security=True;
                   MultipleActiveResultSets=True;
                   Application Name="EntityFramework" 
                   ProviderName="System.Data.SqlClient" 
                   SelectCommand="SELECT [image], [title], [desc] FROM [Art]">
</asp:SqlDataSource>
41 72 6c
  • 1,600
  • 5
  • 19
  • 30
SnorSnor
  • 21
  • 5
  • Create a HttpHandler to stream back the image with a DataReader. – IrishChieftain Jul 06 '19 at 18:16
  • You need to convert the byte array to image. See how to do that with JavaScript here https://stackoverflow.com/questions/20756042/javascript-how-to-display-image-from-byte-array-using-javascript-or-servlet – Bosco Jul 06 '19 at 19:02

1 Answers1

1

Create Generic Handler

public void ProcessRequest(HttpContext context)
{
    string imageid = context.Request.QueryString["id"].ToString();
    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SnorDB"].ConnectionString);
    connection.Open();
    SqlCommand command = new SqlCommand("select image from [Art] where imageid=" + imageid, connection);
    SqlDataReader dr = command.ExecuteReader();
    dr.Read();
    context.Response.BinaryWrite((Byte[])dr[0]);
    connection.Close();
    context.Response.End();

}

public bool IsReusable
{
    get
    {
        return false;
    }
}

At the display webpage

ImageUrl='<%# "ImageHandler.ashx?id="+ Eval("imageid")

by passing the id to the handler.

Reference: Here

SnorSnor
  • 21
  • 5