How to Upload Image to SQL Server?

In this article I have explained about how to upload student image into a SQL server table “image” field using C Sharp code with ASP.Net. This article is also include validations for file upload type and file size. If validation is true then only insert into the SQL Server Table otherwise not insert into the table.

Many people want to upload image file in the SQL server database. It is possible to store image file in the SQL Server image Table "Image" data type. The values are stored in this field as bytes i.e. convert image file in to bytes through code and then store binary values.

How to validate File upload Control using Java script Client side Validation?
Place one Custom control in client side and validate user input in the Java script tag. If the validation is not true then assign it validation false.

function ValidateFileUpload(Source,args) {
var filename = document.getElementById("FileUpload1").value;
//Check file is there or not
if (filename == '')
// There is no file selected
args.IsValid = false;
else {
var Extension = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
if (Extension == "jpg" || Extension == "jpeg") {
args.IsValid = true;
args.IsValid = false;

How to validate File upload Control using Server side validation?

string Ext = filename.Substring(filename.LastIndexOf('.') + 1).ToLower();

if (Ext == "jpg" || Ext == "jpeg")
args.IsValid = true;
args.IsValid = false;

How to convert Image file into Bytes and store in SQL Server Table field?

if (FileUpload1.HasFile)
Stream fs = default(Stream);
fs = FileUpload1.PostedFile.InputStream;
BinaryReader br1 = new BinaryReader(fs);
byte[] picture = br1.ReadBytes(FileUpload1.PostedFile.ContentLength);

//I create Table with only two fields for simply explain to you

//If you need to store File name then add field "Filename" and add this ImageName in query to store Database
ImageName = FileUpload1.FileName;

//If you need to store File Type then add field "FileType" add this ImageType in query to store Database
ImageType = FileUpload1.PostedFile.ContentType;

SqlCommand sqlcmd = new SqlCommand("insert into imgupload(sname,simage) values (@sname, @simage)", sqlcon);
sqlcmd.Parameters.Add("@sname", TextBox1.Text);
sqlcmd.Parameters.Add("@simage", picture);
TextBox1.Text = "";
Label1.Text = "Successfully values are iserted and Image Upload";

Source Code Detail:
Here with i have attached entire source code. Download it and try to upload image file with different combination i.e. check file size and file type validations in different browser.
Front End : ASP.NET
Code Behind : C#

I hope this Article is help to you for upload images into SQL Server database.


  • FileUpload_to_SQLServer (42817-301247-FileUploadEx.rar)
  • Comments

    No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: