.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Cannot insert duplicate key. . Better message

Posted By:      Posted Date: April 10, 2011    Points: 0   Category :


When the user tries to save a record that includes a value that already exists it returns

"Cannot insert duplicate key row in object dbo.tblTableName with unique index IndexName"

I'd like to make this message read better for users - such as simply 'this record already exists'

Do I need to handle this in my application or can I configure something on the SQL database itself (e.g. table design or in the index) to return a more readable message.


View Complete Post

More Related Resource Links

Get error message from bulk insert task.

Hello, I've created a dtsx package that bulk inserts some data from a csv file into a sql table (using the BULK INSERT TASK). Because this package is scheduled to run automatically, I also added a EXECUTE SQL TASK that will insert a row in a 'log table'. It is linked to the BULK INSERT task with a 'Succes constraint (green arrow)', so I know about all the successes. But I am more interested in the failures, so I also added another EXECUTE SQL TASK and linked it to the BULK INSERT task with the 'failure constraint (red arrow)'. Question: How can I get the error message from the BULK INSERT TASK (like 'file does not exist' or 'error converting .....') and store it in a field in the 'log table'? Thanks in advance, Peter

Formview check for duplicate value before insert

I've looked at some posts on here but I keep getting errors.  I am just trying to see if an email address exists already before the insert and if it does, have a label display that message to the user. From what I read so far, it looks like I could do a query in the ItemInserting event.  I just need help on the code.  Thanks in advance for any help.. Here is the Code Behind.....where I assume I can do the duplicate check in the ItemInserting event... Protected Sub duplicatecheck(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInsertingEnd   End Sub     Here is the formview page...   <%@ Page Language="VB" AutoEventWireup="false" CodeFile="signup.aspx.vb" Inherits="signup" %> <! < <DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">html xmlns="http://www.w3.org/1999/xhtml">head runat="server">   </ <<title></title>head>body>  <form id="form1" runat="server">  <div>      <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource2" Def

CreateUserWizard not showing a duplicate user message

I'm using emails as the user name, and I've got requiresUniqueEmail="true" in the web.config.I have an empty CreatingUser method, and in CreatedUser I perform a couple of table entries with the new user.When I try to add a user with an email that's already in use, I get a the default success message.  But no user is added, andit doesn't reach the CreatedUser method.I must be missing something.  Ideas?

Violation of PRIMARY KEY constraint 'PK_User_Details'. Cannot insert duplicate key in object 'dbo

hi,i'm using sql server 2005 as backend for web development .In one of my database table User_Details, 'user_id' is the primary key ,and it is set to autoincrementing as follows.object count = objUser.countUid(); string count1 = Convert.ToString(count);            if (count1 == "0")            {                string uid5 = "U1";           objUser.addUser(string uid5);}else{               object uid = objUser.selectUid();                string usid = uid.ToString();                string uid2 = usid.Substring(1);                int uid3 = int.Parse(uid2);                uid3 += 1;                string uid4 = usid.Substring(0, 1);          

Violation of PRIMARY KEY constraint. Cannot insert duplicate key in object


i am unable to insert values into my table when i am using primary key and foreign key in my tables i am sure that i have not voilated the constraints rules and its working well when i remove the constraints.....

Browser refresh and duplicate insert,...again

Hi Guys,

I have "duplicate ID insertion when user refreshes page" problem.

I looked and looked but can't find a quick fix (response.redirect to the page again is just
not an option,...want to keep the UI without flickering controls ..ajax used here) nor do I
want to add new columsn to the database.

page.IsPostback does not solve the issue either, since it is not a postback we are dealing with here.

I just wonder how the brwoser "refresh" triggers my button1.click sub in the first place - since
the button is not clicked. I guess the http header somehow has the click event in it.
Is there a, easy (vb.net) way to detect "refresh" and avoid the instertion.

i read the following "One way would be to use AJAX and call a web service / page method to handle the insert. "
Any suggestions how that would work?

Cannot insert duplicate key row in object 'dbo.catalog'

Either I did a dumb thing, or SSRS 2005 is doing a dumb thing (or both?). Or maybe it's a SharePoint integration issue...

Here's what happened:

