Get Multiple Images From Database and Save Them on a Single Click in ASP.Net

Introduction

In a real scenario sometimes you have images in the database with their unique id and you need to provide them to the client for some reason, so you can then create a utility with simple code to save the images in the local computer.

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 with a "Button" in the design part of that page (.aspx).

  3. Write the code in the ".cs" file of that page to get multiple images from the database and save them into the computer 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

Now I will assume that I have a table that stored the data like:

table

If you want to learn how to store a single image in the database then click here  or multiple images in the database then click here.

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 as in the following:
    1. <asp:Button ID="Button1" runat="server" Text="Convert Byte to All Image " OnClick="Button1_Click" /> 
    click event

  3. Add a folder named "Images" to the website where you want to store the images from the database.

    Add a folder

    Note: I am assuming that Roll number is unique and is the name of the image.

Step 3

  1. Write the code to get the images from the database to save them on the click event of the button.
    1. protected void Button1_Click(object sender, EventArgs e)  
    2. {  
    3.     string sConn = ConfigurationManager.ConnectionStrings["con"].ToString();  
    4.     SqlConnection objConn = new SqlConnection(sConn);  
    5.     objConn.Open();  
    6.     string sTSQL = "select * from Images order by roll_no asc";  
    7.     SqlCommand objCmd = new SqlCommand(sTSQL, objConn);  
    8.     objCmd.CommandType = CommandType.Text;  
    9.     SqlDataAdapter adapter = new SqlDataAdapter();  
    10.     DataTable dt = new DataTable();  
    11.     adapter.SelectCommand = objCmd;  
    12.     adapter.Fill(dt);  
    13.     objConn.Close();  
    14.     for (int i = 0; i < dt.Rows.Count; i++)  
    15.     {  
    16.         string roll_no = dt.Rows[i]["Roll_no"].ToString();  
    17.         object img = dt.Rows[i]["img"];  
    18.         System.IO.File.WriteAllBytes(Server.MapPath("/Images/" + roll_no + ".jpg"), (byte[])img);  
    19.     }  
    20.     Response.Write("Images has been fetched");  
    21. }  
    code

Step 4

  1. Run the page that will be like:

    Run the page

  2. Click on the button and see the text "Images" has been fetched.

    Click on the button

    Result: As you can see the images have been saved in the folder and now you can use them anywhere.

    Result


Similar Articles