How to store and Retrieve pdf files in sqlserver 2008 using asp .net


Follow the Code Which is Help How to Store /Retrieve pdf files in DataBase :

 Write This code  in Default.aspx page:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
   <form id="form2" runat="server">
    <div>
        <table cellpadding="0" cellspacing="0" align="center" width="600">
            <tr>
                <td height="30" colspan="2">
                    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                </td>
            </tr>
          
            <tr>
                <td height="30">
                    Select Your PDF File
                </td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td height="30" colspan="2" align="center">
                    <asp:Button ID="Button1" runat="server" Text="Upload" onclick="Button1_Click" />
                </td>
            </tr>
            <tr>
                <td height="30" colspan="2" align="center">
                    <asp:GridView ID="GridView1" runat="server" DataKeyNames="ID"
                        AutoGenerateColumns="false"
                        onselectedindexchanged="GridView1_SelectedIndexChanged">
                        <Columns>
                            <asp:TemplateField HeaderText="ID">
                                <ItemTemplate>
                                    <%#Eval("ID")%>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="PDF File Name">
                                <ItemTemplate>
                                    <asp:HyperLink Target="_blank" ID="HyperLink1" runat="server" NavigateUrl=' <%# "~/Default2.aspx?ID=" + Eval("ID", "{0:d}")%> '><%#Eval("PDFNAME")%></asp:HyperLink>                                  
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


Write This code  in Default.aspx.cs page

using System;

using System.Configuration;

using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
    SqlCommand sqlcmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadGrid();
        }
        Label1.Text = "";
    }
    void LoadGrid()
    {
        sqlcon.Open();
        sqlcmd = new SqlCommand("select * from PDFUPLOAD",sqlcon);
        da = new SqlDataAdapter(sqlcmd);
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        sqlcon.Close();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //PDF Upload Code to SQL SERVER database table
        if (FileUpload1.HasFile)
        {
            Stream fs = default(Stream);
            fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br1 = new BinaryReader(fs);
            byte[] pdfbytes = br1.ReadBytes(FileUpload1.PostedFile.ContentLength);
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand("insert into PDFUPLOAD(PDFNAME,PDFCONTENT) values (@pdfname,@pdf)", sqlcon);
            sqlcmd.Parameters.Add("@pdfname", FileUpload1.FileName);
            sqlcmd.Parameters.Add("@pdf", pdfbytes);
            sqlcmd.ExecuteNonQuery();
            sqlcon.Close();
            Label1.Text = "Successfully pdf upload to SQL Server database.";
            LoadGrid();
        }       
    }
}

Write This code  in Default2.aspx page:




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

Write This code  in Default2.aspx.cs page:





using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
    SqlCommand sqlcmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();
    string qstr;
    byte[] b = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            qstr = Request.QueryString["ID"];
            //Read PDF file from DATABASE table pdf field
            SqlCommand sqlcmd = new SqlCommand("Select PDFCONTENT from pdfupload where ID='" + qstr + "'", sqlcon); //use condition to retrieve particulatr PDF
            sqlcon.Open();
            da = new SqlDataAdapter(sqlcmd);
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                b = ((byte[])dt.Rows[0][0]);
                //Collect Bytes from database and write in Webpage
                Response.ContentType = "application/pdf";
                Response.BinaryWrite(b);
            }
        }
    }
}




4 comments:

  1. Columns name and datatype is not indicated here

    ReplyDelete
  2. You can use image data type,its work for me

    ReplyDelete
  3. Very Good article.

    To learn more on this Visit this article.

    http://jayeshsorathia.blogspot.com/2012/09/beginning-net-c-tips-get-or-retrive-stored-file-from-sql-server-database-table.html

    ReplyDelete
  4. sir u r code help me thanks for this article i try this store and retrieve pdf in asp.net ...sir i think when i click the pdf link in gridview it opens an another page containing iframe.....plz help me out for this conditons my project hangs for this....
    amit1574@in.com .... amit saini

    ReplyDelete