I tried to change the example: link to the example , but I get an error message;
Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'
I believe the return identifier (UniqueIdentifier) ββis incorrect.
My code is:
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; } }
source share