Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9319215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:19:38+00:00 2026-06-19T03:19:38+00:00

How to retrieve images from sql database in asp.net using c#. i want to

  • 0

How to retrieve images from sql database in asp.net using c#.

i want to retrieve the image file from database and then display the image in a tag.

i try this code but it is not working

aspx

 <asp:Image ID="Image1" runat="server" ImageUrl="" Height="150px" Width="165px" />

code behind

 Byte[] bytes = (Byte[])ds.Tables[0].Rows[0]["image"];
 Response.Buffer = true;
 Response.Charset = "";
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
 Response.ContentType = "image/jpg";
 Response.BinaryWrite(bytes);
 Response.Flush();
 Response.End();

How to give link to the ImageUrl="" of this image???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:19:39+00:00Added an answer on June 19, 2026 at 3:19 am

    Create a generic http handler as follows

    using System;
    using System.Configuration;
    using System.Web;
    using System.IO;
    using System.Data;
    using System.Data.SqlClient;
    
    public class ShowImage : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
           Int32 empno;
           if (context.Request.QueryString["id"] != null)
              empno = Convert.ToInt32(context.Request.QueryString["id"]);
           else
              throw new ArgumentException("No parameter specified");
    
           context.Response.ContentType = "image/jpeg";
           Stream strm = ShowEmpImage(empno);
           byte[] buffer = new byte[4096];
           int byteSeq = strm.Read(buffer, 0, 4096);
    
           while (byteSeq > 0)
           {
               context.Response.OutputStream.Write(buffer, 0, byteSeq);
               byteSeq = strm.Read(buffer, 0, 4096);
           }       
           //context.Response.BinaryWrite(buffer);
        }
    
        public Stream ShowEmpImage(int empno)
        {
             string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString;
             SqlConnection connection = new SqlConnection(conn);
             string sql = "SELECT empimg FROM EmpDetails WHERE empid = @ID";
             SqlCommand cmd = new SqlCommand(sql,connection);
             cmd.CommandType = CommandType.Text;
             cmd.Parameters.AddWithValue("@ID", empno);
             connection.Open();
             object img = cmd.ExecuteScalar();
             try
            {
                return new MemoryStream((byte[])img);
            }
            catch
            {
                return null;
            }
            finally
           {
                connection.Close();
           }
        }
    
        public bool IsReusable
        {
            get
            {
                 return false;
            }
        }
    
    
    }
    

    and display image as follow

     Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;
    

    There are some links below
    Showing image in GridView from the database?
    How to show a image in database in the image control of Asp.net?
    Display image from database in ASP.net with C#
    http://www.dotnetcurry.com/ShowArticle.aspx?ID=129

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to retrieve Binary Data from SQL Database with asp.net .but the data
I want to store and retrieve images to/from database using java beans, which data
How can I save and retrieve an image from a SQL Server database in
I successfully stored image files in MongoDB using PHP, when i retrieve images from
Using PHP and SQLSRV Driver I have to display binary images from an SQL
I'm trying to retrieve images from a folder using glob() and i want it
I want to fetch blob datatype of images from MySQL database using PHP and
Possible Duplicate: How to retrieve and display images from a database in a JSP
If I can retrieve image data from sql-server database, which stored as image type.
I want to retrieve an image from SQL Server to show it in an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.