15

How can I add a null value in a parameter varbinary datatype?

When I execute the following code:

using (SqlConnection myDatabaseConnection1 = new SqlConnection(myConnectionString.ConnectionString))
{
    using (SqlCommand mySqlCommand = new SqlCommand("INSERT INTO Employee(EmpName, Image) Values(@EmpName, @Image)", myDatabaseConnection1))
    {
        mySqlCommand.Parameters.AddWithValue("@EmpName", textBoxEmpName.Text);
        mySqlCommand.Parameters.AddWithValue("@Image", DBNull.Value);
        myDatabaseConnection1.Open();
        mySqlCommand.ExecuteNonQuery();
    }
}

I get the following System.Data.SqlClient.SqlException:

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

Heinzi
  • 167,459
  • 57
  • 363
  • 519
Karlx Swanovski
  • 2,869
  • 9
  • 34
  • 67
  • I have a little detection strategy for dealing with null instances of `byte[]` cast as `object` on a related post: http://stackoverflow.com/a/42905940/97964 – jocull Mar 20 '17 at 14:50

6 Answers6

13

I dont know the reason why "DBNull.Value" does not work for me. And I figure out another solution can solve this problem.

cmd.Parameters["@Image"].Value = System.Data.SqlTypes.SqlBinary.Null;
NoName
  • 877
  • 12
  • 28
7
sqlCommand.Parameters.AddWithValue("@image", SqlBinary.Null);
kekotek
  • 81
  • 1
  • 2
  • 4
    While this answer may answer the question adding some more detail like why and how this works will improve its quality – NathanOliver Oct 17 '16 at 18:05
5

You can try something like this:-

cmd.Parameters.Add( "@Image", SqlDbType.VarBinary, -1 );

cmd.Parameters["@Image"].Value = DBNull.Value;
JumpingJezza
  • 5,498
  • 11
  • 67
  • 106
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
5

try this :

mySqlCommand.Parameters.AddWithValue("@Image", new byte[]{});
Shamseer K
  • 4,964
  • 2
  • 25
  • 43
  • 3
    `Parameters.Add` is deprecated, this answer maybe better, only thing to be noticed is that, it will not insert a `NULL` data (in the table you see the word `NULL`) but an `EMPTY` data (in the table you will see nothing) – Ronaldinho Learn Coding Jul 28 '15 at 15:30
  • This is the best solution as stated by Ronaldinho, thanks for the answer Shamseer – Eliseo Aug 03 '15 at 22:52
  • 1
    As @RonaldinhoLearnCoding said, it's important to note that `byte[0]` is not the same as `null`. – jocull Mar 20 '17 at 14:51
1

i do it like this without a problem

SqlParameter image= new SqlParameter("@Image", SqlDbType.VarBinary, System.DBNull.Value);
mySqlCommand.Parameters.Add(image);
Ehsan
  • 31,833
  • 6
  • 56
  • 65
1

Set null value in your Stored Procedure. Doesn't need to do anything else.

ex. @photo varbinary(max) = null,

ALTER PROCEDURE [dbo].[InsertOurTeam]
    @name nvarchar(50),
    @Sname nvarchar(50),
    @designation nvarchar(50),
    @photo varbinary(max) = null,
    @Pname nvarchar(50)=null,
    @psize bigint=null,
    @id int output
    AS
    BEGIN

        SET NOCOUNT ON;
        insert into OurTeam values (@name,@Sname,@designation,@photo,@Pname,@psize);

        select @id= SCOPE_IDENTITY();
END
Sunil
  • 3,404
  • 10
  • 23
  • 31