How to Save Multiple Images Into the Database on a Single Click in ASP.Net

Introduction

In real scenarios sometimes we have the images of users with some uniqueness and want to use those images in our application but we can't store so many images in the server due to some restriction, so we can store them in a database by creating a utility with simple code.

Like an Exam committee gets the exam candidate images with their roll numbers but now we want to access them in the other system depending on the roll numbers.

If you want to store only a single image then click here.

To explain this article I will use the following procedure:

  1. Create a table in the database to store the image and some other relevant and unique data like id or roll number by which I can fetch the image from the table.

  2. Create a website with a page wit a "Button" in the design part of that page (.aspx).

  3. Write the code in the ".cs" file of that page to save multiple images into the database on "button click" event.

The following is the detailed procedure for the preceding procedure.

Step 1

Create a table named "Images" that will store the following:

  • Roll number of a student
  • Image of a student
  • Date of image insertion
  1. CREATE TABLE dbo.Images(  
  2.    Roll_no varchar(12) primary key,  
  3.    img varbinary(max) ,  
  4.    img_date datetime  
  5. )  

Create a table

Step 2

  1. Create a new empty website named "Website1".

    empty Website

  2. Add the a button with click event to the default page named Default.aspx:
    1. <asp:Button ID="Button1" runat="server" Text="Convert All Images to Byte" OnClick="Button1_Click" /> 
    button

  3. Add a folder named "Images" to the website and put some images into it that you want to store in the database.

    Images

    Note: I am assuming that the image names are unique as roll numbers in this article.

Step 3

  1. Write the code to insert the image into the database on the click event of the button.
    1. protected void Button1_Click(object sender, EventArgs e)  
    2. {  
    3.     for (int i = 101; i <= 104; i++)  
    4.     {  
    5.         string filename = "~/Images/" + i.ToString() + ".jpg";  
    6.         byte[] imageByte = System.IO.File.ReadAllBytes(Server.MapPath(filename));  
    7.         //Insert the Data in the Table    
    8.         using (SqlConnection connection = new SqlConnection())  
    9.         {  
    10.             connection.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ToString();  
    11.             connection.Open();  
    12.             SqlCommand cmd = new SqlCommand();  
    13.             cmd.Connection = connection;  
    14.             string commandText = "Insert into Images values (@Rollno,@image,getdate())";  
    15.             cmd.CommandText = commandText;  
    16.             cmd.CommandType = CommandType.Text;  
    17.             cmd.Parameters.Add("@image", SqlDbType.VarBinary);  
    18.             cmd.Parameters["@image"].Value = imageByte;  
    19.             cmd.Parameters.Add("@Rollno", SqlDbType.VarChar);  
    20.             cmd.Parameters["@Rollno"].Value = i.ToString();  
    21.             cmd.ExecuteNonQuery();  
    22.             cmd.Dispose();  
    23.             connection.Close();  
    24.         }  
    25.     }  
    26.     Response.Write("Data has been Added");  

    code

Step 4

  1. Run the page that will be like:

    Run the page

  2. Click on the button and see the text "Data has been Added".

    Data

    Result: As you can see the images have been saved in the database in binary format and now you can use them anywhere in your project.

    Result