1

I have a web application which has a user profile page. That page allows the user to upload its own profile picture. I am finding a way to set a default varbinary(max) value for my profile picture in database (SQL Server 2008). Something like Facebook which has a default profile picture once you sign up. I am able to upload a picture to the database and display it out, but if my profile picture column in database is NULL, I get error decoding the image.

Uploading image code:

string filePath = FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);

string contenttype = String.Empty;

switch (ext)
{
    case ".jpg":
       contenttype = "image/jpg";
       break;
}

if (contenttype != String.Empty)
{
   System.Drawing.Image uploaded = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);

   System.Drawing.Image newImage = new Bitmap(1024, 768);

   using (Graphics g = Graphics.FromImage(newImage))
   {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(uploaded, 0, 0, 1024, 768);
   }

   byte[] results;

   using (MemoryStream ms = new MemoryStream())
   {
         ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
         EncoderParameters jpegParms = new EncoderParameters(1);
         jpegParms.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
         newImage.Save(ms, codec, jpegParms);
         results = ms.ToArray();
   }

Display image code:

string strQuery = "select profilepic from MemberAccount where nric='"+ Session["nric"] +"'";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
if (!dt.Rows[0]["profilepicture"].Equals(DBNull.Value))
{
   download(dt);
}

private DataTable GetData(SqlCommand cmd)
{
    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI");
    SqlDataAdapter sda = new SqlDataAdapter();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    try
    {
       con.Open();
       sda.SelectCommand = cmd;
       sda.Fill(dt);
       return dt;
    }
    catch
    {
       return null;
    }
    finally
    {
        con.Close();
        sda.Dispose();
        con.Dispose();
    }
}


private void download(DataTable dt)
{
    Byte[] bytes = (Byte[])dt.Rows[0]["profilepicture"];
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "image/jpg";
    Response.BinaryWrite(bytes);
    Response.End();
}

Added the if statement in the part when displaying image but having error

if (!dt.Rows[0]["profilepicture"].Equals(DBNull.Value))

This line give error:

Object reference not set to an instance of an object.
XiAnG
  • 245
  • 2
  • 9
  • 25

1 Answers1

0

Why don't you save the default image in some images folder on your site? That way you don't have to read the database column, instead use the relative path in the image control.

Try comparing

if(!dt.Rows[0]["profilepicture"].Equals(DBNull.Value))
Nilesh
  • 2,583
  • 5
  • 21
  • 34