I am trying to insert value from code in which i want talukcode = regioncode like
model.talukcode = model.regioncode;
previously it was like
model.villagecode = MaxValue + 1;
but now i am trying
model.talukcode = model.regioncode;
and i am getting an error
System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_ppatalukmaster'. Cannot insert duplicate key in object 'dbo.ppatalukmaster'. The duplicate key value is (3, 3).
The statement has been terminated.
Is there any way to solve this error through code.
Jayraj ChhayaPosted Dec 6, 2023, 6:55 AM
The error message indicates that the primary key constraint on the
ppatalukmastertable is being violated. This means that the combination of values being inserted into the primary key columns already exists in the table. In this case, the duplicate key value is (3, 3).To solve this error, you need to ensure that the combination of values being inserted into the primary key columns is unique. One way to achieve this is by checking if the combination already exists in the table before performing the insert operation.
In the above code, the
CheckCombinationExistsmethod checks if the combination oftalukcodeandregioncodealready exists in the table. If it does not exist, theInsertDatamethod is called to perform the insert operation. Otherwise, an appropriate error message or handling logic can be implemented.By implementing this check, you can prevent the violation of the primary key constraint and avoid inserting duplicate values into the table.