I want to allow user to change the image(Stored as varbinary(max)) in the edit template in Formview.
This works when the fileupload is populated but I want to allow the user to leave the old image as is and just update the other fields. Have seen a lot of similar threads but they don't seem to show how to achive this when image is stored as binary.
<asp:SqlDataSource ID="ShopDatabase" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Product] WHERE ([ProductID] = @ProductID)"
UpdateCommand="UPDATE [Product] SET [ProductName] = @ProductName, [Description] = @Description, [Stock] = @Stock, [Price] = @Price, [Archived] = @Archived, [CategoryID] = @CategoryID,
[ImageData] =@ImageData ,
[ImageName] =@ImageName ,
[ImageType] =@ImageType )
WHERE [ProductID] = @ProductID">
<%--In below code need to use full name of textbox as it is inside the edit template--%>
<UpdateParameters>
<asp:ControlParameter Name="ProductName" Type="String" ControlId="ctl00$Head$FormView1$txtName" PropertyName="Text"/>
<asp:ControlParameter Name="Description" Type="String" ControlId="ctl00$Head$FormView1$TxtDesc" PropertyName="Text"/>
<asp:ControlParameter Name="Price" Type="Decimal" ConvertEmptyStringToNull="true" ControlId="ctl00$Head$FormView1$TxtPrice"/>
<asp:ControlParameter Name="Stock" Type="String" ControlId="ctl00$Head$FormView1$TxtStock" PropertyName="Text"/>
<asp:ControlParameter Name="Archived" Type="Boolean" ControlId="ctl00$Head$FormView1$ChkArchive" PropertyName="Checked"/>
<asp:ControlParameter Name="CategoryID" Type="Int32" ControlId="ctl00$Head$FormView1$CategoryList" PropertyName="SelectedValue"/>
<asp:Parameter Name="ImageName" />
<asp:Parameter Name="ImageType" />
<asp:Parameter Name="ImageData" type="Byte" />
<asp:Parameter Name="original_ImageName" />
<asp:Parameter Name="original_ImageType" />
<asp:Parameter Name="original_ImageData" type="Byte" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="ProductID" QueryStringField="ProductID"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
I tried to achieve this in the form updating but my image keeps getting overwritten by a null value
Protected Sub FormView1_ItemUpdating(sender As Object, e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles FormView1.ItemUpdating
Try
Dim FileUpload2 As FileUpload = DirectCast(FormView1.FindControl("FileUploadImage"), FileUpload)
If FileUpload2.HasFile Then
e.NewValues("ImageName") = FileUpload2.FileName
e.NewValues("ImageType") = FileUpload2.PostedFile.ContentType
e.NewValues("ImageData") = FileUpload2.FileBytes
Else
e.NewValues("ImageName") = e.OldValues("ImageName")
e.NewValues("ImageType") = e.OldValues("ImageType")
e.NewValues("ImageData") = e.OldValues("ImageData")
End If
Catch ex As Exception
lblStatus.Text = "Error Updating Picture"
End Try
End Sub
Can anyone point me in the right direction please?
I want the image to update when the user selects a new file but not when it is left blank.
At the moment when its left blank it wipes the old image data and gives error "Unable to cast object of type 'System.DBNull' to type 'System.Byte[]" when the page tries to reload as it has nulled the data in the image field
Remove this from Item Updating event and in else clause change the Update Command of the DataSource such a way that it will update only other fields except Iamgename . ImageType and ImageData
Kindly mark this post as "Answer", if it helped you.
After all that I have a new Iconvertible error when I do try to upload a file. It used to work so must be something i screwed up while trying to resolve previous error
It occures after the ItemUpdated event but the dump isn't very clear as to what is the problem
If you don't declare type, it will determine type by itself. Declaration is best used only when it is necessary to force a type.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
dave13
Member
29 Points
30 Posts
FileUpload in EditItemtemplate in Formview
May 04, 2012 08:59 PM|LINK
I want to allow user to change the image(Stored as varbinary(max)) in the edit template in Formview.
This works when the fileupload is populated but I want to allow the user to leave the old image as is and just update the other fields. Have seen a lot of similar threads but they don't seem to show how to achive this when image is stored as binary.
Edit item template and data source from form
<EditItemTemplate> <div class="ContentHead"> <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("ProductName")%>'/> </div> <table border="0"> <tr> <td style="vertical-align: top;"> <asp:Image id="picAlbum" Height = "200" Width = "200" runat="server" AlternateText='Image Missing' ImageUrl='<%# Bind("ProductID", "~/ImageHandler.aspx?ProductID={0}")%>'/> </td> <td style="vertical-align: top"><asp:FileUpload ID="FileUploadImage" runat="server" Filebytes ='<%# Bind("ProductID", "~/ImageHandler.aspx?ProductID={0}")%>' /> </td> <br /> <td style="vertical-align: top"> <b>Description:</b> <asp:TextBox ID="TxtDesc" runat="server" Text='<%# Bind("Description")%>'/> <br /><br /> </td> <td style="vertical-align: top"><b>Archived:</b> <asp:CheckBox ID="ChkArchive" runat="server" Checked='<%# Bind("Archived") %>'/> <br /><br /><br /> </td> <asp:DropDownList id="CategoryList" datasourceid="CategorySQLDataSource" datatextfield="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>' runat="server"/> <br /><br /><br /> </tr> </table> <%-- Need to change price format here to remove currency signs so updating can complete--%> <span class="UnitCost"><b>Your Price:</b> <asp:TextBox ID="TxtPrice" runat="server" Text='<%# Bind("Price", "{0:0.00}")%>' /><br /><span class="ModelNumber"><b>Current Stock:</b> <asp:TextBox ID="TxtStock" runat="server" Text='<%# Bind("Stock")%>' /></span> <br /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" /> <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </EditItemTemplate>I tried to achieve this in the form updating but my image keeps getting overwritten by a null value
Protected Sub FormView1_ItemUpdating(sender As Object, e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles FormView1.ItemUpdating Try Dim FileUpload2 As FileUpload = DirectCast(FormView1.FindControl("FileUploadImage"), FileUpload) If FileUpload2.HasFile Then e.NewValues("ImageName") = FileUpload2.FileName e.NewValues("ImageType") = FileUpload2.PostedFile.ContentType e.NewValues("ImageData") = FileUpload2.FileBytes Else e.NewValues("ImageName") = e.OldValues("ImageName") e.NewValues("ImageType") = e.OldValues("ImageType") e.NewValues("ImageData") = e.OldValues("ImageData") End If Catch ex As Exception lblStatus.Text = "Error Updating Picture" End Try End SubCan anyone point me in the right direction please?
basheerkal
Star
10672 Points
2426 Posts
Re: FileUpload in EditItemtemplate in Formview
May 05, 2012 01:50 AM|LINK
If you dont want to change the image why you need code in ItemInserting event. Delete it.
(Talk less..Work more)
dave13
Member
29 Points
30 Posts
Re: FileUpload in EditItemtemplate in Formview
May 05, 2012 10:27 AM|LINK
Hi
I want the image to update when the user selects a new file but not when it is left blank.
At the moment when its left blank it wipes the old image data and gives error "Unable to cast object of type 'System.DBNull' to type 'System.Byte[]" when the page tries to reload as it has nulled the data in the image field
basheerkal
Star
10672 Points
2426 Posts
Re: FileUpload in EditItemtemplate in Formview
May 05, 2012 04:33 PM|LINK
Else e.NewValues("ImageName") = e.OldValues("ImageName") e.NewValues("ImageType") = e.OldValues("ImageType") e.NewValues("ImageData") = e.OldValues("ImageData")Remove this from Item Updating event and in else clause change the Update Command of the DataSource such a way that it will update only other fields except Iamgename . ImageType and ImageData
(Talk less..Work more)
dave13
Member
29 Points
30 Posts
Re: FileUpload in EditItemtemplate in Formview
May 06, 2012 12:50 PM|LINK
Thanks very much, worked perfectly
dave13
Member
29 Points
30 Posts
Re: FileUpload in EditItemtemplate in Formview
May 06, 2012 02:17 PM|LINK
After all that I have a new Iconvertible error when I do try to upload a file. It used to work so must be something i screwed up while trying to resolve previous error
It occures after the ItemUpdated event but the dump isn't very clear as to what is the problem
I assume it has to do with the values inserted from the fileupload.
I tried changing the update parameters to the below but error still exists
<asp:Parameter Name="ImageName" Type="String" />
<asp:Parameter Name="ImageType" Type="String" />
<asp:Parameter Name="ImageData" type="Byte" />
basheerkal
Star
10672 Points
2426 Posts
Re: FileUpload in EditItemtemplate in Formview
May 06, 2012 02:55 PM|LINK
Can you tell which line generates error?
(In page directive Put Debug="True")
(Talk less..Work more)
dave13
Member
29 Points
30 Posts
Re: FileUpload in EditItemtemplate in Formview
May 06, 2012 03:06 PM|LINK
Still didn't pinpoint it.
I removed the type="Byte" from the parameter declaration and it seems to work.
Not sure why, is a type not required
superguppie
All-Star
48225 Points
8679 Posts
Re: FileUpload in EditItemtemplate in Formview
May 11, 2012 02:18 PM|LINK
If you don't declare type, it will determine type by itself. Declaration is best used only when it is necessary to force a type.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.