Store Image Into Binary Format

First Step: First I am creating a dbml file into the project and dragging and dropping a table.
 
Step Second: Adding an aspx form --  there is one textbox, one fileupload control and button control.
 
Third Step: Saving data into the database and handling exception from the catch.
 
  1. protected void btnSave_Click(object sender, EventArgs e)  
  2. {  
  3.     try   
  4.     {  
  5.         using(DataContextLinqDataContext context = new DataContextLinqDataContext())  
  6.         {  
  7.             FileUpload imgUper = (FileUpload) fluImage;  
  8.             Byte[] imgByteUpper = null;  
  9.             if (imgUper.HasFile && imgUper.PostedFile != null)   
  10.             {  
  11.                 HttpPostedFile File = fluImage.PostedFile;  
  12.                 imgByteUpper = new Byte[File.ContentLength];  
  13.                 File.InputStream.Read(imgByteUpper, 0, File.ContentLength);  
  14.                 var binaryData = new BinaryData();  
  15.                 binaryData.Name = txtName.Text.Trim();  
  16.                 binaryData.ImageBinary = imgByteUpper;  
  17.                 context.BinaryDatas.InsertOnSubmit(binaryData);  
  18.                 context.SubmitChanges();  
  19.                 txtName.Text = "";  
  20.                 lblMessage.ForeColor = System.Drawing.Color.Green;  
  21.                 lblMessage.Text = "Image Uploaded Successfully.";  
  22.             } else {  
  23.                 txtName.Text = "";  
  24.                 lblMessage.ForeColor = System.Drawing.Color.Red;  
  25.                 lblMessage.Text = "Please Select Image First.";  
  26.             }  
  27.             GetDetails();  
  28.         }  
  29.     } catch (Exception exc) {  
  30.         if (exc.GetType() == typeof(HttpException))  
  31.         {  
  32.             if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))  
  33.                 return;  
  34.         }  
  35.         Response.Write("<h2>Global Page Error</h2>\n");  
  36.         Response.Write("<p>" + exc.Message + "</p>\n");  
  37.         Response.Write("<p>" + exc.ToString() + "</p>\n");  
  38.         Response.Write("Return to the <a href='ImageBinary'>" + "</a>\n");  
  39.         ExceptionUtility.LogException(exc, "DefaultPage");  
  40.         ExceptionUtility.NotifySystemOps(exc);  
  41.         Server.ClearError();  
  42.         throw;  
  43.     }  
  44. }   
Step Four: This is a private function which will be called on page load event and after saving the data, and also to get update data.  
  1. private void GetDetails()   
  2. {  
  3.     using(DataContextLinqDataContext context = new DataContextLinqDataContext())   
  4.     {  
  5.         var Details = context.BinaryDatas.ToList();  
  6.         grdDetails.DataSource = Details;  
  7.         grdDetails.DataBind();  
  8.     }  
  9. }  
Step Five: Call GetDetails() Function on page load. 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!IsPostBack)  
  4.     {  
  5.         lblMessage.Text = string.Empty;  
  6.         GetDetails();  
  7.     }  
  8.  
Step Six: Show images into the gridview.
  1. <asp:GridView ID="grdDetails" runat="server" AutoGenerateColumns="False">  
  2.     <Columns>  
  3.         <asp:TemplateField HeaderText="Name">  
  4.             <ItemTemplate>  
  5.                 <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>  
  6.             </ItemTemplate>  
  7.         </asp:TemplateField>  
  8.         <asp:TemplateField HeaderText="Image">  
  9.             <ItemTemplate>  
  10.                 <asp:Image ID="Images" runat="server" Height="100px" ImageUrl='<%# "ImageHandler.ashx?id="+ Eval("Id") %>' />  
  11.             </ItemTemplate>  
  12.         </asp:TemplateField>  
  13.     </Columns>  
  14. </asp:GridView>   
Step Seven: In step Six I am calling to the httphandler. 
  1. public class ImageHandler1: IHttpHandler  
  2. {  
  3.     public void ProcessRequest(HttpContext context)  
  4.     {  
  5.         try  
  6.         {  
  7.             if (context.Request.QueryString["id"] != null)  
  8.             {  
  9.                 int Id = Convert.ToInt32(context.Request.QueryString["id"]);  
  10.                 context.Response.ContentType = "image/jpeg";  
  11.                 Stream strm = ShowImage(Id);  
  12.                 byte[] buffer = new byte[4096];  
  13.                 int byteSeq = strm.Read(buffer, 0, 4096);  
  14.                 while (byteSeq > 0) {  
  15.                     context.Response.OutputStream.Write(buffer, 0, byteSeq);  
  16.                     byteSeq = strm.Read(buffer, 0, 4096);  
  17.                 }  
  18.             } else  
  19.             {  
  20.                 throw new ArgumentException("No parameter specified");  
  21.             }  
  22.         } catch (Exception)  
  23.         {  
  24.             throw;  
  25.         }  
  26.     }  
  27.     public Stream ShowImage(int Id)  
  28.     {  
  29.         using(DataContextLinqDataContext context = new DataContextLinqDataContext())  
  30.         {  
  31.             var Details = context.BinaryDatas.SingleOrDefault(x => x.Id == Id);  
  32.             if (Details != null)  
  33.             {  
  34.                 byte[] array = (Details.ImageBinary as System.Data.Linq.Binary).ToArray();  
  35.                 try  
  36.                 {  
  37.                     return new MemoryStream((byte[]) array);  
  38.                 } catch  
  39.                 {  
  40.                     return null;  
  41.                 }  
  42.             } else   
  43.              {  
  44.                 return null;  
  45.             }  
  46.         }  
  47.     }  
  48.     public bool IsReusable  
  49.      {  
  50.         get   
  51.         {  
  52.             return false;  
  53.         }  
  54.     }  
  55. }  
Ebook Download
View all
Learn
View all