Displaying Images in GridView with simple code and without using Handler

Posted by Nkkppp under ASP.NET on 7/3/2012 | Points: 10 | Views : 4078 | Status : [Member] | Replies : 2
Can you please help me with my requirement

i have created a table Student with the colums sid INT,sname VARCHAR(50),photo which is of varbinary(max) type.

i saved the sid,sname,photo(in binary format) from front end (design page screenshot is attached)

In order to save the photo in binary format i have used very simple code

byte [] data= FileUpload1.FileBytes;
dr[2]=data;

_________________________When i click on Gridview button on design the data from database has to be retrieved...but the main problem is how to convert the binary data to Image so that it is displayed in Gridview

SqlConnection cn;
SqlDataAdapter da;
DataSet ds;
SqlCommandBuilder cmd;
byte[] data;

protected void Page_Load(object sender, EventArgs e)
{
cn = new SqlConnection("server=PRATHAPG\\SQLEXPRESS;database=Prathap;trusted_connection=true");
da = new SqlDataAdapter("select * from student", cn);
cmd = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "stu");
}

protected void btnload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
data = FileUpload1.FileBytes;
DataRow dr = ds.Tables["stu"].NewRow();
dr[0] = txtid.Text;
dr[1] = txtname.Text;
dr[2] = data;
ds.Tables["stu"].Rows.Add(dr);
da.Update(ds, "stu");

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








}

}




Responses

Posted by: Sheonarayan on: 7/3/2012 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Read this article that has the exact solution of your problem http://www.dotnetfunda.com/articles/article1084-saving-images-into-the-database-in-aspnet-and-displaying-to-the-gridview-.aspx.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nkkppp on: 7/3/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Narayan,

Thanks for the reply.But i had mentioned that the code has to be written without using handler and further i would like to change the code so that when i click on the Image file in gridview it has to open a download dialog box (without using handler)




Regards,
Prathap.

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response