Tuesday, February 23, 2010

Insert Image File Into SqlServer Database using asp.net and c#

Save An Image Into SQL Server 2005 Database





Normally, images are saved in the web server folder, not in the database; this is for larger file size category. In some cases like, in bank, for example, they scan the user signatures as image files and save that file into the database.


Controls used in this application
◦System.Web.UI.HtmlControls.HtmlInputFile
◦System.Web.UI.WebControls.TextBox
◦System.Web.UI.WebControls.Button
Namespaces used in this application:
using System.Data.SqlClient;
using System.Drawing;
using System.Data;
using System.IO;
using System.Drawing.Imaging;


Solution with Code
Use the HtmlInputFile class, which you can declare an instance of with an tag. The following example is a complete ASPX file that lets a user upload an image file and a comment describing the image. The OnUpload method writes the image and the comment to a table named Pictures in a SQL Server database.


public void OnUpload(Object sender, EventArgs e)
{

int len = Upload.PostedFile.ContentLength;
byte[] pic = new byte[len];
Upload.PostedFile.InputStream.Read (pic, 0, len);
// Insert the image and comment into the database
SqlConnection connection = new SqlConnection("Data Source=home;Initial Catalog=Image;Integrated Security=True");
try
{
connection.Open ();
SqlCommand cmd = new SqlCommand ("insert into Image " + "(Image, Text) values (@pic, @text)", connection);
cmd.Parameters.Add ("@pic", pic);
cmd.Parameters.Add ("@text", Comment.Text);
cmd.ExecuteNonQuery ();
}
finally
{
connection.Close ();
}

}

The above created function is called using the onClick property of a button.

How do I read an image from a database and display it in a Web page
?

Here, I used the web page to display the image and not any other control. The following is the code for displaying the image from the database.


protected void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
MemoryStream stream = new MemoryStream ();
SqlConnection connection = new SqlConnection("Data Source=home;Initial Catalog=Image;Integrated Security=True");
try
{
connection.Open ();
SqlCommand command = new SqlCommand ("select Image from Image", connection);
byte[] image = (byte[]) command.ExecuteScalar ();
stream.Write (image, 0, image.Length);
Bitmap bitmap = new Bitmap (stream);
Response.ContentType = "image/gif";
bitmap.Save (Response.OutputStream, ImageFormat.Gif);
}
finally
{
connection.Close ();
stream.Close ();
}
}

Monday, February 22, 2010

Simple code for Creating the pdf using itextsharp.dll

In this artical we will see how to create pdf file using itextsharp using c# and asp.net.



simple steps :

step 1 : First of all we have to download the iTextSharp.dll file
step 2 : After downloading the file add the reference of iTextSharp.dll to your project by right clicking on solution explorer.
step 3 : Now add the Namespace to your .cs


using iTextSharp.text;
using iTextSharp.text.pdf;
step 4 : Now one whatever click you wnat to generate the file you can write the below code. in this example i am generating the pdf file button click event.

//Create Document class object and set its size to letter and give space left,right.....

Document doc =new Document(iTextSharp.text.PageSize.LETTER,10,10,42,35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("c:\\Mahen.pdf", FileMode.Create));
doc.Open(); // open Document to write

//Now Write Some Content to it.
Paragraph paragraph = new Paragraph("My name is mahen");
Phrase pharse = new Phrase("And the team member of our project is simmy and ayesha");
Chunk chunk=new Chunk(" I am the student of M S University");
doc.Add(paragraph);
doc.Add(pharse);
doc.Add(Chunk);
doc.Close(); // close the Document

And you are done
Here is the sample pdf file





And below is the full code used inside button click event in C# example