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; namespace Defult { public partial class _Default : System.Web.UI.Page { //SqlConnection sqlcon = new SqlConnection("PDF_Files"); 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() { //SqlConnection sqlcon = new SqlConnection("PDF_Files"); SqlCommand sqlcmd = new SqlCommand("select * from Files"); SqlDataAdapter da = new SqlDataAdapter(sqlcmd); da.Fill(dt); if (dt.Rows.Count > 0) { GridView1.DataSource = dt; GridView1.DataBind(); } //sqlcon.Close(); } 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); SqlCommand sqlcmd = new SqlCommand("insert into Files(pdfname,pdfcontent) values (@pdfname,@pdfcontent)"); sqlcmd.Parameters.Add("@pdfname", FileUpload1.FileName); sqlcmd.Parameters.Add("@pdfcontent", pdfbytes); sqlcmd.ExecuteNonQuery(); //sqlcon.Close(); Label1.Text = "Successfully pdf upload to SQL Server database."; LoadGrid(); } } } }
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; namespace Defult { public partial class Default2 : System.Web.UI.Page { SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["PDF_Files"].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='@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); } } } } }
sqlcon
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)