Hi akhter,
You need to use Repeater OnItemDataBound event to bind the binary image.
Check this example. Now please take its reference and correct your code.
Database
I have made use of the following table tblFiles with the schema as follows.
I have already inserted few record in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:Repeater ID="rptImages" runat="server" OnItemDataBound="rptImages_ItemDataBound">
<HeaderTemplate>
<table>
<thead>
<tr>
<th>Name</th>
<th>Photo</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("Name") %></td>
<td><asp:Image ID="Image1" runat="server" Height="100" Width="100" Style="border: 5px solid black" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Namesaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT TOP 3 * FROM tblFiles WHERE ContentType = 'image/jpeg'";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
rptImages.DataSource = dt;
rptImages.DataBind();
}
}
}
}
protected void rptImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Image img = e.Item.FindControl("Image1") as Image;
DataRowView dr = (DataRowView)e.Item.DataItem;
string imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["Data"]);
img.ImageUrl = imageUrl;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT TOP 3 * FROM tblFiles WHERE ContentType = 'image/jpeg'"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
rptImages.DataSource = dt
rptImages.DataBind()
End Using
End Using
End If
End Sub
Protected Sub rptImages_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim img As Image = TryCast(e.Item.FindControl("Image1"), Image)
Dim dr As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim imageUrl As String = "data:image/jpg;base64," & Convert.ToBase64String(CType(dr("Data"), Byte()))
img.ImageUrl = imageUrl
End If
End Sub
Screenshot