Goran Bibic

Goran Bibic

  • 484
  • 2.9k
  • 196k

Save and retrieve image from sql c#

Sep 4 2018 3:51 AM
Everything working fine i I have picture in picture box when insert into sql.
 

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

 
 
 
  1. private void button5_Click(object sender, EventArgs e)  
  2.         {  
  3.             byte[] img_arr = null;  
  4.             MemoryStream ms = new MemoryStream();  
  5.             dataPictureBox.Image.Save(ms, dataPictureBox.Image.RawFormat);  
  6.             img_arr = ms.GetBuffer();  
  7.   
  8.             if (imeTextBox.ReadOnly == false)  
  9.             {  
  10.   
  11.   
  12.   
  13.                 if (string.IsNullOrEmpty(idTextBox.Text))  
  14.                 {  
  15.   
  16.                     using (SqlConnection openCon = new SqlConnection(cs))  
  17.                     {  
  18.                         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)";  
  19.   
  20.                         using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
  21.                         {  
  22.   
  23.                             querySaveStaff.Connection = openCon;  
  24.   
  25.                             querySaveStaff.Parameters.Add("@ime", SqlDbType.VarChar, 255).Value = imeTextBox.Text;  
  26.   
  27.                             querySaveStaff.Parameters.AddWithValue("@data", img_arr);  
  28.   
  29.   
  30.                             openCon.Open();  
  31.                             querySaveStaff.ExecuteNonQuery();  
  32.                             openCon.Close();  
  33.   
  34.                         }  
  35.   
  36.                     }  
  37.                 }  
  38.                 else  
  39.                 {  
  40.   
  41.                     using (SqlConnection openCon = new SqlConnection(cs))  
  42.                     {  
  43.                         string saveStaff = "UPDATE  dbo.roba_usluge SET redni_broj=@redni_broj, ime=@ime, data=@data  WHERE id= " + idTextBox.Text;  
  44.   
  45.   
  46.   
  47.                         using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
  48.                         {  
  49.                             querySaveStaff.Connection = openCon;  
  50.                             querySaveStaff.Parameters.Add("@redni_broj", SqlDbType.Int).Value = redni_brojTextBox.Text;  
  51.                             querySaveStaff.Parameters.Add("@ime", SqlDbType.VarChar, 255).Value = imeTextBox.Text;                             
  52.   
  53.                             querySaveStaff.Parameters.AddWithValue("@data", img_arr);  
  54.   
  55.   
  56.   
  57.                             openCon.Open();  
  58.                             querySaveStaff.ExecuteNonQuery();  
  59.                             MessageBox.Show("Uspješno ste izmenili stavku!""Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  60.                             openCon.Close();  
  61.                         }  
  62.                     }  
  63.                 }  
  64.   
  65.             }  
  66.             else  
  67.             {  
  68.   
  69.                 MessageBox.Show("Dokument je vec potvrden! Unesite novi ili izmjenite postojeci!""Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  70.   
  71.             }  
  72.   
  73.   
  74.   
  75.   
  76.         } 
 

Answers (2)