Monday, 30 June 2014

Retrieving Image from database in window application


                                                                                                                                               Previous..
                                                                                                                                                    Next..
Here i will explain How to Retrieving Image from database in window application.

Step(1): Form 
drag and down  a combo box when i select a name from combo box then all fields will show with his image.
Step(2): Code
Click on combo box a write this code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

String gender;                                                              //we have to define this
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
              SqlConnection con = new SqlConnection(str);
                String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
        string query = "select * from DataBaseTable Where name = '" + comboBox1.text+"';

sqlCommand cmd = new sqlCommand ( query,con);
sqlDatareader dbr = new sqlDatareader;

try
{
con.open();
dbr.cmd.ExecuteReder();while(dbr.read())
{string sEid = dbr.GetInt32("Eid").toString();
string sName = dbr.GetInt32("name");
string sSurname = dbr.GetInt32("Sname");
string sAge = dbr.GetInt32("Age").toString();Eid.text =    sEid ;
Firstname.text=     sName; 
Surname.text =     sSurname; 
Age.text=              sAge ;

byte[]  imageBt = (byte[])(dbr[ " image"]);
if( imageBt == null ) 
{
pictureBox.Image = null;
}

else
{
memoryStream  mstream =  new memoryStream(   imageBt );

pictureBox.Image = system.Drawing.Image.FromStream( mstream );
}
}
}

Catch( Exception ex)
{

messageBox.show(ex.message);


}


Step(3): Output

Run your application and retrieve the data. 
                                                                                                                                               Previous..
                                                                                                                                                    Next..


No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...