Join Dotnetcodes DotnetCodes.com is online Discussion Forum for Software professionals . It lets you find friends around the world and Create professional network that share similar interests as you. Get help on ur projects by industry specialists. Also get answers to all ur technical/placement related querries.Get an edge over others.
Already MemberClick here to login
ASP.net MVC Interview Questions Answers Interview Questions
Serial Number in SSRS Articles
Get Started Developing for Android Apps with Eclipse Articles
How to Print a Crystal Report direct to printer Articles
Razor View Engine Interview Questions Answers Interview Questions
.Net framework 4.0 Interview Questions Answers Interview Questions
SQL server reporting services Interview Questions (SSRS) part 1 Articles
Whats New in ASP.NET 4.0 Part 2 Articles
Difference between Encapsulation and Abstraction Interview Questions
Explaining SDLC -System Development Life Cycle Articles
SPIRE PDF Library Articles
Infosys Interview Questions Interview Questions
Html5 interview questions and answers Interview Questions
Dynamic Menu using HTML List Tag and CSS in ASP.Net Articles
SharePoint 2010 interview Questions Answers Interview Questions
Submit Articles | More Articles..

Uploading an image directly into the database and retreive in the PictureBox control in Windows Forms Application

Posted By: Jaishree On:9/21/2012 3:05:29 AM in:Articles Category:Windows Forms Hits:2699
In this article you will learn how you put an image directly into the database rather saving it in a file system, thats number one. Then second one was, how would you show the same image or any other image in the PictureBox control. 

Introduction

This is a step-by-step tutorial for uploading or adding an image directly into the database and viewing any uploaded image in the PictureBox control in Windows Forms Application.

In VB 6.0 there is no way of saving the binary large object (BLOB) into the database pro-grammatically, it only has the option to show the binary image from database to PictureBox. But over here in this article we will use MemoryStreamobject from System.IO base class to copy an image from the db to directly into the PictureBox control.

In order to perform this tutorial I have used following

- Microsoft Visual Studio 2010
- Microsoft SQL Server 2008 R2 (you can also use the MS Access for this)

Also I can assume that most of guys knows the Visual C# .NET Windows Forms applications, Binary Large Object(BLOB) in databases, ADO.NET data access.

Example

  1. As a first step we have to create a table for storing and retrieving our image(s). So let's create the table first using following query

    Note: For SQL Server users please select/create a database before you run this query

    Creating Table

    Creating Table

  2. Next step would be opening the Visual Studio and creating the Windows Forms application project

    Creating Project

    Creating Project

  3. Once the project is created the then default windows form will appear in the design mode. Over here you have to add to buttons and one picture box control using ToolBox toolbar. I have renamed the form to BLOBTestForm. After the design is complete it should ideally look like this

    Form Design

    Form Design

    Note: I have added a small label for displaying some messages. As of now it not visible, once we will the program and do any action it will show you the appropriate message.

  4. Once your form design is complete then in the code behind we have to put some code to make it work. We will start with using statements as follows
  5. As database is involved we have make the connection string as well. Following declaration we can put it inside thepublic class BLOBTestForm : System.Windows.Forms.Form class declaration, configure the connection string as per your settings.
  6. Now all necessary things are done, it's time to add some real code on the Button 1(File to Database) click event. This code will read the local image file using FileStream object and then convert it into byte array and pass it to the database using parameterized Command object. Change the file path as necessary.
  7. Now we have added code for file to database, now we will add the code for database to picture box control. You can add the following code to Button 2(Database to PictureBox) click event. This piece of code retrieves the image bytes from tblBLOB table in the database and puts it into the DataSet object, copies the recently uploaded image into byte array and then converts that into the MemoryStream array, finally we will load this MemoryStream into the image property of PictureBox control.
  8. Coding is done, let's compile and Run this application, to do so press F5.
  9. Once the our Form will appear you first click on File to Database button. So this will add the file into the database. It will look like this

    File to database

    File to database

  10. Once the label shows successful message then you can click on the Database to PictureBox button. This will load the recently added image from the database to PictureBox control. The final output will look like this

    Database to PictureBox

    Database to PictureBox



comments powered by Disqus
User Profile
Jaishree
Sr. Engineer
Delhi , India
Email :You must Log In to access the contact details.
Latest Post from :Jaishree
Function to Convert Text String to Title Case – Proper Case
View: 903 | Submitted on: 3/6/2013 11:23:44 PM
Uploading an image directly into the database and retreive in the PictureBox control in Windows Forms Application
View: 2699 | Submitted on: 9/21/2012 3:05:29 AM
Oracle Interview Questions part 2
View: 1827 | Submitted on: 1/7/2011 3:45:40 AM
Oracle Interview Questions part 1
View: 1836 | Submitted on: 1/7/2011 3:43:40 AM
Oracle Interview Questions
View: 1905 | Submitted on: 1/7/2011 3:36:49 AM
Submit Articles | All Post of This User..


Advertise About Us Private Policy Terms of use
All rights reserved to dotnetcodes. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
Best viewed at 1024 x 768 resolution with Internet Explorer 5.0 or Mozila Firefox 3.5 or Google Crome and higher