title: User Profile-Pic in file-system, C#
body: <p>Project: I am trying to make a small website for my school bddies where they can store basic info about them. Have implemented <a href="http://www.asp.net/web-forms/tutorials/security/membership/storing-additional-user-information-cs" rel="nofollow noreferrer">Scott Mitchell</a>'s method so far.</p>
  <p>Problem : I want to let user store a profile pic of him. Doing so by utilising storage in file system and saving the path in database.</p>
  <pre><code>&lt;asp:FileUpload ID=&quot;FileUpload1&quot; runat=&quot;server&quot; /&gt;
  &lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Upload&quot; onclick=&quot;Button1_Click&quot; /&gt;
  &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&quot;Label&quot;&gt;&lt;/asp:Label&gt;
  </code></pre>
  <p>C# :</p>
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
  {
      string virtualFolder = &quot;~/UserPics/&quot;;
      string physicalFolder = Server.MapPath(virtualFolder);
      string fileName = Guid.NewGuid().ToString();
      string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
      FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
      String ImageUrl = virtualFolder + fileName + extension;
  
      Label1.Text = ImageUrl;
  
  
      MembershipUser currentUser = Membership.GetUser();
      Guid currentUserId = (Guid)currentUser.ProviderUserKey;
  
  
      string connectionString = ConfigurationManager.ConnectionStrings[&quot;ConnectionString&quot;].ConnectionString;
      string updateSql = &quot;UPDATE UserDetails SET UserPicUrl = ImageUrl WHERE UserId=@currentUserId&quot;;
      
  
      using (SqlConnection myConnection = new SqlConnection(connectionString))
      {
          myConnection.Open();
          SqlCommand myCommand = new SqlCommand(updateSql, myConnection);
          myCommand.Parameters.AddWithValue(&quot;@UserPicUrl&quot;, DBNull.Value);
  
          myCommand.ExecuteNonQuery();
          myConnection.Close();
      }
  }
  </code></pre>
  <p>However I am unable to do so, I think part where inserting ImageUrl in db is wrong.</p>
  <p>Further more the second time a user changes his profile pic the first image will be left in file how to delete it when a second image is uploaded.</p>
  <p>Please Help..  Have been searching whole day for a solution and will be a great help.</p>