I've managed to retrieve my image from database using imagehandler, but the problem is, it uses the first image retrieve only, instead of showing different image on different data.
So this is my image handler code
i have problem with this line (string volunteerNames = imageRequest.QueryString["VolunteerName"];
) where it shows only System.Byte[]
instead of showing the names
<%@ WebHandler Language="C#" Class=".ShowImage" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpRequest imageRequest = context.Request;
HttpResponse imageResponse = context.Response;
//string connStr = ConfigurationManager.ConnectionStrings["DBconnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Users\\User\\Desktop\\ProjectAV2.9\\ProjectA\\ProjectA\\App_Data\\ProjectA.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True";
string queryStr = "SELECT * FROM TVolunteer WHERE VolunteerName = @Name";
string bQueryStr = "SELECT * FROM BVolunteer WHERE VolunteerName = @Name";
string volunteerNames = imageRequest.QueryString["VolunteerName"];
SqlCommand bCMD = new SqlCommand(bQueryStr, conn);
SqlCommand cmd = new SqlCommand(queryStr, conn);
bCMD.Parameters.AddWithValue("@Name", volunteerNames);
cmd.Parameters.AddWithValue("@Name", volunteerNames);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
string volunteerNRIC = dr["VolunteerNRIC"].ToString();
byte[] volunteerProfilePicture = (byte[])dr["VolunteerProfilePicture"];
string volunteerName = dr["VolunteerName"].ToString();
string volunteerContact = dr["VolunteerContact"].ToString();
string volunteerAddress = dr["VolunteerAddress"].ToString();
string volunteerEmail = dr["VolunteerEmail"].ToString();
imageResponse.BinaryWrite(volunteerProfilePicture);
}
dr.Close();
SqlDataReader bDR = bCMD.ExecuteReader();
if (bDR.HasRows)
{
bDR.Read();
byte[] volunteerProfilePicture = (byte[])bDR["VolunteerProfilePicture"];
string volunteerName = bDR["VolunteerName"].ToString();
string volunteerContact = bDR["VolunteerContact"].ToString();
string volunteerAddress = bDR["VolunteerAddress"].ToString();
string volunteerEmail = bDR["VolunteerEmail"].ToString();
imageResponse.BinaryWrite(volunteerProfilePicture);
bDR.Close();
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
and here is my DataList
<asp:DataList ID="dl_tvolunteer" runat="server" CellPadding="3"
ForeColor="#333333" RepeatColumns="3"
RepeatDirection="Horizontal" Width="375px" Height="193px" GridLines="Both">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "VolunteerProfilePicture","~/ShowImage.ashx?VolunteerName="+ Eval("VolunteerName") %>' />
<br />
<asp:Label ID="lbl_Name" runat="server" Text='<%# Bind("VolunteerName") %>'></asp:Label>
<br />
<asp:Label ID="lbl_Contact" runat="server" Text='<%# Bind("VolunteerContact") %>'></asp:Label>
<br />
<asp:Label ID="lbl_Address" runat="server" Text='<%# Bind("VolunteerAddress") %>'></asp:Label>
<br />
<asp:Label ID="lbl_Email" runat="server" Text='<%# Bind("VolunteerEmail") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>