Inserting string into SQL Server image column

M

Marcio Kleemann

I have a stored procedure in SQL Server that inserts a row into a table. One
of the columns of the table has the "image" data type, which maps to a byte
array in ADO SqlClient.

The problem I have is that the data that I'm adding as a parameter when
calling the stored procedure comes from a TextBox on the web page, which is
of type string. As a result, I keep getting errors because it can't convert
a string to a byte array. I cannot seem to find any kind of method that
would do that for me.

Does anyone know what would be the correct procedure to pass data for
inserting into a column of type image, when the data is coming as a string
from a text box?

Or, if nothing else, how could I convert a string to a byte array?

Thanks
 
J

Jouni Heikniemi

I have a stored procedure in SQL Server that inserts a row into a
table. One of the columns of the table has the "image" data type,
which maps to a byte array in ADO SqlClient.
The problem I have is that the data that I'm adding as a parameter
when calling the stored procedure comes from a TextBox on the web
page, which is of type string. As a result, I keep getting errors
because it can't convert a string to a byte array. I cannot seem to
find any kind of method that would do that for me.

So, umm, _why_ do you have an image field that's filled from a TextBox?
What is that data you're typing into the field? If it's stored in the DB as
text, why not use the SQL Server's Text data type?

Converting a string to a byte array isn't hard, but it may easily result in
unwanted encoding issues if your string contains 8-bit characters. Anyway,
Encoding.GetBytes is your friend there.
 
M

Marcio Kleemann

Thanks. Encoding.GetBytes seems to do the job.

There actually is a reason why we need to use the image data type, so the
conversion from string accomplished what I needed to do.
 

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