Goran Bibic

Goran Bibic

  • 475
  • 2.9k
  • 200.9k

Insert picture in datagrid c#

Apr 5 2018 4:09 PM
Problem line 13 and 38..data column type is image in sql
Or some recomandition to insert into and update pciture from picture box
 
  1. String cs = "Data Source=.\\SQLEXPRESS;Initial Catalog=bss;Integrated Security=True";  
  2.             if (string.IsNullOrEmpty(idTextBox.Text))  
  3.             {  
  4.   
  5.                 using (SqlConnection openCon = new SqlConnection(cs))  
  6.                 {  
  7.                     string saveStaff = "INSERT into dbo.poslovni_partneri (Ime, data) VALUES (@Ime,@data)";  
  8.   
  9.                     using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
  10.                     {  
  11.                         querySaveStaff.Connection = openCon;  
  12.                         querySaveStaff.Parameters.Add("@Ime", SqlDbType.VarChar, 255).Value = imeTextBox.Text;  
  13.                         querySaveStaff.Parameters.Add("@data", SqlDbType.image).Value = pictureBox.Text;  
  14.                          
  15.   
  16.                         openCon.Open();  
  17.                         querySaveStaff.ExecuteNonQuery();  
  18.                         openCon.Close();  
  19.   
  20.   
  21.                     }  
  22.   
  23.                 }  
  24.             }  
  25.   
  26.             else  
  27.             {  
  28.   
  29.                  
  30.                 using (SqlConnection openCon = new SqlConnection(cs))  
  31.                 {  
  32.                     string saveStaff = "UPDATE  dbo.poslovni_partneri SET Ime=@Ime, data=@data WHERE id=" + idTextBox.Text;   
  33.   
  34.                     using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
  35.                     {  
  36.                         querySaveStaff.Connection = openCon;  
  37.                         querySaveStaff.Parameters.Add("@Ime", SqlDbType.VarChar, 255).Value = imeTextBox.Text;  
  38.                         querySaveStaff.Parameters.Add("@data", SqlDbType.image).Value = pictureBox.Text;  
  39.                          
  40.                         openCon.Open();  
  41.                         querySaveStaff.ExecuteNonQuery();  
  42.                         MessageBox.Show("Uspješno ste izmenili stavku!""Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  43.                         openCon.Close();  
  44.                     }  
  45.                 }  
  46.             } 
 For upload picture in picture box use this code and it work...
 
  1. private void button4_Click(object sender, EventArgs e)  
  2.         {  
  3.             //Read image file  
  4.             using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false })  
  5.             {  
  6.                 if (ofd.ShowDialog() == DialogResult.OK)  
  7.                 {  
  8.                     fileName = ofd.FileName;  
  9.                     lblFilename.Text = fileName;  
  10.                     dataPictureBox1.Image = System.Drawing.Image.FromFile(fileName);  
  11.                 }  
  12.             }  
  13.         } 
 

Answers (5)