You must Sign In to post a response.

How to Display Image in Gridview From database without using Handler file ?

How to Display Image in Gridview From database without using Handler file ?

Note: Image is Binary format on database


Comments

Author: NISHAD16 Apr 2012 Member Level: Silver   Points : 4


here given example is in vb code you can convert it as per your req.
First of all take image field from designing side and have to add column for image field as below:-

ShowHeader="False">



AlternateText='<%#DataBinder.Eval(Container.DataItem,"img_title") %>' />






---------------------------------------------------------------------

'THEN DO CODING IN LOAD EVENT OF CODING PAGE OF VB OR C# LIKE THESE



Public Sub displayImages()
Try
If btnSubmit.Text = "Verify" Then
str1 = "select Rno,img_title,img_type,img_stream from reg_master where Rname ='" + name + "'AND Remail = '" + txtEmail.Text + "'"
Else
str1 = "select Rno,img_title,img_type,img_stream from reg_master where Rname = " + Label19.Text
End If

cmd.CommandType = Data.CommandType.Text
cmd.CommandText = str1
cmd.Connection = con
Dim myAdapter As New OleDbDataAdapter(cmd)
Dim myDataSet As New DataSet
myAdapter.Fill(myDataSet)
GD1.DataSource = myDataSet
GD1.DataBind()


Catch ex As Exception
End Try
End Sub
----------------------------------FOR IMAGE URL------------------
Public Function imageURL(ByVal img_id) As String
Return ("retrieveImages.aspx?id=" & img_id)
End Function

Author: Ravindran16 Apr 2012 Member Level: Diamond   Points : 4

Without handler you can use dummy page to write image in gridview

i have table like this


create table stud([ID] [int] IDENTITY(1,1) ,sname varchar(50),simage image)


Default.aspx Client side
      




Enter Student Name


Select student image to be uploaded





DataKeyNames="sid" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onpageindexchanging="GridView1_PageIndexChanging">



<%#Eval("sname")%>








<%--using normal .aspx page convert bytes to image--%>


<%--using handler convert bytes to image--%>
<%----%>











>Delete






Default.aspx.cs code

using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
String sname, query;
byte[] filebyte = null;

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadGrid();
}
Button1_Click(this, new EventArgs());
}
void LoadGrid()
{
sqlcon.Open();
sqlcmd = new SqlCommand("select * from stud", sqlcon);
da = new SqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
GridView1.DataBind();
}
sqlcon.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
sname = TextBox1.Text;
filebyte = convertByte(FileUpload1);
sqlcon.Open();
query = "insert into stud(sname,simage) values(@sname,@simage)";
SqlCommand cmd = new SqlCommand(query, sqlcon);
cmd.Parameters.Add("@sname", SqlDbType.VarChar).Value = sname;
cmd.Parameters.Add("@simage", SqlDbType.Image).Value = filebyte;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
sqlcon.Close();
TextBox1.Text = "";
LoadGrid();
}
}
public byte[] convertByte(FileUpload FileControl)
{
Stream fs = default(Stream);
byte[] bytes1 = null;
byte[] postfile = null;
fs = FileControl.PostedFile.InputStream;
BinaryReader br1 = new BinaryReader(fs);
bytes1 = br1.ReadBytes(FileControl.PostedFile.ContentLength);
postfile = bytes1;
return postfile;
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String sid;
sid = GridView1.DataKeys[e.RowIndex].Value.ToString();
sqlcmd = new SqlCommand("delete from stud where sid='" + sid +"'", sqlcon);
sqlcon.Open();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
LoadGrid();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
LoadGrid();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
LoadGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string sid;
sid = GridView1.DataKeys[e.RowIndex].Value.ToString();

TextBox sname = (TextBox)row.FindControl("txtsname");
FileUpload simage = (FileUpload)row.FindControl("FileUpload2");

sqlcon.Open();
query = "update stud set sname=@sname,simage=@simage where sid=@sid";
SqlCommand cmd = new SqlCommand(query, sqlcon);
cmd.Parameters.Add("@sname", SqlDbType.VarChar).Value = sname.Text;
cmd.Parameters.Add("@simage", SqlDbType.Image).Value = convertByte(simage);
cmd.Parameters.Add("@sid", SqlDbType.Int).Value = Convert.ToInt32(sid);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
LoadGrid();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
LoadGrid();
}
}


Default3.aspx.cs

using System.Data.SqlClient;
using System.Data;
using System.IO;


public partial class Default3 : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
byte[] b = null;

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
int sid = Convert.ToInt32(Request.QueryString["ID"]);
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
SqlCommand sqlcmd = new SqlCommand("Select simage from stud where sid =@sid", sqlcon);
sqlcmd.Parameters.Add("@sid", SqlDbType.Int).Value = sid;
sqlcmd.CommandType = System.Data.CommandType.Text;
sqlcon.Open();
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
b = ((byte[])dt.Rows[0][0]);
MemoryStream ms = new MemoryStream(b);
Response.ContentType = "image/jpeg";
Response.BinaryWrite(b);
}
}
}
}


Further more detail on this refer my DNS resource
http://www.dotnetspider.com/resources/42953-How-show-uploaded-images-Grid-view.aspx