SQL error 547...


Iam experimenting with the ADO.NET. I tried creating a simple Windows based sample application, from which a user can create, modify, and delete rows (data) of Northwind  database through DataGrid.

I can easily add new rows, modify rows but when I try to delete rows SqlException gets thrown. After I handle exception, the following error appears:
SQL Error 547: Delete statement conflicted with COLUMN REFERENCE constaint 'FK_Orders_Customers'. The conflict occured in database 'Northwind', table 'Orders', column 'CustomerID'.

Why is it giving me this error??? when Iam not even using Orders table, Iam using Customers table. btw, here is my code:

//cnn = SqlConnection
SqlCommand cmdDelete = cnn.CreateCommand();
               cmdDelete.CommandType = CommandType.Text;
                    cmdDelete.CommandText = "DELETE FROM Customers WHERE CustomerID = @CustomerID";
                    cmdDelete.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
                    cmdDelete.Parameters["@CustomerID"].SourceVersion = DataRowVersion.Original;

Iam fairly new with ADO.NET, I would appreciate it if someone could tell me the cause of my problem and give me a solution.

Thanks a million!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It looks like you violating referentiol integrity. Check if you have relationshops between Orders and Customers. It might be the child record that you cannot delete until you have deleted the paren record
Mohammed NasmanSoftware DeveloperCommented:

  I think this error for "Constraint violation", seems you are trying to Delete record in Customers table that has details in other tables, if you want to delete from the master, you should first delete it's details records from the detail table, for example if the cusomter table has detail called orders, first delete the records for the cusomter in detail table before you delete it from the master

Best regards
Mohammed Nasman

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
R_a_V_e_NAuthor Commented:
Thanks for ur help guys!!!

both answers were good, unfortunately I can only accept one answer :(
R_a_V_e_NAuthor Commented:
I think you gave more details, so I decided to accept ur answer!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.