17

I get this exception when I try to insert a DBNull.Value into a nullable varbinary(max) field:

Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

This is my code:

insertCMD.Parameters.AddWithValue("@ErrorScreenshot", SqlDbType.VarBinary).Value = DBNull.Value;

I know there exist duplicate questions on SO, but I do NOT use any String like the others do.

What do I wrong?

UPDATE:

using (var insertCMD = new SqlCommand("INSERT INTO TestplanTeststep (TeststepId,TestplanId,CreatedAt,ErrorText,ErrorScreenshot,TestState) VALUES (@TeststepId, @TestplanId,@CreatedAt,@ErrorText,@ErrorScreenshot,@TestState)", con))
{
 var p1 = insertCMD.Parameters.Add("@TeststepId", SqlDbType.Int);
 var p2 = insertCMD.Parameters.Add("@CreatedAt", SqlDbType.DateTime);
 insertCMD.Parameters.AddWithValue("@TestplanId", testplan.Id);
 insertCMD.Parameters.AddWithValue("@ErrorText", (object) DBNull.Value);
 insertCMD.Parameters.AddWithValue("@ErrorScreenshot", (object) DBNull.Value);
 insertCMD.Parameters.AddWithValue("@TestState", (int)Teststep.TeststepTestState.Untested);
        
       foreach (Teststep step in teststeps)
        {
           p1.Value = step.Id;
           p2.Value = step.CreatedAt;
           insertCMD.ExecuteNonQuery();
        }
     }
ahsteele
  • 26,243
  • 28
  • 134
  • 248
Pascal
  • 12,265
  • 25
  • 103
  • 195

3 Answers3

25

I had the same problem while insertion DBNull.Value for a Varbinary(Max) column. After Googling I found a solution that may help you as well:

You need to set size -1 which means Max length for varbinary column when adding your sql parameter:

this.cmd.Parameters.Add("@Photo", SqlDbType.VarBinary, -1).Value = DBNull.Value;

So in your case:

insertCMD.Parameters.Add("@ErrorScreenshot", SqlDbType.VarBinary,-1).Value = DBNull.Value;
Dhara
  • 4,093
  • 2
  • 36
  • 69
gyousefi
  • 306
  • 3
  • 9
  • This basically worked for me. Thank you! I had to make a couple changes so it looks like this: _params.Add("Value", reportCache.Value, DbType.Binary, ParameterDirection.Input, - 1); – Don Rolling Aug 29 '18 at 15:29
  • Thank you! that did it. I was at a loss why it was giving this error. – AntiqTech Mar 05 '19 at 15:55
  • 1
    This bug seems to have returned in .NET5+. The -1 size trick no longer works. Anyone know any other solutions? – Emperor Eto Oct 07 '21 at 18:32
8

Why not change your SQL to:

INSERT INTO TestplanTeststep
(TeststepId,TestplanId,CreatedAt,ErrorText,ErrorScreenshot,TestState) 
VALUES 
(@TeststepId, @TestplanId,@CreatedAt,NULL,NULL,@TestState)

or just

INSERT INTO TestplanTeststep
(TeststepId,TestplanId,CreatedAt,TestState) 
VALUES 
(@TeststepId, @TestplanId,@CreatedAt,@TestState)

...and omit the two parameters?

If it's always NULL, that will have the same effect.

Otherwise, try it in two lines:

var binary1 = insertCMD.Parameters.Add("@ErrorScreenshot", SqlDbType.VarBinary, -1);
binary1.Value = DBNull.Value;

Otherwise, in your original SQL insert statement, you're not defining the parameter type but passing in varbinary, hence the error.

Sean
  • 14,359
  • 13
  • 74
  • 124
  • ah you were faster just found the -1 trick hehe. I want to explicitly set to null this way others or I know about the default behavior of the table. – Pascal Jul 10 '12 at 12:49
0

I had the same issue and it was resolved using Abdülkadir MAVİ's post here.

I used the line below instead of DBNull.Value:

param.Value = Thumbnail is null ? System.Data.SqlTypes.SqlBinary.Null : Thumbnail;
Jeremy Hodge
  • 612
  • 3
  • 14