Hello, At first when it was error free i thought it worked until i noticed the image field within my database was empty. Basically i am using visual studio 2005 with a ms sql 2005 database with c# and asp.net2.0.
I have created a formview on my sell.aspx page which users can place items for sale. They are given the choice to add an image of the item they want to sell. Since my site will be having lots of pictures of peoples items, i am trying to save the image file to a folder on the server called 'Images' and then save the url of the image in my ms sql database. Below is the code i have, it has no errors but it does not save the image in the 'Images' folder or the url in the databse.
What have i missed out or done wrong?
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
FileUpload f = (FileUpload)FormView1.FindControl("FileUpload2");
if (f.HasFile)
{
String path = Server.MapPath("Images/" + f.FileName);
System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();
String[] allowedExtensions =
{ ".gif",".jpeg", ".jpg" };
// To enable this sample, grant Write permission to the ASP.NET process account
// for the Images subdirectory and uncomment below lines of code.
f.SaveAs(path);
CreateThumbnail(path, Server.MapPath("Images/Thumbs/" + f.FileName));
}}
public void CreateThumbnail(String srcpath, String destpath) {
System.Drawing.Image img = System.Drawing.Image.FromFile(srcpath);
System.Drawing.Image imgthumb = img.GetThumbnailImage(100, 75, null, new System.IntPtr(0));
imgthumb.Save(destpath, ImageFormat.Jpeg);
img.Dispose();
imgthumb.Dispose();
}