Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Image upload problem...

Image upload problem...

Scheduled Pinned Locked Moved ASP.NET
sysadminhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RajpootRohan
    wrote on last edited by
    #1

    Hi to all, I am using a gridview to display products with a link "view details". When the user clicks on the "view details" for a product, all the details are displayed in the detailsview control. I the edit mode , if user don't want to edit the image, then the initial path saved in the label should go. The path is going but image is not displayed. I am unable to find the problem. Here is the code: <asp:TemplateField HeaderText="Image"> <ItemTemplate> <asp:Label ID="lblimage" runat="server" Text='<%# Eval("image") %>' /> </ItemTemplate> <EditItemTemplate> <asp:Label ID="labelImage" runat="server" Text='<%# Eval("image") %>' /> <asp:FileUpload ID="UpldImage" runat="server" /> </EditItemTemplate> </asp:TemplateField>

        //To upload large image
        FileUpload UpldImage = (FileUpload)DetailsView1.FindControl("UpldImage");
        Label labelImage = (Label)DetailsView1.FindControl("labelImage");
        string im = labelImage.Text;
        if (UpldImage.HasFile)
        {
            string fileName = "~/ProductImages/" + UpldImage.FileName;
            SqlDataSource2.UpdateParameters\["image"\].DefaultValue = fileName;
            UpldImage.SaveAs(Server.MapPath(fileName));
        }
        else
        {
            SqlDataSource2.UpdateParameters\["image"\].DefaultValue = im;
            UpldImage.SaveAs(Server.MapPath(im));
        }
    

    Please assist me...where I am going wrong.

    cheers, sneha

    A 1 Reply Last reply
    0
    • R RajpootRohan

      Hi to all, I am using a gridview to display products with a link "view details". When the user clicks on the "view details" for a product, all the details are displayed in the detailsview control. I the edit mode , if user don't want to edit the image, then the initial path saved in the label should go. The path is going but image is not displayed. I am unable to find the problem. Here is the code: <asp:TemplateField HeaderText="Image"> <ItemTemplate> <asp:Label ID="lblimage" runat="server" Text='<%# Eval("image") %>' /> </ItemTemplate> <EditItemTemplate> <asp:Label ID="labelImage" runat="server" Text='<%# Eval("image") %>' /> <asp:FileUpload ID="UpldImage" runat="server" /> </EditItemTemplate> </asp:TemplateField>

          //To upload large image
          FileUpload UpldImage = (FileUpload)DetailsView1.FindControl("UpldImage");
          Label labelImage = (Label)DetailsView1.FindControl("labelImage");
          string im = labelImage.Text;
          if (UpldImage.HasFile)
          {
              string fileName = "~/ProductImages/" + UpldImage.FileName;
              SqlDataSource2.UpdateParameters\["image"\].DefaultValue = fileName;
              UpldImage.SaveAs(Server.MapPath(fileName));
          }
          else
          {
              SqlDataSource2.UpdateParameters\["image"\].DefaultValue = im;
              UpldImage.SaveAs(Server.MapPath(im));
          }
      

      Please assist me...where I am going wrong.

      cheers, sneha

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      if (UpldImage.HasFile) is False Why do you need to cal UpldImage.SaveAs ?? If there is no file, no need to cal SaveAs. Also you forgot to mention, what problem you are getting.. ;)

      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


      My Latest Articles-->** Simplify Code Using NDepend
      Basics of Bing Search API using .NET
      Microsoft Bing MAP using Javascript

      R 2 Replies Last reply
      0
      • A Abhishek Sur

        if (UpldImage.HasFile) is False Why do you need to cal UpldImage.SaveAs ?? If there is no file, no need to cal SaveAs. Also you forgot to mention, what problem you are getting.. ;)

        Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


        My Latest Articles-->** Simplify Code Using NDepend
        Basics of Bing Search API using .NET
        Microsoft Bing MAP using Javascript

        R Offline
        R Offline
        RajpootRohan
        wrote on last edited by
        #3

        Hi, The problem is that in the edit mode of details view a label and a file upload control is shown for the image. In the label control the current path is displayed. If the user doesn't changes the image then the path in label goes in the update function. And if the user changes the file then the path from the file upload control goes. My problem is if the user does not change the image then the path from the label goes well but the image disappears from the front end. If I again choose the path from the file uploader then the image appears.

        cheers, sneha

        1 Reply Last reply
        0
        • A Abhishek Sur

          if (UpldImage.HasFile) is False Why do you need to cal UpldImage.SaveAs ?? If there is no file, no need to cal SaveAs. Also you forgot to mention, what problem you are getting.. ;)

          Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


          My Latest Articles-->** Simplify Code Using NDepend
          Basics of Bing Search API using .NET
          Microsoft Bing MAP using Javascript

          R Offline
          R Offline
          RajpootRohan
          wrote on last edited by
          #4

          strange behaviour is that the two paths for the image (one in the label and second when I again choose from the file upload) are same.But the image appears only when the path is choosen from the file upload.

          cheers, sneha

          I 1 Reply Last reply
          0
          • R RajpootRohan

            strange behaviour is that the two paths for the image (one in the label and second when I again choose from the file upload) are same.But the image appears only when the path is choosen from the file upload.

            cheers, sneha

            I Offline
            I Offline
            Imran Khan Pathan
            wrote on last edited by
            #5

            If you have already image in database and If user does not want to update image then why are you calling update method. There are two ways. 1. dont update database if user does not select the file.

            FileUpload UpldImage = (FileUpload)DetailsView1.FindControl("UpldImage");
            Label labelImage = (Label)DetailsView1.FindControl("labelImage");
            string im = labelImage.Text;
            if (UpldImage.HasFile)
            {
            string fileName = "~/ProductImages/" + UpldImage.FileName;
            SqlDataSource2.UpdateParameters["image"].DefaultValue = fileName;
            UpldImage.SaveAs(Server.MapPath(fileName));
            }

            2. Update database if you want but dont save image again because you have already file in folder.

            FileUpload UpldImage = (FileUpload)DetailsView1.FindControl("UpldImage");
            Label labelImage = (Label)DetailsView1.FindControl("labelImage");
            string im = labelImage.Text;
            if (UpldImage.HasFile)
            {
            string fileName = "~/ProductImages/" + UpldImage.FileName;
            SqlDataSource2.UpdateParameters["image"].DefaultValue = fileName;
            UpldImage.SaveAs(Server.MapPath(fileName));
            }
            else
            {
            SqlDataSource2.UpdateParameters["image"].DefaultValue = im;
            //UpldImage.SaveAs(Server.MapPath(im));
            }

            If you dont want to update any code, then please check else condition, what does im contain? and after executing else code, please check your folder to verify that file is exist or not. degun the code, you will get an idea. Regard

            please don't forget to vote on the post that helped you.

            R 1 Reply Last reply
            0
            • I Imran Khan Pathan

              If you have already image in database and If user does not want to update image then why are you calling update method. There are two ways. 1. dont update database if user does not select the file.

              FileUpload UpldImage = (FileUpload)DetailsView1.FindControl("UpldImage");
              Label labelImage = (Label)DetailsView1.FindControl("labelImage");
              string im = labelImage.Text;
              if (UpldImage.HasFile)
              {
              string fileName = "~/ProductImages/" + UpldImage.FileName;
              SqlDataSource2.UpdateParameters["image"].DefaultValue = fileName;
              UpldImage.SaveAs(Server.MapPath(fileName));
              }

              2. Update database if you want but dont save image again because you have already file in folder.

              FileUpload UpldImage = (FileUpload)DetailsView1.FindControl("UpldImage");
              Label labelImage = (Label)DetailsView1.FindControl("labelImage");
              string im = labelImage.Text;
              if (UpldImage.HasFile)
              {
              string fileName = "~/ProductImages/" + UpldImage.FileName;
              SqlDataSource2.UpdateParameters["image"].DefaultValue = fileName;
              UpldImage.SaveAs(Server.MapPath(fileName));
              }
              else
              {
              SqlDataSource2.UpdateParameters["image"].DefaultValue = im;
              //UpldImage.SaveAs(Server.MapPath(im));
              }

              If you dont want to update any code, then please check else condition, what does im contain? and after executing else code, please check your folder to verify that file is exist or not. degun the code, you will get an idea. Regard

              please don't forget to vote on the post that helped you.

              R Offline
              R Offline
              RajpootRohan
              wrote on last edited by
              #6

              Imran Khan Pathan wrote:

              If you dont want to update any code, then please check else condition, what does im contain? and after executing else code, please check your folder to verify that file is exist or not. degun the code, you will get an idea.

              I already checked the else condition and on debugging found that "im" contains the correct path. And the image folder still contains the image. But strange behaviour is image disappears. I dont know why it is happening.

              cheers, sneha

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups