Wednesday, 13 June 2012

ASP.NET & C# - FILE UPLOAD TO SQL SERVER DATABASE accepts two types of file upload operations :

  1. Uploading a file to the file system 
  2. Uploading file to the database on the server 
  • Uploading a file to the file system:
To save the file to the file system ,the windows account associated with ASP.NET page must have sufficient permission to save the file .For windows 2003 and windows 2008 the page performs in the security context of the NETWORK SERVICE account.


To enable ASP.NET framework to save an uploaded file , you need to:
 right click the folder within windows explorer -> select the security tab->provide either the NETWORK SERVICE or ASP.NET account->write permission for the folder.

  • Uploading file to the database on the server

To upload a file to the SQL server database table in we use the FileUpload control .Retrieving a file from a database table on the server places more stress on the server.


  • Avoid file system permission issues 
  • Saving a file to database table enable you to more easily back up your information

FileUpload Control:

Supports the following properties:

  • Enabled-Enables you to disable the FileUpload Control.
  • FileBytes-Enables you to get the uploaded file contents as a byte array.
  • FileContent-Enables you to get the uploaded file contents as a stream.
  • FileName- Enables you to get the name of the uploaded file
  • HasFile-Returns True when the file has been uploaded.
  • PostedFile- Enables you to get the uploaded file wrapped in the HttpPostedFile
  • Focus- Enables you to shift the form focus to the  FileUploaded Control
  • SaveAs- Enables you to save the uploaded file to the file system.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <br />
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <br />
        File Name:
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <br />
    Author: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
    <asp:Button ID="Upload" Text="Upload Now" runat="server" onclick="Upload_Click" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
    SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDBFileName=|DataDirectory|\\Database.mdf;Integrated Security=true;user instance=true;");
    protected void Page_Load(object sender, EventArgs e)

    protected void Upload_Click(object sender, EventArgs e)
            byte[] img=new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile myfile=FileUpload1.PostedFile;
            SqlCommand cmd=new SqlCommand("Insert into Table1(author,filecontent) values(@filename,@file)",conn);
            SqlParameter flname=new SqlParameter("@filename",System.Data.SqlDbType.VarChar,25);
            SqlParameter fl=new SqlParameter("@file",System.Data.SqlDbType.Binary);
            Label1.Text="File Uploaded";

            Label1.Text="Can't upload file";


1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.