2
(playerListTableAdapter.GetHeight(sortedPlayers[counter])

"Sorted Players" is an array of decimals, which match primary keys.

When I call the query GetHeight, I get the following error: "ConstraintException was unhandled: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."

The SQL statement for the query is:

SELECT        Height
FROM            PlayerList
WHERE        (Number = @PlayerID)

Note: In the database, Number is a primary key.

When we step through the execution, we can see that sortedPlayers[counter] does match a player in the database.

Any ideas on what causes this error?

Thanks!

-Dominique

Dominique
  • 167
  • 10

3 Answers3

2

You add your primary key in your query

SELECT       YourPrimaryKey, Height
FROM            PlayerList
WHERE        (Number = @PlayerID)
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
2

To solve this you can set EnforceConstraints to false on the DataTable.

EplayerListTableAdapter.EnforceConstraints = false;

A more detailed answer is here: http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/c2c9158d-cddf-40cf-bf6c-794dc3ef9c7f/

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
2

After fighting to get the query working, I discovered that I could access the value more directly by using the following syntax:

decimal? Player1Hgt = dsPlayerTeam.PlayerList.Rows.Find(sortedPlayers[counter]).Field<decimal?>("Height");

This allowed me to go pull the value from the database without using the tableAdapter query and is significantly simpler.

Incidentally, I did attempt to set the EnforceConstraints property of my tableAdapter to false, but it still threw the error.

Thank You for your help!

Dominique
  • 167
  • 10