Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
i want show images in grid view and also add in data my code is....

C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UploadImages
{
    public partial class WebForm1 : System.Web.UI.Page
    {
      
        SqlConnection conn = new SqlConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            conn.ConnectionString = "Data Source=DELL-PC;Initial Catalog=db;Integrated Security=True";
            Load_GridData(); // call method below
        }

        void Load_GridData()
        {
            conn.Open();  // open the connection 
            SqlDataAdapter Sqa = new SqlDataAdapter("select * from ImageToTable", 
            DataSet ds = new DataSet();
            Sqa.Fill(ds);   // fill the dataset 
            GridView1.DataSource = ds; // give data to GridView
            GridView1.DataBind();
            conn.Close();
        }




    }
}


My Xml Code

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UploadImages.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="name" HeaderText="name" />
                <asp:ImageField DataImageUrlField="myphoto" HeaderText="images">
                </asp:ImageField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
conn);

Plz answer me step by step my db is
SQL
myphoto	image	Checked
name	nvarchar(50)	Checked
id	nchar(10)	Unchecked
Posted
Updated 23-Apr-15 3:39am
v2
Comments
Herman<T>.Instance 23-Apr-15 9:42am    
Is Google banned from your country?
What is the problem or question?
Deepak Kanswal Sharma 23-Apr-15 10:44am    
Google banned in your country!! LOL!!

Add a new aspx page in your project like GetImg.aspx, that requires the imageID as a param.

Add a new function or directly in PageLoad
SqlConnection con = new SqlConnection(your_connection);
SqlCommand cmd = new SqlCommand("select Imatge from table where idImge ='" + paramIdImg + "'", con);

con.Open();
				
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);

if (reader.Read())
{
	Response.ContentType = "image/jpg";
	Response.Clear();
	Response.BufferOutput = true;

	SqlBinary sb = reader.GetSqlBinary(0);
	Response.BinaryWrite(sb.Value);

	Response.Flush();
}
		
reader.Close();
con.Close();


The column of your gridview must be something like this:

ASP.NET
<itemtemplate>>
<asp:image id="Image1" runat="server" backcolor="Transparent" height="75px" imageurl="<%# Eval("YourDbFIeld", "GetImg.aspx?paramImg={0}") %>" width="75px" xmlns:asp="#unknown" />
 
Share this answer
 
There's an article on this site:
Displaying Images from a Database in a GridView[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900