Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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

1 solution

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#[^]
 
Share this answer
 
v2
Comments
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
fjdiewornncalwe 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900