Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7712681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:41:35+00:00 2026-06-01T01:41:35+00:00

I have avatars stored in a database. I have used the the data type

  • 0

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;
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:41:36+00:00Added an answer on June 1, 2026 at 1:41 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

User of my site upload images which are used as avatars. I have set
Quick question: We want to be able to store a user avatar image for
I have a User table that has all of their avatars saved in an
I am working on a custom avatar system for a project, but I have
I have users who will have 2 types of picture images, + avatar image.
I have a query that selects data from 4 tables via joins, i want
I am making a social networking site where I want to have a user's
I am making a social networking site where I want to have a user's
I have two models Profile and Avatar. models.py class Profile(models.Model): user = models.ForeignKey(User) profile_name
I have about 60 images I want to store in Core Data, 30 of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.