In this article I will explain with an example, how to save (insert) Excel (.xls and .xlsx) document in ASP.Net using C# and VB.Net.
This article makes use of a table named tblFiles whose schema is defined as follows.
Save (Insert) Excel file in Database in ASP.Net
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
Adding ConnectionString to the Web.Config file
You need to add the Connection String in the ConnectionStrings section of the App.Config file in the following way.
<?xml version="1.0?>
        <add name="constr" connectionString="Data Source=Mudassar-PC\SQL2019;Initial Catalog=tblFiles;Integrated Security=true" />
HTML Markup
The HTML Markup contains of:
FileUpload – For uploading Excel file.
Button – For saving Excel file in database.
Label – For displaying success message.
<asp:FileUpload runat="server" ID="FileUpload1" />
<asp:Button runat="server" ID="btnUpload" Text="Upload" OnClick="Upload" />
<hr />
<asp:Label runat="server" ID="lblMessage" ForeColor="Green" />
You will need to import the following namespaces.
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Saving Excel file in Database in ASP.Net
When Upload button is clicked, the name, content type, and the InputStream of the Excel File are read from the FileUpload control.
The File Stream is converted to BYTE Array and is read using ReadBytes method of the BinaryReader class.
After that, the connection string is fetched from the Web.Config file and object of SqlConnection class is created using it.
Note: For more details on how to read Connection String from Web.Config file, please refer my article Read (Get) Connection String from Web.Config file in ASP.Net using C# and VB.Net.
Then, an object of SqlCommand class is created and the INSERT query is passed to it as parameter.
The values of the Name, ContentType and Data i.e. bytes of the uploaded File are added as parameter to SqlCommand object.
And the connection is opened and the ExecuteNonQuery function is executed.
Note: For more details on how to use ExecuteNonQuery function, please refer Understanding SqlCommand ExecuteNonQuery in C# and VB.Net.
Finally, a success message is displayed in the label control.
Note: If you want to learn about download Excel file from database, please refer my article Download Excel file from Database in ASP.Net.
protected void Upload(object sender, EventArgs e)
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string contentType = FileUpload1.PostedFile.ContentType;
    using (Stream fs = FileUpload1.PostedFile.InputStream)
        using (BinaryReader br = new BinaryReader(fs))
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
                string query = "INSERT INTO tblFiles VALUES (@Name, @ContentType, @Data)";
                using (SqlCommand cmd = new SqlCommand(query))
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@Name", filename);
                    cmd.Parameters.AddWithValue("@ContentType", contentType);
                    cmd.Parameters.AddWithValue("@Data", bytes);
    lblMessage.Text = "File uploaded successfully.";
Protected Sub Upload(sender As Object, e As EventArgs)
    Dim filename As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
    Dim contentType As String = FileUpload1.PostedFile.ContentType
    Using fs As Stream = FileUpload1.PostedFile.InputStream
        Using br As New BinaryReader(fs)
            Dim bytes As Byte() = br.ReadBytes(CType(fs.Length, Integer))
            Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
            Using con As New SqlConnection(constr)
                Dim query As String = "INSERT INTO tblFiles VALUES (@Name, @ContentType, @Data)"
                Using cmd As New SqlCommand(query)
                    cmd.Connection = con
                    cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename
                    cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contentType
                    cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes
                End Using
            End Using
        End Using
    End Using
    lblMessage.Text = "File uploaded successfully."
End Sub
The Form
Save (Insert) Excel file in Database in ASP.Net
Record after Insert in database
Save (Insert) Excel file in Database in ASP.Net