how can i fix this error "Operand type clash: nvarchar is incompatible with image" this error occur when i try to DBNULL.Value for insert an empty image means null image in image data type of ms sql serever RRS feed

  • Question

  • conn.Open()

            cmd.CommandText = "Insert into customer (cid,name,cno1,cno2,email,address,city,state,pincode,photo) Values('" & cid & "' , '" & name_textBox.Text & "' , '" & cno1_textBox.Text & "' , '" & cno2_textBox.Text & "' , '" & email_textBox.Text & "', '" & add_TextBox.Text & "'  , '" & city_textBox.Text & "' , '" & state_textBox.Text & "' , '" & pin_textBox.Text & "' , @img); "
            cmd.Connection = conn

            cmd.Parameters.AddWithValue("@img", System.DBNull.Value)
            Dim i As Integer = cmd.ExecuteNonQuery
            If i > 0 Then
                info("Customer Added", "Customer Adding Information")
                err("Customer Adding Exe NON QUERY ERROR", "Customer Adding Error")
            End If
    Friday, April 26, 2019 10:55 PM

All replies

  • This is apparently a known issue.  By adding the null that way, the SQL database sees it as an incompatible type.  Here is a way to work around it:

    By the way, your code is Visual Basic, so it's not clear why you would post this in a C# forum.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Saturday, April 27, 2019 12:48 AM
  • The problem is here:

    cmd.Parameters.AddWithValue("@img", System.DBNull.Value)

    You cannot use "AddWithValue" with a DBNull Value. Normally, AdWithValue infers the type of the paramenter from the type that you pass for a value. But a type cannot be inferred from NULL. Addwithvalue treats it as nvarchar and the db server doesn't like it.

    The remedy is to use the "long" form for adding the parameter ("Add" instead of "AddWithValue"):

    cmd.Parameters.Add("@img", SqlDbType.Image).Value = System.DBNull.Value;

    Sunday, April 28, 2019 8:19 PM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, April 29, 2019 1:30 AM