2

I get this error when I try to add a new row.

HexStringToByteArray("0x546F206A65737420707573747920706C696B0D0A");

String or binary data would be truncated.\r\nThe statement has been terminated.

attachement.ATF_ID = 244512;
attachement.ATF_WFDID = 160489;
attachement.ATF_ATTID = 244512;
attachement.ATF_Name = "teska14.txt";
attachement.ATF_FileType = ".txt";
attachement.ATF_Value = HexStringToByteArray("0x546F206A65737420707573747920706C696B0D0A");
attachement.ATF_FileIsOcr = 0;
attachement.ATF_CreatedBy = "test";
attachement.ATF_UpdatedBy = "test";
attachement.ATF_OrginalValueHash = HexStringToByteArray("0x4C35C01D7E10FB7F7440D39932265E7C7F09A4D17FEFB55AE9E1DDAC97AD373E");
attachement.ATF_OrginalName = "test.txt";
attachement.ATF_Version = -1;
context2.WFAttachmentFiles.AddObject(attachement);
//context.AddToWFAttachmentFiles(attachement);
//context.WFAttachmentFiles.AddObject(attachement);
context2.SaveChanges();

HEC Code:

public static byte[] HexStringToByteArray(string hexString)
{
    if (string.IsNullOrWhiteSpace(hexString))
        throw new ArgumentNullException("hexString");

    if (hexString.Length % 2 != 0)
        throw new Exception("Invalid hex string");

    var bytes = new byte[hexString.Length / 2];
    for (int i = 2; i < bytes.Length; i++)
    {
        bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
    }
    return bytes;
}
2
  • It appears you changed your question, try checking db column size limits to resolve this error. Commented Mar 10, 2015 at 14:08
  • @RaviMPatel I copy this 0x546F206A65737420707573747920706C696B0D0A from ATF_Value from database. Now I want to send this for test to database. Using sql is very simple when I want to INSERT I write ATF_Value = '0x546F206A65737420707573747920706C696B0D0A' Commented Mar 10, 2015 at 14:28

2 Answers 2

5

Usually when you see that error message:

String or binary data would be truncated. The statement has been terminated.

It means that one of the database columns is not big enough to hold the data you are trying to save.

In your case, check the size of the columns ATF_OrginalValueHash and ATF_Value in the database and maybe increase them

Sign up to request clarification or add additional context in comments.

6 Comments

I copy this 0x546F206A65737420707573747920706C696B0D0A from ATF_Value from database. Now I want to send this for test to database. Usig sql is very simple when I want to INSERT I write ATF_Value = '0x546F206A65737420707573747920706C696B0D0A'
what is the datatype of the column in SQL Server - try setting it to VARBINARY
ATF_OrginalValueHash -> binary (32), ATF_Value -> image
just noticed that that length of the data you are trying to insert to ATF_OrginalValueHash is 44 characters - which will cause the above truncation error. Try increasing the field size in the db
Damn! I've been working on this one for an hour already. Thank god i saw your post.
|
4

Update

Basic idea is to make sure whether entity properties are translated to compatible sql columns or not.

One more solution is to use LINQPad, if you put your code there, it'll show you SQL statement fired by entity framework and that give you you sufficient insight to figure out why your code fails.


Usually this error shows up when you don't initialize the DateTime properties of your entity and you try to save changes to SQL Server database.

All c# DateTime properties are initialized to January 1, 0001, which is a minimum value of the type.

The minimum value of the datetime data type in Sql Server is January 1, 1753.

That results in out of range exception. If you initialize your DateTime properties accordingly, you shouldn't see this error. If appropriate in your scenario, you might as well consider using SqlDateTime.MinValue (reference) to initialize your properties.

1 Comment

I found solution for date but now I edited my post to this problem. "HexStringToByteArray"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.