Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

"Failed to enable constraints. One or more rows contain values violating non-null, un

 

"Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints." string

 

 

 

 

 

Hi!

 

I get this error when I excetute this code

 

(error on line: m_dataAdapter.Fill(m_dsCarTrade,"CustomerAddress");)

 

 

also I get one error in one Row:

 

<error: an exception of type: {System.Data.StrongTypingException} occurred>

 

(Tables have a relationship to eachother)

 

 


	private OleDbDataAdapter m_dataAdapter;

	private OleDbConnection m_accessConnection;

	private OleDbCommand m_accessCommand;


	private	const string m_conStrAccessConnection  = "provider=Microsoft.JET.OLEDB.4.0; " + 
												   "data source = C:\\Visual Studio Projects\\" +
												   "CarTrade\\CarTradeDB.mdb";	

               private const string m_conStrSelectCommandCustomer = "SELECT Customer.*, CustomerAddress.* FROM Customer INNER JOIN CustomerAddress ON Customer.CustomerID = CustomerAddress.CustomerID";


	private void LoadFreshCustomersFromDB()
	{
		//we create an access object
		m_accessConnection = new OleDbConnection(m_conStrAccessConnection);
		m_accessCommand = new OleDbCommand(m_conStrSelectCommandCustomer, m_accessConnection);
		m_dataAdapter = new OleDbDataAdapter(m_accessCommand);

		//we open the connection to DB
		m_accessConnection.Open();

		try
		{
			m_dataAdapter.Fill(m_dsCarTrade,"CustomerAddress");

			m_dataAdapter.Fill(m_dsCarTrade,"Customer");

		}
		catch (Exception e)
		{
			MessageBox.Show("Sorry....but we have some problems with database. " + "Error message from database : " + e.Message,"ERROR!!!");

		}
		finally
		{
			//we close the connection to DB
			m_accessConnection.Close();
		}

		m_dtCustomers = m_dsCarTrade.Tables["Customer"];
	}


 

Know someone why this error caused?

  • Leaders
Posted
Looks like you either have data in your database that violate constraints that you have in your dataset, or, more likely, the nature of the relationship won't allow the fill to work. To see if it's the second, you set enforceconstraints to false, then call fill, then set enforceconstraints back to true. Someone else may have a better solution...
--tim
  • *Experts*
Posted

I'm not sure what you're trying to do exactly, but it looks like your SELECT is only returning one table (using joins will still bring back one table's worth of data even though it's using multiple tables).

 

You execute your DataAdapter's Fill method twice, but it's not going to fill each table.

 

I think what you want is to have two separate SELECT statements and create a DataRelation between the two (in code) similar to your INNER JOIN in your SQL. The DataAdapter doesn't recognize SQL joins - it treats them as a SELECT from one table.

 

Let me know if you need help piecing things together...

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Your code looks like you are first loading the customer addresses and then the customers.

 

Probably each address is poiting to a customer, though.

As you have the relations enfordec in your dataset, the filling of the addresses will fail, because the customeres are not there yet.

 

Solutions.

(a) Fill the customers and after that fill the adresses.

(b) Fill as you like without enforcing the constraints, turn on the constraints later (not recommended).

 

HTH

Heiko

.nerd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...