I found this URL about how to add an item in my table.
http://msdn.microsoft.com/en-us/library/bb386941.aspx
// Create a new Order object.
Order ord = new Order
{
OrderID = 12000,
ShipCity = "Seattle",
OrderDate = DateTime.Now
// …
};
This should work without any problems
but if I have something like this:
// Create a new Order object.
Order ord = new Order
{
OrderID = 12000,
CustomerID = 22, // where CustomerID is a foreign key to table Customer
ShipCity = "Seattle",
OrderDate = DateTime.Now
// …
};
Then it will give errors like:
Exception Details: System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint “FK7_REVIEW”. The conflict occurred in database “Dbname”, table “dbo.Customer”, column ‘CustomerID’.
The statement has been terminated.
How can I insert a row in my table, whith foreign keys?
Order.CustomerIDrefers to a primary key in another table (sayCustomer). To insert the new order, theCustomerIDmust be equal to an existing value of the primary key inCustomertable.