Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null value in a parameter varbinary datatype

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.

like image 892
Karlx Swanovski Avatar asked Aug 11 '13 10:08

Karlx Swanovski


2 Answers

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;
like image 145
NoName Avatar answered Nov 16 '22 11:11

NoName


sqlCommand.Parameters.AddWithValue("@image", SqlBinary.Null);
like image 7
kekotek Avatar answered Nov 16 '22 12:11

kekotek