Hello readers, today’s blog in about image uploading into and store our image into database into byte form. In my blog we divide the whole explanation into 3 parts. In First part we create a table into our database, in second part we design our form to upload image and in the end we discuss about C# code for image uploading. So, Without wasting time let’s start.

Create a database table for image uploading

Firstly, you have to create a simple table with two columns- “img” and “name”. First one for storing image and second one for name of the image. We write a simple SQL query for create a table name “tb_ank_upload_img” which containing above columns. The code for creating column is as follows:

create table tb_ank_upload_img(

img image,

name varchar(max) not null

);

Note that “img” type must be “image” and “name” will be “varchar”.

Design a form for image uploading

Now in the second part we design form. For designing the form you have to simple create a table with 2 columns and 3 rows. The code for the following code is as follows:

<table class="style1">

            <tr>

                <td>

                    <asp:Label ID="Label1" runat="server" Text="Select Image"></asp:Label>

                </td>

                <td>

                    <asp:FileUpload ID="fileImg" runat="server" />

                </td>

            </tr>

            <tr>

                <td>

                    <asp:Label ID="Label2" runat="server" Text="Image Name"></asp:Label>

                </td>

                <td>

                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>

                </td>

            </tr>

            <tr>

                <td>

                    <asp:Label ID="lblMsg" runat="server" Text="Label" Visible="False"></asp:Label>

                </td>

                <td>

                    <asp:Button ID="btnUpload" runat="server" onclick="btnUpload_Click"

                        Text="Upload" />

                </td>

            </tr>

        </table>     

    </div>

    </form>

Backend code for upload image in C#

Now you have to code on the click button. Firstly add your connection string for stablish the connection from the database. Then you have to code within the click button event. The code is as follows:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

public partial class ImageUpload : System.Web.UI.Page

{
    SqlConnection con = new SqlConnection(@"Data Source=XXXXXXXXX;Initial Catalog=abcd;User ID=sa;Password=abcd123");

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void btnUpload_Click(object sender, EventArgs e)

    {

        // check wheather any image is selected or not

        if (!fileImg.HasFile)

        {

            lblMsg.Visible = true;

            lblMsg.Text = "Please Select Image File";

        }

        else

        {

            int length = fileImg.PostedFile.ContentLength;

            byte[] pic = new byte[length];

            fileImg.PostedFile.InputStream.Read(pic, 0, length);

            try

            {
                //inserting uploaded image query

                SqlCommand com = new SqlCommand("insert into tb_ank_upload_img" + "(img,name) values (@img, @name)", con);

                com.Parameters.AddWithValue("@img", pic);

                com.Parameters.AddWithValue("@name", txtName.Text);

                con.Open();

                com.ExecuteNonQuery();

                lblMsg.Visible = true;

                lblMsg.Text = "Image Uploaded Successfully";  //after Successfully uploaded image

            }

            finally

            {

                con.Close();

            }

        }
 

    }

}

That’s all readers we have competed our task. Now your form is ready to upload images. Please upload some images and with different-different names and check your table into the database with select query.

If you are facing any issue within my code you can contact me at my Gmail (the Gmail ID is given in the contact page). Thank you.