Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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);

            }




        }
Posted
Updated 3-Sep-18 23:41pm
v2
Comments
F-ES Sitecore 4-Sep-18 5:12am    
dataPictureBox is null or dataPictureBox.Image is null. From the code you've posted it is impossible to say which, or why, or what you need to do to fix it.
Richard Deeming 5-Sep-18 13:44pm    
declare @maxNo integer = 0 select @maxNo = isnull(max(redni_broj), 0) from [dbo].[roba_usluge]; Set @maxNo=@maxNo+1; ...

Don't do it like that. If multiple users are accessing your system at the same time, you will end up trying to insert the same primary key multiple times. Use an IDENTITY column instead.

string saveStaff = "UPDATE  dbo.roba_usluge SET redni_broj=@redni_broj, ime=@ime, data=@data  WHERE id= " + idTextBox.Text;

Once again, SQL Injection:
Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

You already know how to use parameters, since you're already using them in the same query. So why do you keep writing vulnerable code time and time again?!

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Solved Thank you

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);
 
Share this answer
 

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