Save and retrieve image from sql c#

  • 35 Views
  • Last Post 3 weeks ago
  • Topic Is Solved
Stylus STYLUS posted this 3 weeks ago

Everything working fine i I have picture in picture box when insert into sql.

Complete code

private void button5_Click(object sender, EventArgs e)
        {
            byte[] img_arr = null;
            MemoryStream ms = new MemoryStream();
            dataPictureBox.Image.Save(ms, dataPictureBox.Image.RawFormat);
            img_arr = ms.GetBuffer();
            if (imeTextBox.ReadOnly == false)
            {
                if (string.IsNullOrEmpty(idTextBox.Text))
                {
                    using (SqlConnection openCon = new SqlConnection(cs))
                    {
                        string saveStaff = "declare @maxNo integer = 0 select @maxNo = isnull(max(redni_broj), 0) from [dbo].[roba_usluge]; Set @maxNo=@maxNo+1; INSERT into dbo.roba_usluge (redni_broj, ime, data) VALUES (@maxNo,@ime,@data)";
                        using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
                        {
                            querySaveStaff.Connection = openCon;
                            querySaveStaff.Parameters.Add("@ime", SqlDbType.VarChar, 255).Value = imeTextBox.Text;                            
                            querySaveStaff.Parameters.AddWithValue("@data", img_arr);
                            openCon.Open();
                            querySaveStaff.ExecuteNonQuery();
                            openCon.Close();
                        }
                    }
                }
                else
                {
                    using (SqlConnection openCon = new SqlConnection(cs))
                    {
                        string saveStaff = "UPDATE  dbo.roba_usluge SET redni_broj=@redni_broj, ime=@ime, data=@data  WHERE id= " + idTextBox.Text;
                        using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
                        {
                            querySaveStaff.Connection = openCon;
                            querySaveStaff.Parameters.Add("@redni_broj", SqlDbType.Int).Value = redni_brojTextBox.Text;
                            querySaveStaff.Parameters.Add("@ime", SqlDbType.VarChar, 255).Value = imeTextBox.Text;                          
                            querySaveStaff.Parameters.AddWithValue("@data", img_arr);
                            openCon.Open();
                            querySaveStaff.ExecuteNonQuery();
                            MessageBox.Show("Uspješno ste izmenili stavku!", "Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            openCon.Close();
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Dokument je već potvrđen! Unesite novi ili izmjenite postojeci!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

If is picturebox empty have error

Object reference not set to instanceof an object.  Line 152

Line 152 is:

dataPictureBox.Image.Save(ms, dataPictureBox.Image.RawFormat);

Reciving image from database is ok...

Some help please

Stylus STYLUS posted this 3 weeks ago

Solved

Replace this Line 
dataPictureBox.Image.Save(ms, dataPictureBox.Image.RawFormat);

To

bool isNullOrEmpty = dataPictureBox == null || dataPictureBox.Image == null; if(!isNullOrEmpty) dataPictureBox.Image.Save(ms, dataPictureBox.Image.RawFormat);

Close