Karlx Swanovski Karlx Swanovski - 1 month ago 15
C# Question

Null value in a parameter varbinary datatype

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

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();
}}

Answer

You can try something like this:-

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

cmd.Parameters["@Image"].Value = DBNull.Value;
Comments