My project in C# Winforms 2010 and use SQL Server express and Linq-to-SQL. My project get me exception:
The INSERT statement conflicted with the CHECK constraint “CK_BarCode_Num”. The conflict occurred in database “Parking”, table “dbo.TBL_Cards”, column ‘BarCode_Num’
and when I ran this query:
SELECT name, definition
FROM sys.check_constraints
WHERE name = 'CK_BarCode_Num'"
the output shows the definition of:
Name: CK_BarCode_Num
Definition: (datalength([BarCode_Num])=(13))"
but in TBL_Cards in database, type of BarCode_Num is varchar(100) and in code behind, I declare int for type of BarCode_Num.
I don’t know where is set datalength([BarCode_Num])=(13)?
The check constraint is saying that whatever you insert into
Barcode_Numhas to be exactly 13 characters long.If you’re trying to insert something that isn’t 13 characters long, I’d recommend you consult the documentation for your database (or talk to whoever set it up) to understand why this constraint has been applied.
We’ll not be able to answer that for you, and I wouldn’t recommend altering the check constraint without understanding why it exists in the first place.
DATALENGTHFor
varchar, the number of bytes used corresponds 1-1 with the number of characters. Fornvarcharyou would need to divide by 2.