473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

The INSERT statement conflicted with the FOREIGN KEY constraint

Hey All,

I am using MSSQL -2005 with VB6.
I have created a master table tblCompany and detail Table tblDetail having foreign key relationship.
When i try to insert a value within a TRANSACTION I am getting Error No. -2147217873 at Line No. 0 (The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tblDetail_tblCompany". The conflict occurred in database "DBTest", table "dbo.tblCompany", column RefID.) .
please help me to solve this problem.


CREATE TABLE [dbo].[tblCompany](
[RefID] [int] IDENTITY(1,1) NOT NULL,
[CompanyName] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Amount] [decimal](18, 2) NOT NULL CONSTRAINT [DF_tblCompany_Amount] DEFAULT ((0)),
CONSTRAINT [PK_tblCompany_1] PRIMARY KEY CLUSTERED
(
[RefID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[tblDetail](
[DetailID] [int] IDENTITY(1,1) NOT NULL,
[RefID] [int] NULL,
[Amount] [decimal](18, 2) NOT NULL CONSTRAINT [DF_tblDetail_Amount] DEFAULT ((0)),
CONSTRAINT [PK_tblDetail] PRIMARY KEY CLUSTERED
(
[DetailID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[tblDetail] WITH CHECK ADD CONSTRAINT [FK_tblDetail_tblCompany] FOREIGN KEY([RefID])
REFERENCES [dbo].[tblCompany] ([RefID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[tblDetail] CHECK CONSTRAINT [FK_tblDetail_tblCompany]



CODE :

Dim intID as Integer

adoconn.BeginTrans
with adoRsMaster
.Open "Select * from tblCompany Where 1=2",adoconn,adOpenDynamic,adLockOptimistic
addnew
!CompanyName="Sample"
!Amount =2500
.update
intID = !RefID
end with
with adorsDetail
.Open "Select * from tblDetail Where 1=2",adoconn,adOpenDynamic,adLockOptimistic
.addnew
!RefID = intID
!Amount = 2500
.update
end with
adoconn.commitTrans
Jul 3 '09 #1
2 7316
ck9663
2,878 Expert 2GB
Display all the values you're inserting on your detail table. You might have RefID as NULL.

--- CK
Jul 8 '09 #2
First you have to check NULL values for primary key.

And now my suggestion is that you need to create two DIFFRERENT TRANSACTIONS to insert/update in parent and child tabels.

Not sure but when you create a transaction and insert/update any rows in parent tables, these are not inserted until you have not COMMIT the transaction.
Because of that when you try to insert rows in child table it does'nt found the parent row.

So,you can commit parent table transaction ,create new transaction for child table then commit child table transaction.

OR simply if it will not affect your requirement REMOVE transaction from code.


Hope this will work.

HAPPY CODING...

-Pankaj Tambe
Jul 16 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: postings | last post by:
Hi I'm trying to port some data from one database table to another database table on the same server. This is the query I am using: -----> INSERT into newdatabase.dbo.contactevents ...
2
by: trint | last post by:
I am trying to insert a record, but I have to get an automatically created identity field at the same time to do this. string strSQL2 = "INSERT INTO tblTravelDetail(MemberID, " + "...
7
by: trint | last post by:
This: string strSQL2 = "INSERT INTO tblTravelDetailMember(memberId, " + " TravelDetailUplineId, " + " rankId, " + " TravelDetailId, " + " CreatedDateTime, " + " Operator) " + "VALUES ('" +...
2
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error from the code below! Error: INSERT statement conflicted with COLUMN FOREIGN KEY constraint...
0
by: macupryk | last post by:
{System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ProjectResponse__ProjectQuestionId". The conflict occurred in database "RG_ProjectData", table...
0
by: Robert Bravery | last post by:
HI all, VS 2005 C# MSSQL2005 I have a parent child grandchild related tables. Claimheader, claim (deteails), claimfinancialloss When ever I try to add row to claimfinancialloss table, I get...
3
by: weird0 | last post by:
I have two tables accounts and ATM and i am trying to insert a tuple in ATM with accountId as foreign key. But even this simple work,I encounter the following error: The INSERT statement...
3
by: haiminnu | last post by:
I have created Two tables 1]EmployeeTable -------------------------- EmpID EmpName AccessLevelID ...
1
by: waqasahmd | last post by:
Hi i have two tables 1- Person 2- Account In Account table i have key accountID which is a FK in Person table. when i run the insert query as
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.