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
- 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 vec potvrden! Unesite novi ili izmjenite postojeci!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
Jenith ThakkarPosted Sep 4, 2018, 4:20 AM
dataPictureBox.Image.Save(ms, dataPictureBox.Image.RawFormat);
To
and try
If it is working for you then please accept my answer
Goran BibicPosted Sep 4, 2018, 4:39 AM