Saturday, February 25, 2012

Operand type clash: nvarchar is incompatible with image

I uploading an image from a web page into Sql Server 2000 database. I call a stored procedure with the parameter @.Flag of type Image. When I tried to pass a null value to this parameter I got the error:
"Operand type clash: nvarchar is incompatible with image".

I was adding the parameter like this:
sqlCmd.Parameters.AddWithValue("@.Flag", DBNull.Value);

After trying few different things I found a workaround using the following:
sqlCmd.Parameters.Add("@.Flag", SqlDbType.Image);
sqlCmd.Parameters["@.Flag"].Value = DBNull.Value;

Possibly a bug in the SqlCommand.Parameters.AddWithValue method ?

Not a bug. It's a limititation. If you don't specify a type (like with AddWithValue), then it makes a best guess. In this case, it guesses wrong.

No comments:

Post a Comment