Goran Bibic

Goran Bibic

  • 455
  • 2.9k
  • 178k

Update picture c#

May 27 2018 1:00 PM
Insert picture working fine, I have problem with update...some help?
 
  1. string fileName; 
 Upload image
 
  1. private void buttonupload_Click(object sender, EventArgs e)  
  2.        {  
  3.            //Read image file  
  4.            using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg|BMP|*.bmp|PNG|*.png|All Files|*.*", ValidateNames = true, Multiselect = false })  
  5.            {  
  6.                if (ofd.ShowDialog() == DialogResult.OK)  
  7.                {  
  8.                    fileName = ofd.FileName;  
  9.   
  10.                    pictureBox1.Image = System.Drawing.Image.FromFile(fileName);  
  11.                }  
  12.            }  
  13.        } 
  1.  private void buttonsave_Click(object sender, EventArgs e)  
  2.         {  
  3.   
  4.             byte[] img_arr = null;  
  5.             MemoryStream ms = new MemoryStream();  
  6.             pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);  
  7.             img_arr = ms.GetBuffer();  
  8.   
  9.  using (SqlConnection openCon = new SqlConnection(cs))  
  10.                 {  
  11.                     string saveStaff = "INSERT INTO dbo.radnici ( data) VALUES (@data)";  
  12.   
  13.                     using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
  14.                     {  
  15.                      querySaveStaff.Connection = openCon;  
  16.                      querySaveStaff.Parameters.AddWithValue("@data", img_arr);  
  17.   
  18.   
  19.                             openCon.Open();  
  20.                         querySaveStaff.ExecuteNonQuery();  
  21.                         openCon.Close();  
  22.   
  23.                     }  
  24.  }  
  25.   
  26.  else  
  27.             {  
  28.   
  29.                 using (SqlConnection openCon = new SqlConnection(cs))  
  30.                 {  
  31.                    string updateStaff = "UPDATE dbo.radnici SET data=@data  WHERE id= " + idTextBox.Text;  
  32.   
  33.                     using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
  34.                     {  
  35.                         queryupdateStaff.Connection = openCon;                            
  36.                             queryupdateStaff.Parameters.AddWithValue("@data", img_arr);  
  37.   
  38.   
  39.                             openCon.Open();  
  40.                         queryupdateStaff.ExecuteNonQuery();  
  41.                         MessageBox.Show("UspjeĆĄno ste izmenili stavku!""Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  42.                         openCon.Close();  
  43.   
  44.                     }  
  45.   
  46.                 }  
  47.   
  48.             }          
 

Answers (1)