Click here to Skip to main content
13,045,631 members (66,895 online)
Rate this:
Please Sign up or sign in to vote.
I am having a web application. In which i have uploaded some images in my database, now i want to show that table in my gridview with all the images not a single image.
I have googled it so much but did not find the right answer.

My table design is
create table pictures 
pic image,
name varchar(50)
I am saving my images like this.
 protected void Button2_Click(object sender, EventArgs e)
            if (FileUpload2.HasFile)
                    string filename = FileUpload2.FileName;
                    string path = "~\\uploads\\" + filename.Trim();
                    FileUpload2.PostedFile.SaveAs(Server.MapPath("~\\uploads\\" + filename.Trim()));
                    SqlConnection conn = new SqlConnection(connstr);
                    SqlCommand cmd = new SqlCommand("insert into pictures values('" + path + "', '" + filename + "')", conn);
                    Label1.Text = "file uploaded successfully";
But now i want to retrieve these images in a gridview from database.

SqlConnection conn = new SqlConnection(connstr);
            SqlCommand cmd = new SqlCommand("select pic from picture", conn);
            Byte[] img = (Byte[])(cmd.ExecuteScalar());
            GridView2.DataSource = img;

I have used image field, item template but could not succeed.

Please help me.
Thanx in advance
Rakesh Sharma
Posted 16-Oct-12 7:10am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The datasource for a GridView needs to be a DataTable because a GridView is made up of Rows and Columns.
You are attempting to use a byte[]. The GridView has no idea what to do with this.
When you read an image from the database, do it to a Bitmap or Image class and then assign that Image to a Control that accepts an image.

Try looking at this article for some more information: Previewing Image in ASP.NET Image Control using C#[^]
kanha.460 16-Oct-12 13:21pm
I have used dataset and datareader but when it shows the gridview it shows some system.byte data. I bound the gridview with the help of wizard but did not work. So i moved to this
Marcus Kramer 16-Oct-12 13:48pm
This is just plain wrong. Look at my answer again. The byte data of an image is NOT a datatable and cannot magically be converted into a data table. The GridView has NO IDEA what to do with the data you are giving it. Replace the GridView with an image control and then bind it to the image source.
kanha.460 17-Oct-12 2:24am
If i replace grid view with the image control then i can show only single image in the control and i want to show all the images in the grid view.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2017
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