Kavi suja

Kavi suja

  • NA
  • 298
  • 171.1k

Retrive Image from Mysql DB in VisualStudio

May 5 2013 2:06 AM
Hi,
   I have to store image in to MySql DB and Retrive it again from DB.Now,i've completed up to the image stored in DB.But I can't retrive image from DB.Can any one help me?


In Open Image Button:

OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.png; *.jpg; *.bmp)|*.png; *.jpg; *.bmp";
            DialogResult result = open.ShowDialog();
            if (result == DialogResult.Cancel)
                return;
            pictureBox1.Image = Image.FromFile(open.FileName);
            textBox1.Text = open.FileName;


In SaveImage Button:

MySqlConnection con = new MySqlConnection(@"Data Source=localhost;Initial Catalog=details;User Id=root;Password=MySqlAdmin;");
            //MySqlConnection con = new MySqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\Faraz Documents\\Course\\Visual Studio Projects\\save picture into datbase( PICTURE BOX )\\save picture into datbase( PICTURE BOX )\\image.mdf;Integrated Security=True;User Instance=True");
            con.Open();
            MemoryStream ms = new MemoryStream();// convert image into data field
            pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] pic_array = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(pic_array, 0, pic_array.Length);


            MySqlCommand cmd = new MySqlCommand("insert into imgsave (name,imagename,image) values(@name,@imagename,@image)", con);
            cmd.Parameters.AddWithValue("@name", textBox2.Text);
            cmd.Parameters.AddWithValue("@imagename", textBox1.Text);
            cmd.Parameters.AddWithValue("@image", pic_array);
            try
            {
                int res = cmd.ExecuteNonQuery();
                if (res >= 0)
                {
                    MessageBox.Show("uploded");
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                MessageBox.Show("error");
            }
            finally
            {
                con.Close();
                textBox1.Text = "";               
            }


In ViewImage Button:


MySqlConnection con = new MySqlConnection(@"Data Source=localhost;Initial Catalog=details;User Id=root;Password=MySqlAdmin;");
            try
            {
                string sql = "SELECT name,imagename,image FROM imgsave WHERE name=" + textBox2.Text + "";
                if (con.State != ConnectionState.Open)
                    con.Open();
               MySqlCommand command = new MySqlCommand(sql, con);
               MySqlDataReader reader = command.ExecuteReader();
                reader.Read();
                if (reader.HasRows)
                {
                    textBox2.Text = reader[0].ToString();
                    textBox1.Text = reader[1].ToString();
                    byte[] img = (byte[])(reader[2]);
                    if (img == null)
                        pictureBox1.Image = null;
                    else
                    {
                        MemoryStream ms = new MemoryStream(img);
                        pictureBox1.Image = Image.FromStream(ms);
                    }
                }
                else
                {
                    MessageBox.Show("This ID does not exist");
                }
                con.Close();
            }
            catch (Exception ex)
            {
                con.Close();
                MessageBox.Show(ex.Message);
            }

First I enter name I then upload image from system after that save into DB.Then again I enter the name,click View Button the ErrorMessage("This ID doesn't exist") shown.


Help me guys... Thanks in advance...








Answers (2)