Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
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 8: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 at 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 at 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 at 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
0 OriginalGriff 455
1 Sergey Alexandrovich Kryukov 312
2 DamithSL 179
3 King Fisher 179
4 Peter Leow 154
0 OriginalGriff 240
1 Maciej Los 95
2 Sascha Lefévre 85
3 King Fisher 70
4 F-ES Sitecore 55

Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2015
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