My friends I was working on the concept like a user can upload his image as a user avatar, and that should be shown in all his pages.

I have two pages namely get.aspx and show.aspx

get.aspx contains image file uploading and saving the records in database.

show.aspx is to show the image file of the user.

I got the image in the form of byte[].

But I need to display it as a image.

Here is the code for show.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Drawing;

namespace ImagesaveinDB
    public partial class show : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)

        public Stream ShowEmpImage(int empno)
        string conn = "asdasdasdas";
            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);
            object img = cmd.ExecuteScalar();
            //show sh = new show();
            //DataTable dt = cmd.ExecuteScalar();
            //sh.Image1.ImageUrl = img;

                Byte[] bytes = (Byte[])img;
                MemoryStream stream = new MemoryStream((byte[])img);
                Bitmap bitmap = new Bitmap(stream);
                show s1 = new show();
                return null;
                //return new MemoryStream((byte[])img);
            catch(Exception e)
                return null;

I need your help in doing this friends.

Thank you in advance...

Saving image on the database, consumes a lot of space, so it's better to save image in a folder and save the image path in the database

I know that too write2varun. I just want everything to be stored in database only. Hence i have to do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.