Hi
I have unique Index on Student field in Database sql. I want when Unique Index error comes , i want to display my own error 'Duplicate record found'
Thanks
Hi
I have unique Index on Student field in Database sql. I want when Unique Index error comes , i want to display my own error 'Duplicate record found'
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal YelveRank #178 · 13.6K reputationPosted Apr 13, 2023, 6:41 AM EDT
Hi Ramco,
try this
if(ex.Message.Contains("UNIQUE KEY constraint"))
{
// your message
}
Amit MohantyRank #112 · 20K reputationPosted Apr 13, 2023, 6:20 AM EDT
Try this
try { // Your code to insert a record into the database } catch (SqlException ex) { if (ex.Number == 2601) // Check if the error is due to a unique index violation { // Display your custom error message Console.WriteLine("Duplicate record found"); } else { // Handle other exceptions as needed Console.WriteLine("An error occurred: " + ex.Message); } }Vishal YelveRank #178 · 13.6K reputationPosted Apr 13, 2023, 6:24 AM EDT
Hi Ramco,
Do refer below link which shows complete implementaion
https://www.codeproject.com/Articles/1130113/Best-Ways-of-Implementing-Uniqueness-or-Unique-Key
else
try { } catch(SqlException ex) { if(ex.Number == 2601 || ex.Number == 2627) { // Violation in one on both... lblErrorMessage.text = "Duplicate record found"; } } OR try { } catct(Exception ex) { if(ex.Message.Contains("UNIQUE KEY constraint")) lblErrorMessage.text="[your unique field] is insert before."; }Ramco RamcoRank #462 · 5.4K reputationPosted Apr 13, 2023, 6:36 AM EDT
Hi AMit
It is giving message ex does not contain a definition for number
Thanks
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 15, 2023, 9:00 PM EDT
You can catch the unique index violation error in your SQL query and replace the standard error message with your own custom error message.
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 15, 2023, 9:02 PM EDT
Lets say you have a table named Students with a unique index on the StudentID field. You can catch the unique index violation error using a TRY...CATCH block in your SQL query like this
BEGIN TRY INSERT INTO Students (StudentID, Name, Age) VALUES (123, 'John Doe', 25); END TRY BEGIN CATCH IF ERROR_NUMBER() = 2601 BEGIN RAISERROR('Duplicate record found', 16, 1); END END CATCHif the INSERT statement violates the unique index on the StudentID field, the error number 2601 will be raised. The IF condition in the CATCH block checks for this error number and raises a custom error message using the RAISERROR statement.
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 15, 2023, 9:04 PM EDT
You can also include the custom error message in a THROW statement
BEGIN TRY INSERT INTO Students (StudentID, Name, Age) VALUES (123, 'John Doe', 25); END TRY BEGIN CATCH IF ERROR_NUMBER() = 2601 BEGIN THROW 50000, 'Duplicate record found', 1; END END CATCH