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.
5
  • You can check if dt.Rows[0]["profilepicture"] is null. Can you add the exception you are getting? Commented Jul 29, 2013 at 4:18
  • I try using this if (!dt.Rows[0]["profilepicture"].Equals(null)) but it dont work Commented Jul 29, 2013 at 4:21
  • Can you update the question with how you retrieve data from database, what is dt? Commented Jul 29, 2013 at 4:31
  • 2
    This question is answered stackoverflow.com/questions/4604414/…. You have to check for DBNull.Value. Commented Jul 29, 2013 at 4:32
  • @Damith dt is datatable. Commented Jul 29, 2013 at 5:36

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.