Image upload problem...
-
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
-
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
if (
UpldImage.HasFile
) is False Why do you need to calUpldImage.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 -
if (
UpldImage.HasFile
) is False Why do you need to calUpldImage.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 JavascriptHi, 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
-
if (
UpldImage.HasFile
) is False Why do you need to calUpldImage.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 Javascriptstrange 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
-
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
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.
-
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.
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