1. I published a report to the report library (we're in SharePoint integration mode)
2. In SharePoint, went to the new report and did "Edit Properties", and changed the report name in the Name field to a nicer, user-friendly name
3. Back to BIDS, I make a change to this same report and deploy again.
4. Now back in SharePoint, I see that there are two reports - the original one that I renamed in step 2, and this new one that I just deployed 
5. Now I delete the original one (with the nice new name), and rename this new one to that same exact name
6. When execute the report now, it won't run -- I get the following error:

Report Server has encountered a SharePoint error. (rsSharePointError)
Cannot insert duplicate key row in object 'dbo.catalog' with unique index 'IX_Catalog'. The statement has been terminated.
For more information about this error navigate to the report server on the local server machine, or enable remote errors 

The problem i

"Violation of PRIMARY KEY constraint . Cannot insert duplicate key in object ".

How this error could arise? I use data flow to insert rows from another server. I checked there is no duplicate key in primary key. thanks

How to avoid that BCP.exe insert stops when duplicate record exist?

I’m a migration consultant at Infor (Infor and Microsoft have a partnership) and we use in case the target database is SQL Server the bcp.exe to upload the data from old Baan-systems and legacy-systems into our core product “Infor ERP LN”.

The big, big problem with the bcp.exe is that loading stops as soon as a duplicate record exists. Is it possible to extend the bcp.exe to be able to load in append- and conventional mode like Oracle sql loader?
This is the big advantage of the Oracle High performance loader (sqlldr) where you can load the data in Conventional mode which creates the records per row!

I also heard that the bcp.exe will be replaced in future by another utility. We really hope this new utility don’t stop by each duplicate! Maybe you can provide me with more details about the new utility?

Many thanks in advance!

Cannot insert duplicate key row in object 'Table' with unique index 'Column_Id' Problem


All, We have a User where there is an Access Application installed. Every Day he enters some Account Numbers in it. This Access application is hooked to SQL Server 2008 which updates a table in SQL Server. Now last 2 weeks user had some Connection Issues so he did not enter any Account Numbers in the Access application instead he just noted them on a Excel sheet.

Now I did not want him to insert manually, so I took that Excel sheet, loaded account numbers in a Temp table and tried to insert all the Missing Account Numbers in the Main table. But it failed with the following error: Note: Account_No has a Unique Non Clustered Index on it and it is not a Primary key also. How can I insert them? (There are no duplicate account numbers as well that I am trying to insert its all Unique Accout Numbers)

Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.Table_Name' with unique index 'Accoutn_No'.
The statement has been terminated.


Violation of PRIMARY KEY constraint. Cannot insert duplicate key in object...


Hi all I set up transactional replication between a sql 2008 and sql 2005 server.  I have a table with an identiy column and a varchar field. I accidentally SET IDENTITY INSERT ON at the subscriber instead of the subscriber server and inserted a row. After that every row I inserted in the publisher has not be replicated to subscriber? I see this error message in the replication monitor?

Violation of PRIMARY KEY constraint 'PK__IdentTes__32149A135070F446'. Cannot insert duplicate key in object 'dbo.IdentTest'


I tried deleting the row at subscriber but still the same message?


Gmail internal chat like message box

This article provides you with the multi-functional message box window through which the usability of the web-site or any web application increases.

.Configurable MessageBox window with AJAX Support
.can set maximum number of message boxes to be visible
.can define the auto reload time, for single message, for all messages or no reload
.can create messages through a single function call
.can define auto hide time for each messages

Insert value using Table Value Functions

a real gem in Sql Server 2008. mostly people still using Stored procedure may be they shifted to SQL Server but they are not using TVF right now.

Insert Rows with a GridView

In ASP.NET version 1.x, I used the footer on the DataGrid control to insert new rows by placing a series of TextBox, DropDownLists, CheckBoxes etc. controls on it and then handling a save button. This also works with the GridView in version 2.0 but with one major exception: if your data source is empty, the GridView will hide your footer (and header) and only display the EmptyDataText.

SqlCommand.ExecuteNonQuery() returns -1 when doing Insert / Update / Delete

Sometimes you end up with a return value of -1 when using the SqlClient.SqlCommand.ExecuteNonQuery method.

Why is that?

Well, the ExecuteNonQuery method is there for statements for changing data, ie. DELETE / UPDATE /INSERT, and the returned value are the number of rows affected by that statement.

When checking the documentation we can see that there are some conditions that return -1.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.

When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of

rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

Inserting personal error message in ValidationSummary



I am creating a web form that requires a lot of validation. I use many of the validation control in asp.net already like RequiredFieldValidator and CompareValidator. 

My Problem: after my custom validation for the value entered return an error message, and I pressed enter key when the field is empty, my custom error message is overlapped by my ValidationSummary. 

Setting my custom error to visible = false on button click also does not seem to work as it requires for me to enter the value first.

This made me think, is there a way to insert my custom validation inside the ValidationSummary as an error message like for other validation control?

Thanks in advance

UpdatePanel is not a known element - message when upgrading to 1.0 ajax


It worked on one of my old pages.  I change the web.config, renamed all the <asp.

On some aspx page it doesn't recognize the updatepanel.  It only does on one page.  If I create a new aspx page with a different name - then it works.

Should I just create a new web project ajax - then copy all my code into there?  Or is there a way to get those old pages to recognize the new ajax controls?

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend