Hi everyone,
In my database table I created a column whose data type is Image. I have successfully inserted a image from PictureBox control in binary format in that table field. Now I would like to retrieve that image stored in binary format and would like to show this in a PictureBox. Therefore i have written code but when i run it show problems at the following line.
pictureBox1.Image = Image.FromStream(stmBLOBData,false,false);
I am giving all the code written for the work. Is there any one who can help me. I need the solution very urgently as this is my university project work.
Thanks
Nasir
C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//Initialize Connection string
String strCn = "server=PC1\\SQLEXPRESS; integrated security=true; database=DB_Payroll";
SqlConnection cn = new SqlConnection(strCn);
cn.Open();
//Retrieve BLOB from database into DataSet.
SqlCommand cmd = new SqlCommand("select Employee_ID, Photo from Employee_Info where employee_id='E004'", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Employee_Info");
int c = ds.Tables["Employee_Info"].Rows.Count;
MessageBox.Show(c.ToString());
if (c > 0)
{ //BLOB is read into Byte array, then used to construct MemoryStream,
//then passed to PictureBox.
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["Employee_Info"].Rows[c - 1]["Photo"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
if (stmBLOBData != null)
{
pictureBox1.Image = Image.FromStream(stmBLOBData,false,false);
}
}
cn.Close();
}
catch (ArgumentException ae)
{
MessageBox.Show(ae.Message);
}
}
}
}
pictureBox1.Image = Image.FromStream(stmBLOBData,false,false);