Click here to Skip to main content
15,897,334 members

How to retrieve image from database

kanha.460 asked:

Open original thread
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
Tags: C#, ASP.NET, SQL Server

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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