Hi RichardSa,
Refer below sample.
Database
CREATE TABLE [dbo].[tblFiles](
[id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NOT NULL,
[ContentType] [nvarchar](200) NOT NULL,
[Data] [varbinary](max) NULL,
CONSTRAINT [PK_tblFiles] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
I have inserted few record in the table.
For more details on insert refer below article.
HTML
<asp:Repeater ID="rpttblFiles" runat="server" OnItemDataBound="OnItemDataBound">
<ItemTemplate>
<asp:TableRow>
<asp:TableCell>
<div class="col-md-5" style="max-width: 50%; height: auto;">
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' /><br />
<asp:Image ID="imgPhoto" runat="server" Height="100" Width="100" />
<hr />
</div>
</asp:TableCell>
</asp:TableRow>
</ItemTemplate>
</asp:Repeater>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tblFiles"))
{
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
rpttblFiles.DataSource = dt;
rpttblFiles.DataBind();
}
}
}
}
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Image img = e.Item.FindControl("imgPhoto") as Image;
DataRowView dr = (DataRowView)e.Item.DataItem;
if (!Convert.IsDBNull(dr["Data"]))
{
string imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["Data"]);
img.ImageUrl = imageUrl;
img.Visible = true;
}
else
{
img.Visible = false;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM tblFiles")
cmd.Connection = con
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
rpttblFiles.DataSource = dt
rpttblFiles.DataBind()
End Using
End Using
End Using
End If
End Sub
Protected Sub OnItemDataBound(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("imgPhoto"), Image)
Dim dr As DataRowView = CType(e.Item.DataItem, DataRowView)
If Not Convert.IsDBNull(dr("Data")) Then
Dim imageUrl As String = "data:image/jpg;base64," & Convert.ToBase64String(CType(dr("Data"), Byte()))
img.ImageUrl = imageUrl
img.Visible = True
Else
img.Visible = False
End If
End If
End Sub
Screenshot