473,626 Members | 3,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The INSERT statement conflicted with the FOREIGN KEY constraint

1 New Member
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_t blCompany". 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_Gene ral_CP1_CI_AS NOT NULL,
[Amount] [decimal](18, 2) NOT NULL CONSTRAINT [DF_tblCompany_A mount] 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_Am ount] 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_tb lCompany] FOREIGN KEY([RefID])
REFERENCES [dbo].[tblCompany] ([RefID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[tblDetail] CHECK CONSTRAINT [FK_tblDetail_tb lCompany]



CODE :

Dim intID as Integer

adoconn.BeginTr ans
with adoRsMaster
.Open "Select * from tblCompany Where 1=2",adoconn,ad OpenDynamic,adL ockOptimistic
addnew
!CompanyName="S ample"
!Amount =2500
.update
intID = !RefID
end with
with adorsDetail
.Open "Select * from tblDetail Where 1=2",adoconn,ad OpenDynamic,adL ockOptimistic
.addnew
!RefID = intID
!Amount = 2500
.update
end with
adoconn.commitT rans
Jul 3 '09 #1
2 7331
ck9663
2,878 Recognized Expert Specialist
Display all the values you're inserting on your detail table. You might have RefID as NULL.

--- CK
Jul 8 '09 #2
pankajvtcse
10 New Member
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
2246
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 (EventTypeID, UserID, ContactID, DateEntered, EventDate, Description)
2
1637
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, " + " TravelEventID, " + " CreatedDateTime, " + " Operator) " + "VALUES ('" + TxtMLV.Text.Trim() + "', " + " '" + TxtEventID+ "', " + " '" + TxtDATE.Text.Trim() + "', " +
7
1938
by: trint | last post by:
This: string strSQL2 = "INSERT INTO tblTravelDetailMember(memberId, " + " TravelDetailUplineId, " + " rankId, " + " TravelDetailId, " + " CreatedDateTime, " + " Operator) " + "VALUES ('" + insertString2 + "', " + " '" + insertString3 + "', " +
2
12954
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 'FK_tblOfficePageContent_tblPageContent'. The conflict occurred in database 'CPNCMS', table 'tblPageContent', column 'pageID'. The statement has been terminated. I cant seem to see why I keep getting an error when I do the insert!
0
1982
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 "dbo.ProjectQuestion", column 'ProjectQuestionId'. The statement has been terminated. public void ProjectResponseandRespondentToDB(DataRow dr, string contents, int iProjectQuestionID, int iAnswerNumber) { // Create the command object...
0
2646
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 this error, if and only if the claim detail table is empty: Obviously as the foreign key is 0. System.Data.SqlClient.SqlException was unhandled Message="The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_Claim_claimheader\". The...
3
45321
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 conflicted with the FOREIGN KEY constraint "FK_ATM_Accounts". The conflict occurred in database "G:\DOCUMENTS AND SETTINGS\ADMINISTRATOR\DESKTOP\VERSION2\APP_DATA\BANKINGDB.MDF", table "dbo.Accounts", column 'acc_Id'. please sugggest how can i fix...
3
7247
by: haiminnu | last post by:
I have created Two tables 1]EmployeeTable -------------------------- EmpID EmpName AccessLevelID 2]AccessLevelTable -----------------------------
1
2013
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
8268
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8202
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7199
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.