1

Please pardon my knowledge on C# as I am very new to it,I am unable to insert a record in SQL and getting the below error while insert image to SQL. Error :

Object reference not set to an instance of an object.

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] img = ms.ToArray();

if (img == null)
{
    com.Parameters.AddWithValue("@img", null);
}
else
{
    com.Parameters.AddWithValue("@img", img);
}

If i select a image and insert it inserts successfully, but if i do not select an image it throws the above error. Please help!!

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
Ruchi
  • 146
  • 1
  • 8
  • in which line do you have the NullReferenceException ? and why are you so sure your pictureBox1 has an image, and that that image has a RawFormat? did you check that somewhere above in the code? – LongChalk Feb 24 '20 at 13:50
  • `AddWithValue` has to infer the parameter type based on the value passed. Since you are passing `null` it cannot infer the parameter type and throws an error. The best solution is to not use `AddWithValue` and expclicitly type your parameter. More reading: [Can we stop using AddWithValue() already?](https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/), [AddWithValue is Evil](https://www.dbdelta.com/addwithvalue-is-evil/) – GarethD Feb 24 '20 at 13:51
  • 1
    The exception message tells it is related to c# code, so that exception may occur here: `pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);` when pictureBox1.Image is null – Tomas Chabada Feb 24 '20 at 13:52
  • 1
    @GarethD Yes it should not be used, and yes it throws an error but not a NRE. It is _a parameterized query expects ....._ – Steve Feb 24 '20 at 13:52
  • @Steve, fair enough, I made an assumption without checking, I stand corrected and using `AddWithValue` is not the cause of the exception being thrown. I would now expect the problem line to be here then: `pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);`, if you don't select an image then `pictureBox.Image` will be null, meaning that you can't call the `Save()` method on the image. – GarethD Feb 24 '20 at 14:04
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – devsmn Feb 24 '20 at 14:36

3 Answers3

3

Change this:

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] img = ms.ToArray();
if (img == null)
{
     com.Parameters.AddWithValue("@img", null);
}
else
{
     com.Parameters.AddWithValue("@img", img);
}

To this

MemoryStream ms = new MemoryStream();
pictureBox1?.Image?.Save(ms, pictureBox1?.Image?.RawFormat);
byte[] img = ms.ToArray();
com.Parameters.AddWithValue("@img", (object)img ?? DBNull.Value);
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
2

Try testing that PictureBox1.Image exists before referencing it, like this:

        if (pictureBox1.Image != null)
        {
            MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
            byte[] img = ms.ToArray();
            com.Parameters.AddWithValue("@img", img);
        }
        else
        {
            com.Parameters.Add("@img", SqlDbType.VarBinary, 0).Value = DbNull.Value;
        }

EDITED to include comment by GarethD

Jon Roberts
  • 2,262
  • 2
  • 13
  • 17
  • 1
    `com.Parameters.AddWithValue("@img", null);` will still throw an error, it would be better to explicitly declare the parameter type, e.g. `com.Parameters.Add("@img", SqlDbType.VarBinary, 0).Value = DbNull.Value;` or `com.Parameters.Add("@img", SqlDbType.VarBinary, 0).Value = img;` (depending on which side of the `if` you are in) – GarethD Feb 24 '20 at 14:05
  • Thanks, @GarethD. I've updated my answer accordingly – Jon Roberts Feb 24 '20 at 14:13
  • Superb this works!! Thank you Everyone..Much Much Appreciated for all the help. – Ruchi Feb 24 '20 at 14:31
0

first, you need to make a new condition to check is file selected or not.here are code below:

if pictureBox1.hasfile == true 
{
                    MemoryStream ms = new MemoryStream();
                    pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                    byte[] img = ms.ToArray();

                    if (img == null)
                    {
                        com.Parameters.AddWithValue("@img", null);
                    }
                    else
                    {
                        com.Parameters.AddWithValue("@img", img);
                    }
}

because you are not selecting any file thats why it throwing exception on runtime

Sagar Kumar
  • 55
  • 1
  • 5