Saving images from formview

Last post 04-05-2007 7:13 AM by anantham. 6 replies.

Sort Posts:

  • Saving images from formview

    01-28-2007, 7:13 AM

    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();
        }

  • Re: Saving images from formview

    01-28-2007, 9:51 AM
    • Loading...
    • CSharpSean
    • Joined on 10-21-2006, 5:43 AM
    • Orlando, FL
    • Posts 873

    The user will have to upload the file first by clicking an upload button.

    So, create an upload button next to the upload control (and a label underneath it), put the code you currently have above, inside of the upload button click event. When you upload the file, save the filename in the label. then do all your saving in the ItemInserting Event and reference the filename through the label name. Try that :)

     

    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx 

  • Re: Saving images from formview

    01-30-2007, 9:35 AM

    Hey, this code is very good, although i have a problem. My fileUpload2 is in my formview (visual studio) and it says that 'FileUpload2 does not exist in the current context' but it does exists. Do you know why it is doing this. I have tried placing the code everywhere on the .cs page and it still comes up with the error. I had created a button alongside the fileupload2 and placed the code within there and it still doesn't recognise fileupload2. i know its because i have probably placed the code somewhere wrong but ive tried it everywhere.

    ARRRRRRRRRRRRRRRRRR

  • Re: Saving images from formview

    01-30-2007, 9:55 AM
    • Loading...
    • CSharpSean
    • Joined on 10-21-2006, 5:43 AM
    • Orlando, FL
    • Posts 873

    make sure you use the ((FormView) FormView1.FindControl("FileUpload2")) something like that.  Also, double check the name of the control in the findcontrol method b/c it will not recognize if it is the correct control name until runtime.

    Label1.Text = ((FormView)FormView1.FindControl("FileUpload2")).FileName ; 

  • Re: Saving images from formview

    02-02-2007, 6:23 AM

    Ok, so i have now managed to save the image into my 'Images' folder on my server from a FormView. Now all i need to do is save the images url into my ms sql 2005 database. Does anybody know what datatype these images should be saved too and how i go about the coding behind it in c# and asp.net2.

     

  • Re: Saving images from formview

    02-02-2007, 8:48 AM
    Answer
    • Loading...
    • CSharpSean
    • Joined on 10-21-2006, 5:43 AM
    • Orlando, FL
    • Posts 873

    save the url as a string. I usually save the image filename in the database as a string. So, you can code this to pull the image like:

    aspx:

    <asp:Image ID="imgProduct" runat="server" ImageUrl='<%# Eval("fldImage","~/images/{0}") %>' />

     

  • Re: Saving images from formview

    04-05-2007, 7:13 AM
    • Loading...
    • anantham
    • Joined on 04-05-2007, 7:01 AM
    • Posts 1

    if we want to use ' / ' in the string we have we should specify like " \/ " insted of "/" like

    "Images\/"

    or

    we should specify @ at the begining of the string like

    @"Images/"

    String path = Server.MapPath("Images/" + f.FileName);

Page 1 of 1 (7 items)
Microsoft Communities
Page view counter