Saving images in database using ASP.NET FileUpload Control

You are currently viewing Saving images in database using ASP.NET FileUpload Control

Saving and displaying images in database tables is a common requirement in ASP.NET projects. There are two ways to store images in database either you store image URLs in database as normal string or you store image as binary data. In this tutorial I will show you how you can upload and save images directly in SQL Server database table as binary data.

Now let’s take a look at the form I am going to use, to upload images to the above table.

<form id="form1" runat="server">
    Product Name:
    <asp:TextBox id="txtProductName" runat="server" />
    <br />
    Product Image:
    <asp:FileUpload id="FileUpload1" runat="server" />
    <asp:Button id="btnSave" runat="server" Text="Save Product" onclick="btnSave_Click" />
    <br /><br />
    <asp:Label id="lblMessage" runat="server" />    
</form>

The above code looks pretty straightforward as it is just showing one TextBox, one ASP.NET 2.0 FileUpload control and a Button to Save Product Name and Image in database as shown in the following figure:

Following is the C# code for Save Product button click event.

protected void btnSave_Click(object sender, EventArgs e)
{ 
   if(FileUpload1.HasFile) 
   { 
      string productName = txtProductName.Text; 
      byte[] productImage = FileUpload1.FileBytes; 

      string constr = "Server=TestServer; Database=SampleDB; uid=sa; pwd=abc123"; 
      string query = "INSERT INTO Products(ProductName, ProductImage) VALUES(@ProductName, @ProductImage)"; 

      SqlConnection con = new SqlConnection(constr); 
      SqlCommand com = new SqlCommand(query, con); 

      com.Parameters.Add("@ProductName", SqlDbType.NVarChar).Value = productName; 
      com.Parameters.Add("@ProductImage", SqlDbType.VarBinary).Value = productImage; 

      con.Open(); 
      int result = com.ExecuteNonQuery(); 
      con.Close(); 

      if (result > 0) 
      { 
         lblMessage.Text = "Product Saved Successfully"; 
      } 
   } 
   else 
   { 
      lblMessage.Text = "Please Select Product Image File"; 
   } 
}

The above code is running simple SQL Insert query to store images in database table. FileUpload control HasFile property checks whether user has selected a file or not. FileBytes property returns the image file as the array of bytes.

READ ALSO:  Dynamically Add CSS in ASP.NET Content Page

If you want to upload the image file to a File System then you can also use FileUpload SaveAs method that requires the server path to upload the file. If you want to verify the file size and its type then you can use FileUpload.PostedFile.ContentLength and FileUpload.PostedFile.ContentType properties.

This Post Has 39 Comments

  1. Caleb TIMOTHY

    THANKS A LOT, AND GOD BLESS YOU

  2. Caleb TIMOTHY

    THANKS A LOT, AND GOD BLESS YOU

  3. farhad

    I search on internet for it
    and I very confused with a lot of complex code
    I lost my hope
    I obtain fantastic feel with your awesome help

  4. Ajrudin Baliyana

    Hi, Great job,

  5. Zaka

    Wow Man Amazing Simple and Brilliant

  6. Zaka

    Wow Man Amazing Simple and Brilliant

  7. Ajay

    thanks alot sir for helping me to my programe.

  8. venu

    Hello,Sir I want to save multiple values database so how can i save.

  9. venu

    Hello,Sir I want to save multiple values database so how can i save.

  10. Govind K

    Great…
    Its works at first try….
    Awesome…..

  11. Govind K

    Great…
    Its works at first try….
    Awesome…..

  12. Azad

    Thanks a lot! Great work!

  13. manoj sharma

    very nice , easy and clear solution,..

  14. manoj sharma

    very nice , easy and clear solution,..

  15. cholan

    First thanks for your post.. But when i saved an image and opened the database it shows 0px. what does it mean.is the image saved successfully or not…???? Plz leave a reply to my mail.

  16. Hardik Joshi

    Its goooooood

  17. Hardik Joshi

    Its goooooood

  18. Mawsar

    Thanks a lot.
    Could you please send me this Saving Image In Database in ASP.NET 4 VB Code.

    Thanks in Advance,

    Mawsar

  19. Niraj

    Nice article……. easy to understand…..
    thanks….

  20. joseph

    This is really helpful.

    What i did with my example i am storing files but not images,
    My question is how do i retrive this files from the database?
    otherwise storing files its working.

    Thanks alot

  21. jaswinder singh

    Sir,Please tell me about sqldbtype,Hasfile,File bytes

  22. jaswinder singh

    Sir,Please tell me about sqldbtype,Hasfile,File bytes

  23. viral

    after uploading how to display image in listview from varbinary datatype?

  24. HighFlying

    Thanks for posting this code, very helpful

  25. HighFlying

    Thanks for posting this code, very helpful

  26. Abhinaba Majumdar

    Thanxxxx…but how can I store image URLs in database as normal string ….Plzzzz Help…..

  27. Abhinaba Majumdar

    Thanxxxx…but how can I store image URLs in database as normal string ….Plzzzz Help…..

  28. dinesh

    fantastic job……

  29. dinesh

    fantastic job……

  30. Muhammad Jamil Akbar

    Awesom……… LIKE Always……
    Preetuy Coollllll Actuallyyyy….
    Keep it up…….
    Keep sharing your knowledge……….
    Bleave me u will go veryyyyy far….
    again great……………

  31. Arisha

    Sir!!! thanks for nice tutorial its really usefull!!!

  32. Arisha

    Sir!!! thanks for nice tutorial its really usefull!!!

  33. Abid Farooq Bhutta

    Thanks for useful information.

  34. Abid Farooq Bhutta

    Thanks for useful information.

  35. Fahad Siddiqui

    Thanks 4 all your tutorials, Sub aik sey bher ker aik hain, Sir , also include tutorial for windows application, PictureBox to sql Server.
    Once Again, Thank you very much.
    May Allah bless you (Amen)

  36. Furqan Khawaja

    What a great informative tutorial

  37. Sharjeel Hassan

    Sir, Great article please we need another article —
    Display BLOB (you have saved in this article) in DataList or ListView …. Product Catalog Like scenario

  38. Sharjeel Hassan

    Sir, Great article please we need another article —
    Display BLOB (you have saved in this article) in DataList or ListView …. Product Catalog Like scenario

  39. Furqan Khawaja

    What a great informative tutorial

Leave a Reply