1

I have avatars stored in a database. I have used the the data type image for the avatars. I want to load the avatar of a user into an image control, but i cant get it to work. I have tried this code without any luck:

public void GetUserAvatar()
    {
        string username = Convert.ToString(Session["username"]);

        var image = from u in dc.Users
                    where u.username == username
                    select u.image;
        imgAvatar.Controls.Add(image);
    }

I think i might have to save the images to a folder and then save the file path in the database instead. This seems to be easier and smoother. Any ideas?

I ended up saving the image to a folder and then saving the path to the database. My code for image uploading now looks like this:

public void addImage()
    {
        if (fuAvatar.PostedFile.ContentType.ToLower().StartsWith
            ("image") && fuAvatar.HasFile)
        {
            string saveLocation = Server.MapPath("savedAvatars/");
            string fileExtension = System.IO.Path.GetExtension(fuAvatar.FileName);
            string fileName = Convert.ToString(Session["userid"]);
            string savePath = saveLocation + fileName + fileExtension;
            fuAvatar.SaveAs(savePath);

            string imageDBPath = fileName + fileExtension;

            LinqClass1DataContext dc = new LinqClass1DataContext();
            int userid = Convert.ToInt32(Session["userid"]);
            var tblUser = (from u in dc.Users
                           where u.userid == userid
                           select u).First();
            tblUser.imagePath = @"\savedAvatars\"+imageDBPath;
            dc.SubmitChanges();
            lblResult.Text = "Avatar lastet opp!";
        }
        else lblResult.Text = "Avatar ikke lastet opp!";
    }

And to load the picture:

public void GetUserAvatar()
   {
       int userid = Convert.ToInt32(Session["userid"]);

       var varPath = dc.Users.Single(u => (u.userid == userid)).imagePath;

       string imagePath = Convert.ToString(varPath);
       imgAvatar.ImageUrl = imagePath;
   }
2
  • 1
    Any reason as to why you are storing the actual image in the database? Surely storing a file path would be better? Commented Mar 27, 2012 at 8:40
  • stackoverflow.com/questions/3748/… Commented Mar 27, 2012 at 8:49

3 Answers 3

3

you should add a generic handler file (.ashx) and write in it something like that:

string sql = "select image from table1";
SqlCommand command = new SqlCommand(sql, con);
SqlDataReader dReader = command.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["Source"]);
dReader.Close();

then in your page do something like this:

img.ImageUrl = "Handler.ashx;
Sign up to request clarification or add additional context in comments.

3 Comments

Do I have to create a new method in this file or can I just use the public void ProcessRequest(HttpContext context)?
you don't need to create a new method, actually you should use the ProcessRequest method :)
A little more info can be found here: codeproject.com/Articles/33310/…
0

http://www.dotnetcurry.com/ShowArticle.aspx?ID=129 read this article, first I've found. Basically you need to create a handler which responds with the raw image.

Comments

0

Please find the answer in below link.

Get images from database using Handler

You will have to create a handler which will load the image and u can pass the source to image tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.