We will save the name and path of the image to the database.

Initial chamber

Step 1: Open Visual Studio 2010 and create an empty website. Give it a suitable name: FileUpload_demo.

Step 2: In Solution Explorer you will get your empty website. Add a web form and SQL Database like the following:

For Web Form:

FileUpload_demo (Your Empty Website) - Right Click, Add New Item, then click Web Form. Name it Fileupload_demo.aspx.

Go back to your website in Solution Explorer and Add New Folder and Give name as - New Folder, then Upload.

folder

For SQL Server Database:

FileUpload_demo (Your Empty Website) - Right Click and Add New Item, then click SQL Server Database. Add Database inside the App_Data_folder.

Database chamber

Step 3: Go to your Database [Database.mdf], we will create a table - tbl_Data. Also, go to the database.mdf - Table and add new table, design your table like this:

Table - tbl_data ([Don’t forget to make ID, then Identity Specification as Yes).

table

Design chamber

Step 4: Open Fileupload_demo.aspx file to create our design, we will drag an HTML table, a FileUpload control, button, label and a textbox.

Fileupload_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <style type="text/css">
  7. .style1
  8. {
  9. width: 283px;
  10. }
  11. .style2
  12. {
  13. width: 247px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <form id="form1" runat="server">
  19. <div>
  20. <table style="width:100%;">
  21. <tr>
  22. <td class="style1">
  23. Image Name:</td>
  24. <td class="style2">
  25. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  26. </td>
  27. <td>
  28. </td>
  29. </tr>
  30. <tr>
  31. <td class="style1">
  32. Upload Your Image:</td>
  33. <td class="style2">
  34. <asp:FileUpload ID="FileUpload1" runat="server" />
  35. </td>
  36. <td>
  37. <asp:Label ID="Label1" runat="server"></asp:Label>
  38. </td>
  39. </tr>
  40. <tr>
  41. <td class="style1">
  42. </td>
  43. <td class="style2">
  44. <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />
  45. </td>
  46. <td>
  47. </td>
  48. </tr>
  49. </table>
  50. </div>
  51. </form>
  52. </body>
  53. </html>
Your design looks like the following image:

Design

Code chamber

Step 5: Open Fileupload_demo.aspx.cs file to write code for saving the image to database.

Fileupload_demo.aspx.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.IO;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. public partial class _Default : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. }
  15. protected void Button1_Click(object sender, EventArgs e)
  16. {
  17. if (FileUpload1.HasFile)
  18. {
  19. string str = FileUpload1.FileName;
  20. FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Upload/" + str));
  21. string Image = "~/Upload/" + str.ToString();
  22. string name = TextBox1.Text;
  23. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  24. SqlCommand cmd = new SqlCommand("insert into tbl_data values(@name,@Image)", con);
  25. cmd.Parameters.AddWithValue("@name", name);
  26. cmd.Parameters.AddWithValue("Image", Image);
  27. con.Open();
  28. cmd.ExecuteNonQuery();
  29. con.Close();
  30. Label1.Text = "Image Uploaded";
  31. Label1.ForeColor = System.Drawing.Color.ForestGreen;
  32. }
  33. else
  34. {
  35. Label1.Text = "Please Upload your Image";
  36. Label1.ForeColor = System.Drawing.Color.Red;
  37. }
  38. }
  39. }
Output chamber

upload image

Output

Hope you liked it. Have a good day. Thank you for reading.