Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 25637 |  Welcome, Guest!   Register  Login
 Home > Forums > ASP.NET > Displaying Images in GridView with simple code and without using Handler ...
Nkkppp

Displaying Images in GridView with simple code and without using Handler

Replies: 2 | Posted by: Nkkppp on 7/3/2012 | Category: ASP.NET Forums | Views: 2021 | Status: [Member] | Points: 10  Download source file 


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)
{








}

}


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

Sheonarayan
Sheonarayan  
Posted on: 7/3/2012 9:30:29 PM
Level: HonoraryPlatinum | Status: [Microsoft_MVP] [Administrator] | Points: 25

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, Microsoft MVP
The Founder
http://www.dotnetfunda.com

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

Nkkppp
Nkkppp  
Posted on: 7/3/2012 10:30:57 PM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Reply - Please login to reply


Click here to login & reply

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/20/2013 4:08:28 AM