Click here to Skip to main content
Rate this: bad
good
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)
            {
                try
                {
                    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);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    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);
            conn.Open();
            Byte[] img = (Byte[])(cmd.ExecuteScalar());
            GridView2.DataSource = img;
            GridView2.DataBind();
            conn.Close();
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
good
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#[^]
  Permalink  
v2
Comments
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 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 85
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2014
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