Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Insert null to an image-data-typed field

Insert null to an image-data-typed field

Scheduled Pinned Locked Moved C#
questioncsharpdatabasesql-serversysadmin
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DannyAdler
    wrote on last edited by
    #1

    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

    A G 2 Replies Last reply
    0
    • D DannyAdler

      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

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • D DannyAdler

        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

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups