Insert null to an image-data-typed field
-
Hi all, So you have a table "Items" with 2 fields (SQL Server 2000): Column Datatype Length Allow Nulls ------------------------------------------- ItemID int 4 0 ItemPicture image 16 1 Now you want to insert a record with the ItemID=1 and with no picture (i.e. null). So you do this (.NET 2.0): string strSQL = "insert into items (ItemID, ItemPicture) values(@ItemID, @ItemPicture)"; SqlCommand cmd = new SqlCommand(strSQL, GetDBConnection()); cmd.Parameters.AddWithValue("@ItemID", "1"); cmd.Parameters.AddWithValue("@ItemPicture", System.DBNull.Value); if (cmd.Connection.State != ConnectionState.Open) cmd.Connection.Open(); cmd.ExecuteNonQuery(); if (cmd.Connection.State != ConnectionState.Closed) cmd.Connection.Close(); And you get an exception: "Operand type clash: nvarchar is incompatible with image". Question: So how can you insert a null value to an image field? Thanks in advance, Danny
-
Hi all, So you have a table "Items" with 2 fields (SQL Server 2000): Column Datatype Length Allow Nulls ------------------------------------------- ItemID int 4 0 ItemPicture image 16 1 Now you want to insert a record with the ItemID=1 and with no picture (i.e. null). So you do this (.NET 2.0): string strSQL = "insert into items (ItemID, ItemPicture) values(@ItemID, @ItemPicture)"; SqlCommand cmd = new SqlCommand(strSQL, GetDBConnection()); cmd.Parameters.AddWithValue("@ItemID", "1"); cmd.Parameters.AddWithValue("@ItemPicture", System.DBNull.Value); if (cmd.Connection.State != ConnectionState.Open) cmd.Connection.Open(); cmd.ExecuteNonQuery(); if (cmd.Connection.State != ConnectionState.Closed) cmd.Connection.Close(); And you get an exception: "Operand type clash: nvarchar is incompatible with image". Question: So how can you insert a null value to an image field? Thanks in advance, Danny
DannyAdler wrote:
So how can you insert a null value to an image field?
I have no idea. Can you not just insert a picture of size 1*1, or 0*0? Infact, does it even let you make a picture of 0*0?
My current favourite word is: Nipple!
-SK Genius
-
Hi all, So you have a table "Items" with 2 fields (SQL Server 2000): Column Datatype Length Allow Nulls ------------------------------------------- ItemID int 4 0 ItemPicture image 16 1 Now you want to insert a record with the ItemID=1 and with no picture (i.e. null). So you do this (.NET 2.0): string strSQL = "insert into items (ItemID, ItemPicture) values(@ItemID, @ItemPicture)"; SqlCommand cmd = new SqlCommand(strSQL, GetDBConnection()); cmd.Parameters.AddWithValue("@ItemID", "1"); cmd.Parameters.AddWithValue("@ItemPicture", System.DBNull.Value); if (cmd.Connection.State != ConnectionState.Open) cmd.Connection.Open(); cmd.ExecuteNonQuery(); if (cmd.Connection.State != ConnectionState.Closed) cmd.Connection.Close(); And you get an exception: "Operand type clash: nvarchar is incompatible with image". Question: So how can you insert a null value to an image field? Thanks in advance, Danny
The DBNull value is converted to a string, so you are actually trying to put the string returned by DBNull.ToString() (which is an empty string) into the field. Specify the data type for the parameter: cmd.Parameters.Add("@ItemPicture", SqlDbType.Image).Value = System.DBNull.Value;
Experience is the sum of all the mistakes you have done.