Hi,
Does anyone have any recent examples of saving and retrieving images to/from a database? I'm using visual Studio 2008, Sql 2008 and ASP.Net AJAX Q3 2010 NET35 .
I'm using varbinary(MAX) to store the image stream and I store the contenttype and filesize in the database as well.
This is what I'm doing to save the image:
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click If Page.IsValid Then Dim oProduct As New Product Dim iProductID As Integer = CInt(hdnProductID.Value) oProduct = ProductBL.GetProductByID(iProductID) oProduct.Name = txtName.Text.Trim oProduct.Description = txtDescription.Text.Trim Dim imgStream As Stream = fleUpload.PostedFile.InputStream oProduct.Filename = fleUpload.PostedFile.FileName oProduct.ContentType = fleUpload.PostedFile.ContentType oProduct.Filesize = fleUpload.PostedFile.ContentLength Dim imgData(oProduct.Filesize) As Byte oProduct.imgContent = imgData Dim oProductBL As New ProductBL If oProductBL.Save(oProduct) > 0 Then '== Show save message Else '== Show error message End If End If End SubEverything saves to the database without errors...but when I view the page I get an error.
This is my RadGrid:
<telerik:RadGrid ID="gridProducts" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" GridLines="None" Skin="Outlook" width="675px"> <MasterTableView allowcustompaging="True"> <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings> <RowIndicatorColumn> <HeaderStyle Width="20px"></HeaderStyle> </RowIndicatorColumn> <ExpandCollapseColumn> <HeaderStyle Width="20px"></HeaderStyle> </ExpandCollapseColumn> <Columns> <telerik:GridTemplateColumn HeaderText="Item" UniqueName="Item"> <ItemTemplate> <telerik:RadBinaryImage ID="rbiItem" runat="server" /> </ItemTemplate> </telerik:GridTemplateColumn> <telerik:GridTemplateColumn HeaderText="Item Description" UniqueName="ItemDescription"> <ItemTemplate> <asp:Label ID="lblItem" runat="server" Text=""></asp:Label> <br /> <asp:Label ID="lblDescription" runat="server" Text=""></asp:Label> </ItemTemplate> </telerik:GridTemplateColumn> <telerik:GridTemplateColumn HeaderText="Price" UniqueName="Price"> <ItemTemplate> <asp:Label ID="lblPrice" runat="server" Text=""></asp:Label> </ItemTemplate> </telerik:GridTemplateColumn> <telerik:GridTemplateColumn HeaderText="Add to Cart"> <ItemTemplate> <asp:ImageButton ID="imgCart" OnCommand="imgCart_Command" CommandName="Add" runat="server" /> </ItemTemplate> </telerik:GridTemplateColumn> </Columns> </MasterTableView> <ClientSettings> <Selecting AllowRowSelect="True" /> </ClientSettings> </telerik:RadGrid>I'm trying to retrieve the image into the RadBinaryImage control rbiItem.
This is how I retrieve the images into a gridview:
Private Sub LoadGrid() gridProducts.DataSource = ProductBL.GetActiveProducts gridProducts.DataBind() End Sub Private Sub gridProducts_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles gridProducts.ItemDataBound If (TypeOf (e.Item) Is GridDataItem) Then Dim dr As PawnShop.Entities.Product = CType(e.Item.DataItem, PawnShop.Entities.Product) Dim lblItem As Label = CType(e.Item.FindControl("lblItem"), Label) lblItem.Text = dr.Name Dim lblDescription As Label = CType(e.Item.FindControl("lblDescription"), Label) lblDescription.Text = dr.Description Dim lblPrice As Label = CType(e.Item.FindControl("lblPrice"), Label) lblPrice.Text = String.Format(Constants.CURRENCY, dr.Price) Dim imgCart As ImageButton = CType(e.Item.FindControl("imgCart"), ImageButton) imgCart.ImageUrl = "~/images/shoping_cart_sm.png" imgCart.CommandArgument = dr.ProductID.ToString imgCart.CommandName = "Add" '==Get the image from the database Dim rbiItem As RadBinaryImage = CType(e.Item.FindControl("rbiItem"), RadBinaryImage) Response.ContentType = dr.ContentType rbiItem.DataValue = CType(dr.imgContent, Byte()) End If End SubServer Error in '/' Application. -------------------------------------------------------------------------------- Parameter is not valid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentException: Parameter is not valid. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [ArgumentException: Parameter is not valid.] System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) +1062379 System.Drawing.Image.FromStream(Stream stream) +8 Telerik.Web.UI.ImageFilterHelper.CreateImgFromBytes(Byte[] image) +99 [ArgumentException: The provided binary data may not be valid image or may contains unknown header] Telerik.Web.UI.ImageFilterHelper.CreateImgFromBytes(Byte[] image) +173 Telerik.Web.UI.RadBinaryImage.AutoAdjustImageElementSize(Byte[] dataValue) +51 Telerik.Web.UI.RadBinaryImage.ProcessImageData() +188 Telerik.Web.UI.RadBinaryImage.OnPreRender(EventArgs e) +41 System.Web.UI.Control.PreRenderRecursiveInternal() +80 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842 -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:2.0.50727.4952; ASP.NET Version:2.0.50727.4955Does this mean that I'm not storing it correctly or am I not retrieving it correctly?
Can anyone help me on this?
Thanks