Upload And Download Image In ASP.NET Using Database

I am going to create a new ASP.NET application called “ImageDemo.” Open Visual Studio 2015 and click New Project and from the New Project window, you need to choose “ASP.NET Application” and give a name to the application.

It will open new windows for “New ASP.NET Project,” here you can choose from different type of templates. For example, you can choose Web Forms or MVC or Web API, etc. But we just need to choose the Web Forms as in the following:

web form

In this web application, I will create a front end UI from where I can upload the image with some other details. For example, for this demo, I have taken the “WebSite” example where I will add the details like name, where it is, and profile image.

So, first I have just created the GUI from where I can upload the image as well as displaying the uploaded image in GridView. Yes, you are right, for displaying the image I am using the gridview with image control as in the following:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="ImageDemo.Employee" %>  
  2.   
  3.     <!DOCTYPE html>  
  4.   
  5.     <html xmlns="http://www.w3.org/1999/xhtml">  
  6.   
  7.     <head runat="server">  
  8.         <title></title>  
  9.     </head>  
  10.   
  11.     <body>  
  12.         <form id="form1" runat="server">  
  13.             <div style="border:2px solid black;">  
  14.                 <table>  
  15.                     <tr>  
  16.                         <td colspan="2">  
  17.                             <b>Enter WebSite Details</b>  
  18.                             <br /><br /><br />  
  19.   
  20.                         </td>  
  21.                     </tr>  
  22.                     <tr>  
  23.                         <td>Name  
  24.                         </td>  
  25.                         <td>  
  26.                             <asp:TextBox runat="server" ID="txtName"></asp:TextBox>  
  27.                         </td>  
  28.                     </tr>  
  29.                     <tr>  
  30.                         <td>Address  
  31.                         </td>  
  32.                         <td>  
  33.                             <asp:TextBox runat="server" ID="txtAddress"></asp:TextBox>  
  34.                         </td>  
  35.                     </tr>  
  36.                     <tr>  
  37.                         <td>Profile Image  
  38.                         </td>  
  39.                         <td>  
  40.                             <asp:FileUpload runat="server" ID="fupProfileimage" />  
  41.                         </td>  
  42.                     </tr>  
  43.                     <tr>  
  44.                         <td>  
  45.                         </td>  
  46.                         <td>  
  47.                             <asp:Button runat="server" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" />  
  48.                         </td>  
  49.                     </tr>  
  50.                     <tr>  
  51.                         <td colspan="2">  
  52.                             <asp:Label runat="server" ID="lblMessage" ForeColor="Red"></asp:Label>  
  53.                         </td>  
  54.                     </tr>  
  55.                 </table>  
  56.                 <br />  
  57.                 <br />  
  58.                 <asp:GridView runat="server" ID="grvEmployee" AutoGenerateColumns="false">  
  59.                     <Columns>  
  60.                         <asp:BoundField DataField="Name" HeaderText="Name" />  
  61.                         <asp:BoundField DataField="Address" HeaderText="Address" />  
  62.                         <asp:TemplateField HeaderText="Profile Image">  
  63.                             <ItemTemplate>  
  64.                                 <asp:Image runat="server" ID="imgProfile" ImageUrl='<%# Eval("Image") %>' Width="400px" Height="150px" />  
  65.                             </ItemTemplate>  
  66.                         </asp:TemplateField>  
  67.                     </Columns>  
  68.   
  69.                 </asp:GridView>  
  70.             </div>  
  71.         </form>  
  72.     </body>  
  73.   
  74.     </html>  
As you can see with above ASP.NET code, I have made two parts with this application: Where I can upload the WebSite details with images as well as you can see the latest uploaded images in grid view with their corresponding data.

I have just made database connectivity to upload the image path inthe  database rather than directly uploading the image in the database as binary format.

What I will do is, I will upload the image in the “Upload” folder and save the relevant path into the database. So at the time of displaying the data I will use the same path from database. This technique makes your site faster as compared to uploading the image as binary format in database.

upload

So, I am going to first create the connection string for database connectivity.
  1. <connectionStrings>  
  2.     <add name="MyConnection" connectionString="Data Source=my-computer;Initial Catalog=TestEmployee; user id=mukesh; password=adminadmin;" providerName="System.Data.SqlClient" />  
  3. </connectionStrings>  
Here's the code behind part for uploading and displaying the website details:.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Configuration;  
  10. using System.IO;  
  11.   
  12. namespace ImageDemo  
  13. {  
  14.     public partial class Employee: System.Web.UI.Page  
  15.     {  
  16.         string connectionstring = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;  
  17.         protected void Page_Load(object sender, EventArgs e)  
  18.         {  
  19.   
  20.             if (!IsPostBack)  
  21.             {  
  22.                 BindEmployeeData();  
  23.             }  
  24.         }  
  25.   
  26.   
  27.   
  28.         public void BindEmployeeData()   
  29.         {  
  30.             using(var con = new SqlConnection(connectionstring))  
  31.             {  
  32.                 string commandText = "select * from employees";  
  33.                 var com = new SqlCommand(commandText, con);  
  34.                 con.Open();  
  35.                 var adapter = new SqlDataAdapter(com);  
  36.                 DataSet ds = new DataSet();  
  37.                 adapter.Fill(ds);  
  38.                 grvEmployee.DataSource = ds;  
  39.                 grvEmployee.DataBind();  
  40.   
  41.             }  
  42.         }  
  43.   
  44.         protected void btnSubmit_Click(object sender, EventArgs e)  
  45.         {  
  46.             string empName = Convert.ToString(txtName.Text);  
  47.             string address = Convert.ToString(txtAddress.Text);  
  48.   
  49.   
  50.             if (fupProfileimage.HasFile)   
  51.             {  
  52.                 string profileImageName = fupProfileimage.PostedFile.FileName;  
  53.   
  54.                 string filePath = Server.MapPath(@ "~/upload/" + profileImageName);  
  55.                 fupProfileimage.SaveAs(filePath);  
  56.                 using(var con = new SqlConnection(connectionstring))  
  57.                 {  
  58.                     try   
  59.                     {  
  60.                         string commandText = "insert into employees (Name, address, image) values ('" + empName + "','" + address + "','~/upload/" + profileImageName + "')";  
  61.                         var com = new SqlCommand(commandText, con);  
  62.                         con.Open();  
  63.                         int result = com.ExecuteNonQuery();  
  64.                         if (result > 0)  
  65.                         {  
  66.                             BindEmployeeData();  
  67.                         } else  
  68.                         {  
  69.                             lblMessage.Text = "Failed to Add Employee Data !";  
  70.                         }  
  71.                     } catch (Exception ex)   
  72.                     {  
  73.   
  74.                     }  
  75.                 }  
  76.             } else  
  77.             {  
  78.                 lblMessage.Text = "choose any image first !";  
  79.             }  
  80.         }  
  81.     }  
  82. }  
You can see with the above code at the time of submitting the data, I am only saving the Name, Address, and Image Path into the database and saving the whole image into the database as binary format. I am just uploading the image in some other folder as well.

When you run the project and upload some images with other details then your application will look the same as below.

enter detail

Thanks for reading this article, hope you enjoyed it.
 
Read more articles on ASP.NET:

 


Similar Articles