How to retrieve images from database table in ASP.NET

December 04, 2014 0 Comments

Database is a storage system, through which we can store data permanently into the secondary storage device. Similar to programming languages, database also take some data types for categorize data. like int, Text, DateTime etc. Now, we will discussed on image. In this example we will retrieve images from database. First we store images into database, actually images is not stored directly, so for that path of the images is stored into the database. Now, bind the GridView with the image field.  

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" Width="280px">
                <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                <asp:ImageField DataImageUrlField="imagepath" HeaderText="Image">
                    <ControlStyle Height="200px" Width="200px" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [imagetable]"></asp:SqlDataSource>
Code Generate the following output

How to retrieve images from database table in ASP.NET

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google