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
<div class="container-fluid">
<div class="row">
<asp:Repeater ID="RepDetails" runat="server" OnItemDataBound="RepDetails_ItemDataBound">
<ItemTemplate>
<asp:TableRow>
<asp:TableCell>
<div class="col-md-5" style="max-width: 100%; height: auto;">
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' /><br />
<asp:Image ID="Image1" runat="server" Height="100%" Width="100%" />
<hr />
</div>
</asp:TableCell>
</asp:TableRow>
</ItemTemplate>
</asp:Repeater>
</div>
Namespaces
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 * 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);
RepDetails.DataSource = dt;
RepDetails.DataBind();
}
}
}
}
protected void RepDetails_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;
if (!Convert.IsDBNull(dr["Data"]))
{
img.Visible = true;
img.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["Data"]);
}
else
{
img.Visible = false;
}
}
}
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 * 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)
RepDetails.DataSource = dt
RepDetails.DataBind()
End Using
End Using
End If
End Sub
Protected Sub RepDetails_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)
If Not Convert.IsDBNull(dr("Data")) Then
img.Visible = True
img.ImageUrl = "data:image/jpg;base64," & Convert.ToBase64String(CType(dr("Data"), Byte()))
Else
img.Visible = False
End If
End If
End Sub
Screenshot