Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

User Profile-Pic in file-system, C#.

$
0
0

Project: I am trying to make a small website for my school buddies where they can store basic info about them. Have implemented Scott Mitchell's method so far.

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. 

<asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="Button1" runat="server" Text="Upload" onclick="Button1_Click" /><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
protected void Button1_Click(object sender, EventArgs e)
{
string virtualFolder = "~/UserPics/";
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["ConnectionString"].ConnectionString;
string updateSql = "UPDATE UserDetails SET UserPicUrl = ImageUrl WHERE UserId=@currentUserId";
    

using (SqlConnection myConnection = new SqlConnection(connectionString))
{
    myConnection.Open();
    SqlCommand myCommand = new SqlCommand(updateSql, myConnection);
    myCommand.Parameters.AddWithValue("@UserPicUrl", DBNull.Value);

    myCommand.ExecuteNonQuery();
    myConnection.Close();
}
}


However I am unable to do so, I think part where inserting ImageUrl in db is wrong. 

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.

Please Help..  Have been searching whole day for a solution and will be a great help.


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>