Search code examples
c#sqlsql-server-2005subsonic

Error while trying to store image as byte array using SubSonic


Does SubSonic provide any mechanism to store image (converted in byte[] format)?
Can someone help to get rid of this error?

Database : SQL server 2005
Developing enviornment: ASP.NET (c#)
Subsonic version:2.2.1.0

Datatype in DB: image
Datatype in Subsonic generated DAL : DbType.Binary
datatype in code: byte[] image

Error at statement:

DB.Insert().Into(DamageItems.Schema, "ImageID", "Cost","Image").Values(imageID, cost,image).Execute();

Query Expression in watch window:

{INSERT INTO [dbo].[ImageItems](ImageID,Cost,Image)
 VALUES (@ins_ImageID,@ins_Cost,@ins_Image)
}

Error:

Failed to convert parameter value from a Byte[] to a String.


Solution

  • You may try the following approach:

    DB.Insert().Into<DamageItems>()
        .Value(DamageItems.ImageIDColumn, imageID)
        .Value(DamageItems.CostColumn, cost)
        .Value(DamageItems.ImageColumn, image)
        .Execute();
    

    Maybe the type gets properly recognized when specifying TableColumn objects (can't test right now while writing this).

    If that doesn't work, I'd also check whether naming a column with a data type name (image) causes any problem.

    By the way, from SQL 2005 on, image is deprecated, it is recommended to use varbinary(max) instead.