Hi phonghue,
Check this example. Now please take its reference and correct your code.
HTML
<div>
<asp:FileUpload ID="FileUpload1" runat="server" accept="application/pdf" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<hr />
<asp:Label ID="lblMessage" ForeColor="Green" runat="server" />
</div>
<div>
<asp:GridView ID="gvImages" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="name" HeaderText="Image Id" />
</Columns>
</asp:GridView>
</div>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindImageGrid();
}
}
private void BindImageGrid()
{
string connString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tblFiles", conn))
{
DataTable dt = new DataTable();
sda.Fill(dt);
gvImages.DataSource = dt;
gvImages.DataBind();
}
}
}
protected void Upload(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string extension = Path.GetExtension(filename);
string contentType = FileUpload1.PostedFile.ContentType;
// Check file extension.
if (extension.ToLower() == ".pdf")
{
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes(Convert.ToInt32(fs.Length));
string connString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connString))
{
string query = "INSERT INTO tblFiles (Name, Contenttype, data) VALUES (@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = conn;
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contentType;
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
}
lblMessage.Text = "File uploaded successfully.";
lblMessage.ForeColor = Color.Green;
BindImageGrid();
}
else
{
lblMessage.Text = "Select only pdf file.";
lblMessage.ForeColor = Color.Red;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindImageGrid()
End If
End Sub
Private Sub BindImageGrid()
Dim connString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As SqlConnection = New SqlConnection(connString)
Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM tblFiles", conn)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
gvImages.DataSource = dt
gvImages.DataBind()
End Using
End Using
End Sub
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
Dim filename As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim extension As String = Path.GetExtension(filename)
Dim contentType As String = FileUpload1.PostedFile.ContentType
' Check file extension.
If extension.ToLower() = ".pdf" Then
Using fs As Stream = FileUpload1.PostedFile.InputStream
Using br As BinaryReader = New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))
Dim connString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As SqlConnection = New SqlConnection(connString)
Dim query As String = "INSERT INTO tblFiles (Name, Contenttype, data) VALUES (@Name, @ContentType, @Data)"
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Connection = conn
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contentType
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
End Using
End Using
lblMessage.Text = "File uploaded successfully."
lblMessage.ForeColor = Color.Green
BindImageGrid()
Else
lblMessage.Text = "Select only pdf file."
lblMessage.ForeColor = Color.Red
End If
End Sub