ASP.NET store Image in SQL and retrieve for Asp:Image

I am looking to fileupload a picture jpeg,gif,etc into an SQL database on an updateprofilepicture page. Then on the profile page, I want to retrieve the image from an sql database and have it show up in an Asp:Image control. I have much code trying to do this and it doesn’t work. The table contains a column of type Image.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As Joel mentioned you should use an HttpHandler or a page to display the image. Here is a sample code to output image (Image.ashx) :

// ProcessRequest method of Image.ashx
long imageId = Convert.ToInt64(Request.QueryString["ImageId"]);

using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand(
    "SELECT ImageFile FROM ImageTable WHERE ImageId = @ImageID", conn))
    command.Parameters.Add("@ImageID", SqlDbType.Int).Value = imageId;

    Response.ContentType = "image/gif";
    Response.BinaryWrite((byte[]) command.ExecuteScalar());

and then use image in your page as :
  <asp:Image id="Image1" runat="server" ImageUrl="Image.ashx?ImageID=12"/>

Method 2

The important thing to remember here is that you shouldn’t try to transmit the image data with the profile page itself. Instead, you want your profile page to generate HTML markup for the browser that looks something like this:

<img src="~/MyImageHandler.ashx?UserID=1234" alt="User 1234 avatar" width="100px" height="150px" />

That is the ultimate result of your <asp:Image .../> control. Then the browser will send a completely separate Http request to retrieve the image. That’s how pictures on web sites work. You then need to be able to handle that additional request. To do that, create an Http handler (*.ashx file) and use it to retrieve the appropriate image data from the database and send it to the browser.

Method 3

If you’re using SQL 2005 or greater you should not use the data type Image because it’s now deprecated. Instead you want to use the new Varbinary(MAX) type if possible. Once you have it stored all you need to do is retrieve it via ADO.Net call and cast the cell value into type Byte[] and then call Response.BinaryWrite like in ScarletGarden’s example above.

Method 4

After a few hundred gigabytes of images, I believe you’ll find yourself thinking that the operating systems’ file system and static file http servers is better suited than the database, which is busy which a lot of other details, for storing images. It also allows you to use thousands of existing free tools to work with, move, host, etc the images.

Method 5

Instead of storing images in the database, store the path and/or filename for the image. Images will fill up the database and make it slow.

Method 6

protected void Page_Load(object sender, EventArgs e) {
    GridView1.DataSourceID = "";
    GridView1.DataSource = SqlDataSource1;

protected void btnSubmit_Click(object sender, EventArgs e) {
    string strImageName = txtImageName.Text.ToString();

    if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "") {
        byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
        HttpPostedFile uploadedImage = FileUpload1.PostedFile;
        uploadedImage.InputStream.Read(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);

        // Create SQL Connection
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=RND3" + "\" + "SQLEXPRESS;Initial Catalog=SSSolutionFiles;Integrated Security=True";

        // Create SQL Command 
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "INSERT INTO Imagess(ImageName,Image)" + " VALUES (@ImageName,@Image)";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;

        SqlParameter ImageName = new SqlParameter("@ImageName", SqlDbType.VarChar, 50);
        ImageName.Value = strImageName.ToString();

        SqlParameter UploadedImage = new SqlParameter("@Image", SqlDbType.Image, imageSize.Length);
        UploadedImage.Value = imageSize;

        Label1.Text = "File Uploaded";

        GridView1.DataSourceID = "";
        GridView1.DataSource = SqlDataSource1;

Method 7

Try these links it might help you..

you can also try by storing the image files on the server and store the paths on the Sql table..
by these links

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x