Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Insert picture working fine, I have problem with update...some help?


Error is(when update)
Object reference not set to an instance of an object


What I have tried:

string fileName;  


private void buttonupload_Click(object sender, EventArgs e)  
       {  
           //Read image file  
           using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg|BMP|*.bmp|PNG|*.png|All Files|*.*", ValidateNames = true, Multiselect = false })  
           {  
               if (ofd.ShowDialog() == DialogResult.OK)  
               {  
                   fileName = ofd.FileName;  
  
                   pictureBox1.Image = System.Drawing.Image.FromFile(fileName);  
               }  
           }  
       }  


private void buttonsave_Click(object sender, EventArgs e)  
        {  
  
            byte[] img_arr = null;  
            MemoryStream ms = new MemoryStream();  
            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);  
            img_arr = ms.GetBuffer();  
  
 using (SqlConnection openCon = new SqlConnection(cs))  
                {  
                    string saveStaff = "INSERT INTO dbo.radnici ( data) VALUES (@data)";  
  
                    using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
                    {  
                     querySaveStaff.Connection = openCon;  
                     querySaveStaff.Parameters.AddWithValue("@data", img_arr);  
  
  
                            openCon.Open();  
                        querySaveStaff.ExecuteNonQuery();  
                        openCon.Close();  
  
                    }  
 }  
  
 else  
            {  
  
                using (SqlConnection openCon = new SqlConnection(cs))  
                {  
                   string updateStaff = "UPDATE dbo.radnici SET data=@data  WHERE id= " + idTextBox.Text;  
  
                    using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
                    {  
                        queryupdateStaff.Connection = openCon;                            
                            queryupdateStaff.Parameters.AddWithValue("@data", img_arr);  
  
  
                            openCon.Open();  
                        queryupdateStaff.ExecuteNonQuery();  
                        MessageBox.Show("Uspješno ste izmenili stavku!", "Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);  
                        openCon.Close();  
  
                    }  
  
                }  
  
            }      
Posted
Updated 27-May-18 17:34pm

1 solution

C#
using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))


this line i this you should use
updateStaff
inserted of
saveStaff
in query parameter while updating
 
Share this answer
 
Comments
Goran Bibic 28-May-18 2:45am    
I solve that I have problem ,,Object reference not set to an instance of an object,,

Every time when saving (save or update) I must upload picture...some solution if picturebox emtpy ore somesthing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900