Forums » .NET » ASP.NET »

How to display a pdf file stored as a image type in DB


Posted Date: 27 Nov 2009      Posted By:: k.anantharengan     Member Level: Gold    Member Rank: 709     Points: 1   Responses: 1



I had uploaded a pdf file in my application and saved it as an image datatype in SQL server 2005 table.I had used the code below .I checked the DB it is getting the file in a binary format .My question is how to display the Pdf file stored in DB ?.if i select a pdf file stored in a table it should open in a new window.


public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void Button1_Click(object sender, EventArgs e)
{

Connection1 obj=new Connection1 ();

SqlConnection connection = obj.Connect();
try
{
int number = Convert.ToInt32(TextBox1.Text.ToString());

FileUpload img = (FileUpload)FileUpload1;
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
Response.Write("hello");
HttpPostedFile File =FileUpload1.PostedFile;
imgByte = new Byte[File.ContentLength];
int s= imgByte.GetLength(0);
Response.Write(s.ToString());
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
connection.Open();
string sql = "INSERT INTO pdfs1 VALUES('"+imgByte+"',"+number+")";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.ExecuteNonQuery ();

}
catch
{
// lblResult.Text = "There was an error";
}
finally
{
connection.Close();
}

}

Thanks in advance.




Responses

#448011    Author: Anuraj      Member Level: Gold      Member Rank: 105     Date: 27/Nov/2009   Rating: 2 out of 52 out of 5     Points: 2

Check this link

http://www.dotnetthoughts.net/2009/10/07/how-to-store-and-retrieve-files-from-sql-server-database/

Thanks
Anuraj

You can follow me in twitter : http://www.twitter.com/anuraj
http://www.dotnetthoughts.net



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Refresh login page after giving userid....and Operator '==' cannot be applied to operands
Previous : How to interchange the rows in sql
Return to Discussion Forum
Post New Message
Category:

Related Messages
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.