Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi,
 
I want to add an image to a gridview using a datatable in ASP.Net(C#).
 
How can I do this?
 
I want to code in .cs file not in .aspx file for adding image to the datatable and display it in gridview.
 
Any help will be appreciated.
Posted 13-Apr-11 21:06pm
Edited 13-Apr-11 22:39pm
v2
Comments
ahmadnawaz at 14-Apr-11 3:32am
   
but i am getting the image from a directory not from database.
How could i do this.
I tried the follwoing.
 

 
DataTable dt = new DataTable();
DataColumn col;
DataRow row;
 
col = new DataColumn();
col.ColumnName = "PictureURL";
dt.Columns.Add(col);
 

row = dt.NewRow();
System.Drawing.Image MyImage = System.Drawing.Image.FromFile("C:\\tickmark.png");
row["PictureURL"] = MyImage;
 

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

but it output the following
System.Drawing.Bitmap
mkgoud at 14-Apr-11 4:28am
   
Did you check the msdn link? There the image is displayed from directory only.
Dalek Dave at 14-Apr-11 3:39am
   
Edited for Grammar and Readability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
mkgoud at 14-Apr-11 2:12am
   
Oh you beat me to it.
mkgoud at 14-Apr-11 2:12am
   
My 5.
Dalek Dave at 14-Apr-11 3:39am
   
:) 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Dalek Dave at 14-Apr-11 3:40am
   
Gets a 5
mkgoud at 14-Apr-11 3:41am
   
Thanks DD.
Ali Al Omairi(Abu AlHassan) at 14-Apr-11 4:16am
   
over my head, sir;
i like te idea of the handler.
 
hundred of roses
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

try this Link
 
Click[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Abhijit Jana has write a good article on it
Let's see Here[^]
  Permalink  
Comments
Dalek Dave at 14-Apr-11 3:40am
   
Nice Link
koolprasad2003 at 14-Apr-11 4:58am
   
Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=ABC-PC\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True");

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{

try
{
FileUpload img = (FileUpload)imgUpload;
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)

{
//To create a PostedFile
HttpPostedFile File = imgUpload.PostedFile;
//Create byte Array with file len
imgByte = new Byte[File.ContentLength];
//force the control to load data in array
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
// Insert the employee name and image into db

con.Open();
string sql = "INSERT INTO EmpDetails(empname,empimg) VALUES(@enm, @eimg) SELECT @@IDENTITY";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@enm", txtEName.Text.Trim());
cmd.Parameters.AddWithValue("@eimg", imgByte);
int id = Convert.ToInt32(cmd.ExecuteScalar());
lblResult.Text = String.Format("Employee ID is {0}", id);

// Display the image from the database
Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;
}
catch
{
lblResult.Text = "There was an error";
}
finally
{
con.Close();
}

}
protected void viewall_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=ABC-PC\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True");
string sql = "select * from Empdetails";
con.Open();

DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();


}and here is my generic handeler showimage :-using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Int32 empno;
if (context.Request.QueryString["id"] != null)
empno = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");

context.Response.ContentType = "image/jpeg";
Stream strm = ShowEmpImage(empno);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);

while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
//context.Response.BinaryWrite(buffer);
}

public Stream ShowEmpImage(int empno)
{
SqlConnection con = new SqlConnection("Data Source=ABC-PC\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True");
string sql = "SELECT empimg FROM EmpDetails WHERE empid = @ID";
SqlCommand cmd = new SqlCommand(sql,con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", empno);
con.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
con.Close();
}
}

public bool IsReusable
{
get
{
return false;
}
}and here is the coding part of my generic handler 2 for displaying the images on grid view :- <%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context)
{
SqlConnection con = new SqlConnection("Data Source=ABC-PC\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select empid,Empname,Empimg from Empdetails where empid = @id";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;

SqlParameter imageID = new SqlParameter("@id", System.Data.SqlDbType.Int);
imageID.Value = context.Request.QueryString["id"];
cmd.Parameters.Add(imageID);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
context.Response.BinaryWrite((byte[])sdr["Image"];
sdr.close();
con.Close();

//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
}

public bool IsReusable
{
get
{
return false;
}
}

}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 285
1 DamithSL 265
2 CPallini 195
3 Maciej Los 175
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 9 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100