Image Field and "Object must implement IConvertible"

T

Thomas

I want to push a file into an Image field in SQL Server, however I keep
getting a "Object must implement IConvertible" error. Firstly, I have
noticed that all of the examples on the net only sample an Update and not an
Insert. Secondly, I want to be able to use a stored procedure, but for this
example I am using simple dynamic SQL. I would rather not use the hokeyness
I have seen with creating a DataTable and using a DataAdapeter and its
Update/Insert command.

Here is the code I have tried that is not working:

(Sql Server table)
Create Table Content (Id uniqueidentifier
, FileName VarChar(255)
, Length Int
, Content Image)

string sql = "Insert dbo.Content(Id, FileName, Length, Content) Values(@Id,
@FileName, @Length, @Content)"
byte[] data = new byte[10000];

Guid id = new Guid("37042A8B-1CD6-4C78-9C17-54EEE4FA3E63");
SqlString fileName = SqlString.Null;

SqlConnection conn = new SqlConnection("...connection string"..);
SqlCommand cmd = new SqlCommand(sql);
cmd.CommandType = CommandType.Text;

cmd.Parameters.Add("@Id", SqlDbType.UniqueIdentifier);
cmd.Parameters["@Id"].Value = id;

cmd.Parameters.Add("@FileName", SqlDbType.VarChar);
cmd.Parameters["@FileName"].Value = fileName;

cmd.Parameters.Add("@Length", SqlDbType.Int);
cmd.Parameters["@Length"].Value = data.Length;

//note that i get errors regardless of whether i pass the length
cmd.Parameters.Add("@Content", SqlDbType.Image, data.Length);
cmd.Parameters["@Content"].Value = data;

cmd.ExecuteNonQuery();


As I mentioned, this fails probably because the byte array, being an Array,
does not implement IConvertible. Ok. Then how do I push binary data to an
image field into a record that does not yet exist?


Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top