In this article I will explain with an example, how to save (insert) PDF file into database in ASP.Net using C# and VB.Net.
Database
This article makes use of a table named tblFiles whose schema is defined as follows.
Note: You can download the database table SQL by clicking the download link below.
Adding ConnectionString to the Web.Config file
You need to add the Connection String in the ConnectionStrings section of the Web.Config file in the following way.
<?xml version="1.0?>
<configuration>
<connectionStrings>
<add name="constr" connectionString="Data Source=Mudassar-PC\SQL2019;Initial Catalog=dbFiles;Integrated Security=true" />
</connectionStrings>
</configuration>
HTML Markup
The HTML Markup contains of:
FileUpload– For uploading PDF file.
Button– For saving PDF 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" />
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Saving PDF file in Database in ASP.Net
When Upload button is clicked, the name, content type, and the InputStream of the PDF 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.
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.
Finally, a success message is displayed in the label control.
C#
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);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
lblMessage.Text = "File uploaded successfully.";
}
VB.Net
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
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Using
End Using
lblMessage.Text = "File uploaded successfully."
End Sub
Screenshots
The Form
Record after Insert in database
Downloads