Hi guys,
i am populating a datalist with data from an SQL Server database, in the database images are stored in BLOBS. i am getting these images out using a handler.
im populating the datalist with data from a table which contains the image and other attributes, which has all worked fine. however as the image field can be null, whenever an image is not present a broken image icon is displayed where an image would normally be displayed.
im wondering if there is a way to either remove the broken image icon, or possibly insert some code somewhere to display a default image if no image is present. any ideas would be most welcome :)
below is the code i am currently using
handler to get image
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Collections.Specialized;
public class Handler : IHttpHandler {
public string GetConnectionString()
{ //Gets Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
}
public void ProcessRequest(HttpContext context)
{ //gets query string for offer id
string id = context.Request.QueryString["carID"];
if (id != null)
{
try
{
a
MemoryStream memoryStream = new MemoryStream();
SqlConnection connection = new SqlConnection(GetConnectionString());
string sql = "SELECT Image FROM Car WHERE CarID = @CarID";
//creates new sql command
SqlCommand cmd = new SqlCommand(sql, connection);
//add paramaters to command
cmd.Parameters.AddWithValue("@CarID", id);
//open connection
connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
//byte arr
byte[] file = (byte[])reader["Image"];
reader.Close();
connection.Close();
memoryStream.Write(file, 0, file.Length);
context.Response.Buffer = true;
context.Response.BinaryWrite(file);
memoryStream.Dispose();
}
catch
{
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
datalist code
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal"
DataKeyField="OfferID" RepeatColumns="1"
Width="580px">
<ItemTemplate>
<center>
<%#Eval("CarName") %>
<br/>
<asp:Image ID="Image" Height="100" Width ="100" runat="server" ImageUrl='<%# "CarImageHandler.ashx?CarID=" + Eval("CarID") %>' />
<br/>
<br /><br /><br />
<center />
</ItemTemplate>
the data is bound with this
string sqlStatement = "SELECT * FROM Cars";
try
{
DataList1.DataSource = DAL.BindTable(sqlStatement);
DataList1.DataBind();
}
catch
{
}
thanks guys :) any help would be amazing