hi
i am trying to retrieve picture in picture box from db but my code is not working.
INSERTING PICTURE IN DATABASE

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
      {
          button1.Hide();
          button2.Hide();
          comboBox1.Hide();
          textBox1.Show();
          foreach (object ab in barcodes)
          {
              string sto = ab.ToString();
              string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/sana/My Documents/EMARKETING.mdb";
              OleDbConnection conn = new OleDbConnection(str);
              conn.Open();
              string s = "insert into Student values('" + sto + "','" + textBox2.Text + "','" + textBox1.Text + "','"+pictureBox1.Image+"')";
              OleDbCommand cmd = new OleDbCommand(s, conn); 
              MemoryStream ms = new MemoryStream();
              pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
              Byte[] bytBLOBData = new Byte[ms.Length];
              ms.Position = 0;
              ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length));
              OleDbParameter prm = new OleDbParameter("@BLOBData", OleDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,0, 0, null, DataRowVersion.Current, bytBLOBData);
              cmd.Parameters.Add(prm);
             
              cmd.ExecuteNonQuery();
              conn.Close();
          }
         
      }

RETREIVING PICTURE FROM DATABASE IN PICTURE BOX

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {

          string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/sana/My Documents/EMARKETING.mdb";
          OleDbConnection conn = new OleDbConnection(str);
          OleDbCommand cmd = new OleDbCommand("select * from student where ID='" + comboBox1.SelectedItem.ToString() + "'", conn);
          DataSet ds = new DataSet();
          OleDbDataAdapter da = new OleDbDataAdapter(cmd.CommandText, conn);
          da.Fill(ds);
          foreach (DataRow dr in ds.Tables[0].Rows)
          {
              textBox2.Text = dr["EMAIL ADDRESS"].ToString();
              Byte[] byteBLOBData = new Byte[0];
             

             
              byteBLOBData = (Byte[])(ds.Tables[0].Rows[0]["IMAGE"]);
              MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
             
pictureBox1.Image = Image.FromStream(stmBLOBData);
              da.SelectCommand = conn.CreateCommand();
              da.SelectCommand.CommandText = "select * from student where ID='" + comboBox1.SelectedItem.ToString() + "'";
              da.Update(ds);
          }}

PLZ KINDLY TELL ME WHAT IS THE PROBLEM IN THE CODE WHY ITS NOT RETRIEVING PICTURE IN THE PICTURE BOX.
THANX.

Recommended Answers

All 2 Replies

..
foreach (DataRow dr in ds.Tables[0].Rows)
    {
      textBox2.Text = dr["EMAIL ADDRESS"].ToString();
      Byte[] byteBLOBData = (Byte[])(dr["IMAGE"]);
      MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
      pictureBox1.Image = Image.FromStream(stmBLOBData);
      ...
     }

hi
i am trying to retrieve picture in picture box from db but my code is not working.
INSERTING PICTURE IN DATABASE

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
      {
          button1.Hide();
          button2.Hide();
          comboBox1.Hide();
          textBox1.Show();
          foreach (object ab in barcodes)
          {
              string sto = ab.ToString();
              string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/sana/My Documents/EMARKETING.mdb";
              OleDbConnection conn = new OleDbConnection(str);
              conn.Open();
              string s = "insert into Student values('" + sto + "','" + textBox2.Text + "','" + textBox1.Text + "','"+pictureBox1.Image+"')";
              OleDbCommand cmd = new OleDbCommand(s, conn); 
              MemoryStream ms = new MemoryStream();
              pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
              Byte[] bytBLOBData = new Byte[ms.Length];
              ms.Position = 0;
              ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length));
              OleDbParameter prm = new OleDbParameter("@BLOBData", OleDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,0, 0, null, DataRowVersion.Current, bytBLOBData);
              cmd.Parameters.Add(prm);
             
              cmd.ExecuteNonQuery();
              conn.Close();
          }
         
      }

RETREIVING PICTURE FROM DATABASE IN PICTURE BOX

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {

          string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/sana/My Documents/EMARKETING.mdb";
          OleDbConnection conn = new OleDbConnection(str);
          OleDbCommand cmd = new OleDbCommand("select * from student where ID='" + comboBox1.SelectedItem.ToString() + "'", conn);
          DataSet ds = new DataSet();
          OleDbDataAdapter da = new OleDbDataAdapter(cmd.CommandText, conn);
          da.Fill(ds);
          foreach (DataRow dr in ds.Tables[0].Rows)
          {
              textBox2.Text = dr["EMAIL ADDRESS"].ToString();
              Byte[] byteBLOBData = new Byte[0];
             

             
              byteBLOBData = (Byte[])(ds.Tables[0].Rows[0]["IMAGE"]);
              MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
             
pictureBox1.Image = Image.FromStream(stmBLOBData);
              da.SelectCommand = conn.CreateCommand();
              da.SelectCommand.CommandText = "select * from student where ID='" + comboBox1.SelectedItem.ToString() + "'";
              da.Update(ds);
          }}

PLZ KINDLY TELL ME WHAT IS THE PROBLEM IN THE CODE WHY ITS NOT RETRIEVING PICTURE IN THE PICTURE BOX.
THANX.

byte[] ImageByte = null;

MemoryStream MemStream = null;

PictureBox PicBx = new PictureBox();

object OB;

string WorkingDirectory = Application.StartupPath + "\\";

connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + WorkingDirectory + "DBFile.mdb; Persist Security Info=True";

cnction = new OleDbConnection(connString);

cnction.Open();

int ImageID = 6;

sqlCommand = "SELECT ImageObject FROM ImagesTable WHERE ImageID = " + ImageID + "";

comm = new OleDbCommand(sqlCommand, cnction);

ImageByte = comm.ExecuteScalar();

MemStream = new MemoryStream(ImageByte);

PicBx.Image = Image.FromStream(MemStream);

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.