none
How can I display image from database? RRS feed

  • Question

  • Hi Guys I have created a simple program which can store Code and Image so as you see that everything works well except on the left I want that when I write a Code and I want to see the image as well how can I do it ?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.IO;
    
    namespace WindowsFormsApplication13
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            SqlConnection con;
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
            string thepath;
            private void btnAdd_Click(object sender, EventArgs e)
            {
                DialogResult dRes= openFileDialog1.ShowDialog();
                if (dRes==DialogResult.OK)
                {
                    thepath = openFileDialog1.FileName;
                    picBAdd.ImageLocation = thepath;
                }
                else
                {
                    MessageBox.Show("Please select an image file", "Select Image", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }
            }
    
            private void btnSave_Click(object sender, EventArgs e)
            {
    
                byte[] myData = File.ReadAllBytes(thepath);
                con = new SqlConnection("Server=DELAROS;Database=Koban;Trusted_Connection=True;");
                SqlCommand cmd = new SqlCommand("INSERT INTO Picture(PictureKod,Image)VALUES(@pictureKod,@image)", con);
                cmd.Parameters.AddWithValue("@pictureKod",txtPictureKod.Text);
                cmd.Parameters.AddWithValue("@image",myData);
                con.Open();
                int effectedRows= cmd.ExecuteNonQuery();
                con.Close();
                if (effectedRows>0)
                {
                    MessageBox.Show("It has saved successfully");
                }
                else
                {
                    MessageBox.Show("It didn't save it");
                }
            }
    
            private void btnDelete_Click(object sender, EventArgs e)
            {
                con = new SqlConnection("Server=DELAROS;Database=Koban;Trusted_Connection=True;");
                SqlCommand cmd = new SqlCommand("DELETE FROM Picture WHERE PictureKod=@pictureKod",con);
                cmd.Parameters.AddWithValue("@pictureKod",txtPictureKod.Text);
                con.Open();
                int effectedRows = cmd.ExecuteNonQuery();
                con.Close();
                if (effectedRows>0)
                {
                    MessageBox.Show("It has deleted successfully");
                }
                else
                {
                    MessageBox.Show("It didn't delete it");
                }
            }
    
            private void btnChange_Click(object sender, EventArgs e)
            {
                byte[] myData = File.ReadAllBytes(thepath);
                con = new SqlConnection("Server=DELAROS;Database=Koban;Trusted_Connection=True;");
                SqlCommand cmd = new SqlCommand("UPDATE Picture SET Image=@image WHERE PictureKod=@pictureKod",con);
                cmd.Parameters.AddWithValue("@image",myData);
                cmd.Parameters.AddWithValue("@pictureKod",txtPictureKod.Text);
                con.Open();
                int effectedRows = cmd.ExecuteNonQuery();
                con.Close();
                if (effectedRows>0)
                {
                    MessageBox.Show("It has changed successfully");
                }
                else
                {
                    MessageBox.Show("It didn't change it");
                }
            }
    
            private void btnShow_Click(object sender, EventArgs e)
            {
                con = new SqlConnection("Server=DELAROS;Database=Koban;Trusted_Connection=True;");
                SqlCommand cmd = new SqlCommand("SELECT * FROM Picture WHERE PictureKod=@pictureKod ",con);
                cmd.Parameters.AddWithValue("@pictureKod",txtShow.Text);
                con.Open();
                con.Close();
            }
        }
    }
    

    Friday, February 15, 2013 5:57 PM

Answers

  • Hi, you should use PictureBox control to show image. Thats why you have to "convert" byte array (these are data inside database) to Image class:

    SqlCommand cmd = new SqlCommand("SELECT Image FROM Picture WHERE PictureKod = @pictureKod", con);
    cmd.Parameters.AddWithValue("@pictureKod", txtShow.Text);
    SqlDataReader reader = cmd.ExecuteReader();
    if(read.Read())
    {
       byte[] byteArr = (byte[])reader[0];
       MemoryStream ms = new MemoryStream(byteArr);
       Image img = Image.FromStream(ms);
       pictureBox1.Image = img;
    }

    Hope it helps,

    bye


    Mitja

    • Marked as answer by Zerhan Friday, February 15, 2013 7:00 PM
    Friday, February 15, 2013 6:37 PM

All replies

  • Hi, you should use PictureBox control to show image. Thats why you have to "convert" byte array (these are data inside database) to Image class:

    SqlCommand cmd = new SqlCommand("SELECT Image FROM Picture WHERE PictureKod = @pictureKod", con);
    cmd.Parameters.AddWithValue("@pictureKod", txtShow.Text);
    SqlDataReader reader = cmd.ExecuteReader();
    if(read.Read())
    {
       byte[] byteArr = (byte[])reader[0];
       MemoryStream ms = new MemoryStream(byteArr);
       Image img = Image.FromStream(ms);
       pictureBox1.Image = img;
    }

    Hope it helps,

    bye


    Mitja

    • Marked as answer by Zerhan Friday, February 15, 2013 7:00 PM
    Friday, February 15, 2013 6:37 PM
  • I have written the codes but It still gives an error like that

    Friday, February 15, 2013 6:58 PM
  • Is it your "Image" column in database type of Image? If so, you have to pass byte array to it. 

    I dont know what you did to pass (ot accept) integer type. Check it.


    Mitja

    Friday, February 15, 2013 7:21 PM