Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6701973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:59:52+00:00 2026-05-26T06:59:52+00:00

I am using Dynamic Data and to apply Unique key and other database level

  • 0

I am using Dynamic Data and to apply Unique key and other database level validations i have overridden the SubmitChanges function.

This gives me original error, which i am able to show to the end user. But, this message is like this:

Violation of UNIQUE KEY constraint 'UK_CountryName_Country'. Cannot insert duplicate key in object 'dbo.Country'. The duplicate key value is (America).
The statement has been terminated.

I need the following keywords of the message:

  1. UNIQUE KEY
  2. UK_CountryName_Country
  3. America

I have written following Regex:

System.Text.RegularExpressions.Regex.Split(e.Message.ToString(), "^Violation of (UNIQUE KEY){1} constraint '([a-zA-Z_]*)'. Cannot insert duplicate key in object 'dbo.([a-zA-Z]*)'. The duplicate key value is ")

I have succeeded in getting #1 & #2, but #3.

Can anyone help me. Also, is there any cleaner way to achieve it.
FYI: I would be catching other types of database errors as well.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:59:53+00:00Added an answer on May 26, 2026 at 6:59 am

    With your example expression, it seemed you were trying to match the Country from dbo.Country instead of America as you asked, so I included both in my expression/code:

     Dim ExpressionText As String = "^Violation of (?<KeyType>.*?) constraint '(?<ConstraintName>[^']*)'\.[^']*'(?<ObjectName>[^']*)'[^(]*\((?<KeyValue>[^)]*)\)"
     Dim SearchText As String = "Violation of UNIQUE KEY constraint 'UK_CountryName_Country'. Cannot insert duplicate key in object 'dbo.Country'. The duplicate key value is (America). " _
                                   & vbCrLf & "The statement has been terminated."
     Dim regex As New System.Text.RegularExpressions.Regex(ExpressionText)
     Dim mc As System.Text.RegularExpressions.Match = regex.Match(SearchText)
    
     Response.Write(mc.Groups("KeyType").Value)           ' writes UNIQUE KEY
     Response.Write(mc.Groups(1).Value)                   ' writes UNIQUE KEY
     Response.Write(mc.Groups("ConstraintName").Value)    ' writes UK_CountryName_Country
     Response.Write(mc.Groups(2).Value)                   ' writes UK_CountryName_Country
     Response.Write(mc.Groups("ObjectName").Value)        ' writes dbo.Country
     Response.Write(mc.Groups(3).Value)                   ' writes dbo.Country
     Response.Write(mc.Groups("KeyValue").Value)          ' writes America
     Response.Write(mc.Groups(4).Value)                   ' writes America
    

    The expression is designed to be very tolerant of the type of error message, as there are other types of Key Constraints that can be violated. I could not find a list of the possible errors, but here is a breakdown of the expression:

    ^Violation of                   # Match literal text
    (?<KeyType>.*?) constraint      # Capture the words that come before  " constraint"
    '(?<ConstraintName>[^']*)'\.    # Capture whatever is inside the single quotes:  '
    [^']*                           # Match anything up to the next single quote
    '(?<ObjectName>[^']*)'          # Capture whatever is inside the single quotes
    [^(]*                           # Match anything up to the next open parentheses:  ( 
    \((?<KeyValue>[^)]*)\)          #Capture whatever is inside the parentheses
    

    You can get rid of the parts that look like ?<Something> if you do not want to use named capturing groups, and only want to use the numbers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ASP.NET Dynamic Data for a project and I have a table
I have an ASP.NET Dynamic Data application (using Entity Framework) in which I have
I have create a LINQ-to-SQL project in Visual Studio 2010 using Dynamic Data. In
I have a number of data structures (trees, queues, lists), created using dynamic allocation
I am using ASP.NET dynamic data. In Insert.aspx page, I have a couple of
I have to send and receive dynamic data using a SysV message queue for
With a function a div-popover gets called and filled with dynamic data using Ajax,
I'm saving NumPy arrays using numpy.save function. I want other developers to have capability
I have a Dynamic Data 4 site using LinqToSql (.dbml) for 2 tables with
Using ASP.NET Dynamic Data with a LINQ to SQL DataContext from the Northwind Database...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.