I tried to modify the example from: link to example but i receive an error;Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'
I suppose that the returned ID (UniqueIdentifier) isn’t correct.
My code:
public static Guid AddRecord(string firstCol, DateTime SecCol, string photoFilePath)
{
using (SqlConnection connection = new SqlConnection(
"Data Source=(local);Integrated Security=true;Initial Catalog=Test;"))
{
SqlCommand addRec = new SqlCommand(
"INSERT INTO myTable (firstCol,SecCol,Image) " +
"VALUES (@firstCol,@SecCol,0x0)" +
"SELECT @Identity = NEWID();" +
"SELECT @Pointer = TEXTPTR(Image) FROM myTable WHERE ID = @Identity", connection);
addRec.Parameters.Add("@firstCol", SqlDbType.VarChar, 25).Value = firstCol;
addRec.Parameters.Add("@SecCol", SqlDbType.DateTime).Value = SecCol;
SqlParameter idParm = addRec.Parameters.Add("@Identity", SqlDbType.UniqueIdentifier);
idParm.Direction = ParameterDirection.Output;
SqlParameter ptrParm = addRec.Parameters.Add("@Pointer", SqlDbType.Binary, 16);
ptrParm.Direction = ParameterDirection.Output;
connection.Open();
addRec.ExecuteNonQuery();
Guid newRecID = (Guid)idParm.Value;
StorePhoto(photoFilePath, (byte[])ptrParm.Value, connection);
return newRecID;
}
}
As noted in the other answer, the example is obsolete; I would not recommend using it.
If you are set on making it work just as an exercise, change your SQL to insert the ID that you created into
myTable, as follows: