Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I divided total sales transaction into two parts. under first part, date, customer name, bill number, gross amount, discount, net amount will come. I am saving all these in salesH database file. Under second part, item name, quantity, price, item wise total will come. I am saving all these in sales database file. I put reference of these two file one common field billno. In salesH, billno is having index key and identity. In sales billno is having Foreign key relation with salesH.billno When I am going to save first transaction one error is coming saying that "The INSERT constraint "FK_sales_salesH. The conflict occured in database "BillingDB".table "dbo.salesH".column "billno". The statement has been terminated.So I ma unable to save the record.


What I have tried:

I searched for this but related not found.
Posted
Updated 18-Apr-17 23:09pm
v3
Comments
Tomas Takac 19-Apr-17 4:06am    
You may want to read about referential integrity and foreign keys.
vijay_bale 19-Apr-17 4:11am    
For first empty databse only it is coming. If I am having data and delete some data and re enter no prob.if I delete all data, re enter and trying save that time only this problem is comimg. How to solve this. For deletion of records I am using CASCADE DELETE so in both files related data will be deleted automatically but prob on saving first time.

1 solution

You can't save the record because it contains a Foreign Key value to a different table, and there is no matching record in the other table.
This often happens when you do the inserts in the wrong order - suppose you have a new customer and you want to record the sale.
You have two tables:
Customers
ID           int, IDENTITY
CustName     NVARCHAR(256)

And Sales
ID           int, IDENTITY
Item         NVARCHAR(256)
Price        decimal
CustID       int, Foreign key to Customers.ID
You can't insert a row in the Sales table first, because you don't have a customer to reference it to - you may create the new Customer row in order to get the ID value you need to insert in the Sales table rows.

In your case, try creating the Bills table entry first, and then you will have the ID value you need to insert the Sales rows.
 
Share this answer
 
Comments
vijay_bale 19-Apr-17 4:35am    
I divided total sales transaction into two parts. under first part, date, customer name, bill number, gross amount, discount, net amount will come. I am saving all these in salesH database file. Under second part, item name, quantity, price, item wise total will come. I am saving all these in sales database file. I put reference of these two file one common field billno. In salesH, billno is having index key and identity. In sales billno is having Foreign key relation with salesH.billno

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900