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.
- protected void btnSave_Click(object sender, EventArgs e)
- {
- try
- {
- using(DataContextLinqDataContext context = new DataContextLinqDataContext())
- {
- FileUpload imgUper = (FileUpload) fluImage;
- Byte[] imgByteUpper = null;
- if (imgUper.HasFile && imgUper.PostedFile != null)
- {
- HttpPostedFile File = fluImage.PostedFile;
- imgByteUpper = new Byte[File.ContentLength];
- File.InputStream.Read(imgByteUpper, 0, File.ContentLength);
- var binaryData = new BinaryData();
- binaryData.Name = txtName.Text.Trim();
- binaryData.ImageBinary = imgByteUpper;
- context.BinaryDatas.InsertOnSubmit(binaryData);
- context.SubmitChanges();
- txtName.Text = "";
- lblMessage.ForeColor = System.Drawing.Color.Green;
- lblMessage.Text = "Image Uploaded Successfully.";
- } else {
- txtName.Text = "";
- lblMessage.ForeColor = System.Drawing.Color.Red;
- lblMessage.Text = "Please Select Image First.";
- }
- GetDetails();
- }
- } catch (Exception exc) {
- if (exc.GetType() == typeof(HttpException))
- {
- if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
- return;
- }
- Response.Write("<h2>Global Page Error</h2>\n");
- Response.Write("<p>" + exc.Message + "</p>\n");
- Response.Write("<p>" + exc.ToString() + "</p>\n");
- Response.Write("Return to the <a href='ImageBinary'>" + "</a>\n");
- ExceptionUtility.LogException(exc, "DefaultPage");
- ExceptionUtility.NotifySystemOps(exc);
- Server.ClearError();
- throw;
- }
- }
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.
- private void GetDetails()
- {
- using(DataContextLinqDataContext context = new DataContextLinqDataContext())
- {
- var Details = context.BinaryDatas.ToList();
- grdDetails.DataSource = Details;
- grdDetails.DataBind();
- }
- }
Step Five: Call GetDetails() Function on page load.
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- lblMessage.Text = string.Empty;
- GetDetails();
- }
- }
Step Six: Show images into the gridview.
- <asp:GridView ID="grdDetails" runat="server" AutoGenerateColumns="False">
- <Columns>
- <asp:TemplateField HeaderText="Name">
- <ItemTemplate>
- <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Image">
- <ItemTemplate>
- <asp:Image ID="Images" runat="server" Height="100px" ImageUrl='<%# "ImageHandler.ashx?id="+ Eval("Id") %>' />
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
Step Seven: In step Six I am calling to the httphandler.

Join the conversation! Your thoughts help the community grow.