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.
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).
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
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <style type="text/css">
- .style1
- {
- width: 283px;
- }
- .style2
- {
- width: 247px;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
-
- <table style="width:100%;">
- <tr>
- <td class="style1">
- Image Name:</td>
- <td class="style2">
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td class="style1">
- Upload Your Image:</td>
- <td class="style2">
- <asp:FileUpload ID="FileUpload1" runat="server" />
- </td>
- <td>
- <asp:Label ID="Label1" runat="server"></asp:Label>
- </td>
- </tr>
- <tr>
- <td class="style1">
- </td>
- <td class="style2">
- <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />
- </td>
- <td>
- </td>
- </tr>
- </table>
-
- </div>
- </form>
- </body>
- </html>
Your design looks like the following image:
Code chamber Step 5: Open Fileupload_demo.aspx.cs file to write code for saving the image to database.
Fileupload_demo.aspx.cs - using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.IO;
- using System.Data;
- using System.Data.SqlClient;
-
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- if (FileUpload1.HasFile)
- {
- string str = FileUpload1.FileName;
- FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Upload/" + str));
- string Image = "~/Upload/" + str.ToString();
- string name = TextBox1.Text;
-
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("insert into tbl_data values(@name,@Image)", con);
- cmd.Parameters.AddWithValue("@name", name);
- cmd.Parameters.AddWithValue("Image", Image);
-
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
-
- Label1.Text = "Image Uploaded";
- Label1.ForeColor = System.Drawing.Color.ForestGreen;
-
- }
-
- else
- {
- Label1.Text = "Please Upload your Image";
- Label1.ForeColor = System.Drawing.Color.Red;
- }
- }
- }
Output chamber Hope you liked it. Have a good day. Thank you for reading.