本文介绍了从SQL C#保存和检​​索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Complete code down





我尝试过:





What I have tried:

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 već potvrđen! Unesite novi ili izmjenite postojeci!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }




        }

推荐答案


Replace this Line 
dataPictureBox.Image.Save(ms, dataPictureBox.Image.RawFormat);
To
bool isNullOrEmpty = dataPictureBox == null || dataPictureBox.Image == null;
if(!isNullOrEmpty)
dataPictureBox.Image.Save(ms, dataPictureBox.Image.RawFormat);


这篇关于从SQL C#保存和检​​索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:19