Skip to content
Advertisement

Adding text and image to database with C# and SQL

The following windows form application takes an input for name, age, gender, description, and an image.

enter image description here

However when clicking the “Save Information” button the following exception error occurs:

Not particularly sure why the script is interpreting the image as a nvarchar datatype.

Here is the stored procedure for the function.

And the relevant code.

Advertisement

Answer

Your issue is this line:

This is a string of where the image is located. Which is getting translated from C# string to SQL nvarchar.

You want to insert the actual image. I suspect this may work alone:

But I always try to avoid AddWithValue because it infers the type.

Here’s another way:

Note this explicitly defines the SQL type, where AddWithValue infers it.

For this to work make sure you specify the SqlDbType that matches the schema.

Here’s how to do the same, for example, using a byte array:

In either case the data type is explicit using this method.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement