How to bind image in gridview using database in asp.net c#

In this article, I will explain easiest way to bind the image in gridview. in the previous article , how to save image in database in asp.net C#

Database:-




Source code:-

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="image_name" HeaderText="Image Name" />
            <asp:ImageField DataImageUrlField="image_path" HeaderText="Image">
            </asp:ImageField>

        </Columns>
    </asp:GridView>
    </form>
</body>
</html>


Code behind (c#)
  using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class gridviewimage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678");


        SqlCommand cmd = new SqlCommand("SELECT * FROM image", con);
        SqlDataAdapter sda = new SqlDataAdapter();

        cmd.Connection = con;
        sda.SelectCommand = cmd;
        DataTable dt = new DataTable();

        sda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
  
}




Out-Put:-

How to bind image in gridview using database in asp.net c# How to bind image in gridview using database in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 5:22:00 PM Rating: 5

No comments:

Powered by Blogger.