Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 13-Apr-11 21:39pm
v2
Comments
ahmadnawaz 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
m@dhu 14-Apr-11 4:28am    
Did you check the msdn link? There the image is displayed from directory only.
Dalek Dave 14-Apr-11 3:39am    
Edited for Grammar and Readability.

 
Share this answer
 
Comments
m@dhu 14-Apr-11 2:12am    
Oh you beat me to it.
m@dhu 14-Apr-11 2:12am    
My 5.
Dalek Dave 14-Apr-11 3:39am    
:) 5!
 
Share this answer
 
Comments
Dalek Dave 14-Apr-11 3:40am    
Gets a 5
m@dhu 14-Apr-11 3:41am    
Thanks DD.
Ali Al Omairi(Abu AlHassan) 14-Apr-11 4:16am    
over my head, sir;
i like te idea of the handler.

hundred of roses
try this Link

Click[^]
 
Share this answer
 
Abhijit Jana has write a good article on it
Let's see Here[^]
 
Share this answer
 
Comments
Dalek Dave 14-Apr-11 3:40am    
Nice Link
koolprasad2003 14-Apr-11 4:58am    
Thanks
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;
}
}

}
 
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