I have stored procedure which returns "Tabular data" and have only "Get method".
I unchecked "Fill a DataTable" checkbox. And Method name is empty field.
So, i only have a Get method to get rows from database table.
SQL code works fine when i tried it in SQL management studio.
But when i create a stored procedure, i get an alert:
I use BLL/BO/DAL architecture
I call my stored procedure like:
MyDataTable dt = myAdapter.MyStoredProcedure();
And the i get an error:
"System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints"
What i am doing wrong?
Check this same issues
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints
http://forums.asp.net/t/1258783.aspx/2/10
Related
When I set up my SQLite DB and try to get UIPath to read it, I get this error: "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints"
What can I do to get UIPath to read this?
UIPath may produce this error when you try to run a query
“Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints”
The query may work in the command line of the database but not in UIPath but this is deceptive as this is NOT a UIPath issue. It is an issue with the database table itself. The table elements must be a type that UIPath can use.
For instance, one of the issues documented:
SQLite will allow you to create the database table like this
create table clients (name text null, email text null, goodstanding text null);
While this is allowable in the database and it will work, UIPath will not be able to read these rows. Instead, create the table like this:
create table clients (name string null, email string null, goodstanding string null);
UIPath will not know how to handle a variable type “text”
Once you make the table of type string (or other accepted varialbes int, etc) you will be able to use this in UIPath
Is it the same sql exception when we get "Violation of UNIQUE KEY constraint 'XXXXXXX'" vs "Cannot insert duplicate key row in object 'dbo.XXXXXX' with unique index 'XXXXXXX'".
In my serverside code we used to handle this with like following:
catch (SqlException sqlEx)
{
if (sqlEx.Message.Contains("Violation of UNIQUE KEY constraint 'XXXXX'"))
{
.....
}
}
But now the sql server is not prompting the error "Violation of UNIQUE KEY constraint 'XXXXX'", instead it's just saying "Cannot insert duplicate key row in object 'dbo.XXXX' with unique index 'XXXXXX'."
Is there a way in the configuration of MSSQL that I get a descriptive sql exception and get the text "Violation of UNIQUE KEY constraint" back?
The first error happens when you attempt to insert a row that violates a unique constraint on a table.
The second error happens when the row violates a unique index.
It's a subtle difference and frankly, I don't really know why there are two different errors for that, especially considering that a unique constraint is implemented in SQL Server using a unique index.
Anyway, I wouldn't use the error message as an indication of what error you've got. Instead, I would check the error number, exposed in the SqlException class as an int property named - surprise surprise - Number.
You can get a list of all error messages and their relevant numbers by querying sys.messages.
I've done this with a like predicate on text to get the relevant errors for your question:
SELECT message_id, [Text]
FROM sys.messages
WHERE [Text] LIKE '%Cannot insert duplicate key%'
Which returned:
message_id Text
2601 Cannot insert duplicate key row in object '%.*ls' with unique index '%.*ls'. The duplicate key value is %ls.
2627 Violation of %ls constraint '%.*ls'. Cannot insert duplicate key in object '%.*ls'. The duplicate key value is %ls.
(And thanks to Larnu's dupe suggestion, I found the link for Database Engine Errors, but It's faster to query the database than to load that page...)
So, change your c# code in the catch block to
// Cannot insert duplicate key..., Violation of UNIQUE KEY...
if (new int[] {2601, 2627}.Contains(sqlEx.Number))
{
....
}
I have a stored procedure that I'm trying to call from report viewer and I'm getting the error below
Exception has been thrown by the target of an invocation.
Failed to enable constraints. One or more rows contain values
violating non-null, unique, or foreign-key constraints.
The underlying SP makes use of the apply operator to join a function and a table. How do i fix the error?
I was working with LINQ in my asp.net application. I've putted a Unique Key on two columns of the 'ColorDV' table. When trying to add a duplicated value like 'ss' I've got the result:
System.Data.UpdateException: An error occurred while updating the
entries. See the inner exception for details. --->
System.Data.SqlClient.SqlException: Violation of UNIQUE KEY constraint
'TITLE_AR_ColorDV'. Cannot insert duplicate key in object
'dbo.ColorDV'. The duplicate key value is (ss). The statement has been
terminated.
but the problem is that after this exception i can't insert any record in any of the tables, because of the same exact exception. I wonder if LINQ is trying to perform the same previous insert each time.
Please how can I solve this problem?
When you add an object to the ObjectContext, that object stays within the list of objects being submitted to the database, so yes that is the problem. Remove the object and continue, or if an edit try to use the Refresh method on the context to refresh the contents of the object to the original state.
This has an additional approach; can't personally say it works though.
While validation can prevent most SQL errors, there are situations that simply cannot be prevented. I can think of two of them: uniqueness of some column and wrong foreign key: validation cannot be effective as the an object can be created or deleted by other parties just after validation and before db insertion. So there are (at least) two SQL errors that should lead to a message of invalid user input.
SQLException has a Number property for the error type, but I don't know how to find out which column is duplicated or which foreign key is wrong without trying to parse the actual error message text, which happens to be localized.
Is there any way to identify the offending column other than parsing the error message (which means at least to strictly choose a language for SQL Server and always use it)?
edit:
I should mention that I come from RubyOnRails, where the approach is: let's pretend that the db doesn't exist: no constraints, no db-enforced foreign keys etc. As I'm approaching ASP.NET MVC, I'd like to get rid of the rails biases, and accept the fact that the db indeed exists.
Are you sure these two situations absolutely cannot be prevented?
You can avoid unique constraint SQLexceptions on Insert, by using an Identity (database generated) primary key column. SQL Server will guarantee that the value is unique.
The same goes for inserting related rows into tables linked by a foreign key. Insert a row in each referenced table first, before inserting a row in the main table. Use IDENTITY_INSERT to get the value of each auto generated primary key and use this as the foreign key in your main table.
You should also wrap these individual statements in a transaction to ensure that either all tables are inserted successfully or none are. The transaction also isolates (hides) these changes from all other concurrent database accesses until the transaction is committed.