csharp

How to Upload and Save Files to Database in ASP.Net C#

In this blog, We will learn How to Upload and Save Files to Database in ASP.Net C#. We will save the name of the Image and full path of the Image into the Database.

In this article learn step by step How to Upload and Save Files to Database in ASP.Net C# and How to use FileUpload Control in ASP.Net C#.
Next article we will learn Display list of files from database in GridView and download files
For this blog i have created a Database and Simple Table with the three column “ImageId”,”ImageName” and “Image”.
Let’s Start How to Upload and Save Files to Database in ASP.Net C#.
[php]
Create database FileUpload_Demo

use FileUpload_Demo

create table tblFileUpload(
ImageId int primary key identity(1,1),
Imagename varchar(50),
Image varchar(50)
)
[/php]
Step 2:Create Stored procedure for Insert image name and Image into Database.
[php]
create proc sp_tblFileUpload_Insert
@Imagename varchar(50),
@Image varchar(50)
as
begin
insert into tblFileUpload(Imagename,Image) values(@Imagename,@Image)
end
[/php]
Step 3:Now Open Visual Studion and create the project as:
“Start” –> “All Programs”- “Microsoft Visual Studio 2012”.
“File” – “New Project” – “C#” – “Empty Project” (to avoid adding a master page).
Give the Project name such as “FileUpload_Demo” or another as you wish and specify the location.
Then right-click on Solution Explorer – “Add New Item” – “Default.aspx” page.
Step 4: Go back to your website in Solution Explorer and Add New Folder and Give name as – New Folder, then Uploads.

Step 5:Open Default.aspx page and write this code.
In “Default.aspx” page drag and drop one of an ASP.Net FileUpload control,a TextBox, a Button and a Label control. The Button has been assigned a Click event handler.
[php]
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FileUpload_Demo.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Image Name:</td>
<td>
<asp:TextBox ID="txtImgName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Upload Image:
</td>
<td>
<asp:FileUpload ID="fileUpload" runat="server" />
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="SaveImage" runat="server" Text="Save Image" OnClick="SaveImage_Click" /></td>
</tr>
<tr>
<td></td>
<td>
<asp:Label ID="lblmsg" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
[/php]

Step 4: Open web.config file and Establish a connection with database

Key Point:

  • In initial catalog write Database Name
  • In data source you can simply write (.) /your server name/(local)
  • Integrated security for Authentication

[php]
<connectionStrings>
<add name="DBCS" connectionString="data source=.;initial catalog=FileUpload_Demo;integrated security=true"/>
</connectionStrings>
[/php]

Step 5:Open Fileupload_demo.aspx.cs file to write code for saving the image to database.
Fileupload_demo.aspx.cs
[php]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//Need to Import this Namespaces
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace FileUpload_Demo
{
public partial class Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{

}

protected void SaveImage_Click(object sender, EventArgs e)
{
if (fileUpload.HasFile)
{
string Ext = "";
Ext = Path.GetExtension(fileUpload.PostedFile.FileName);
if (Ext.ToLower() == ".jpg" || Ext.ToLower() == ".jpeg" || Ext.ToLower() == ".png")
{
string FN = "";
FN = Path.GetFileName(fileUpload.PostedFile.FileName);
fileUpload.SaveAs(Server.MapPath("Uploads" + "\\" + FN));
string Image = "~/Uploads/" + FN.ToString();

con.Open();
SqlCommand cmd = new SqlCommand("sp_tblFileUpload_Insert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ImageName",txtImgName.Text);
cmd.Parameters.AddWithValue("@Image", Image);
cmd.ExecuteNonQuery();
con.Close();

lblmsg.Text = Path.GetFileName(fileUpload.FileName)+ "File Uploaded Successfully and save into the database";
}
else
{
lblmsg.Text = "Plzz select only .jpg||.jpeg||.png image";
}
}
else
{
lblmsg.Text = "Plzz select a Image!!";
}
}
}
}
[/php]
Screenshot describe the output.
output
Output
Output

Leave a Reply

Your email address will not be published.