The INSERT statement conflicted with the FOREIGN KEY constraint

Reply

Join Date: Jun 2009
Posts: 6
Reputation: ksenthilbabu is an unknown quantity at this point 
Solved Threads: 0
ksenthilbabu ksenthilbabu is offline Offline
Newbie Poster

The INSERT statement conflicted with the FOREIGN KEY constraint

 
0
  #1
Jul 3rd, 2009
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.


  1. CREATE TABLE [dbo].[tblCompany](
  2. [RefID] [INT] IDENTITY(1,1) NOT NULL,
  3. [CompanyName] [VARCHAR](250) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
  4. [Amount] [DECIMAL](18, 2) NOT NULL CONSTRAINT [DF_tblCompany_Amount] DEFAULT ((0)),
  5. CONSTRAINT [PK_tblCompany_1] PRIMARY KEY CLUSTERED
  6. (
  7. [RefID] ASC
  8. )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
  9. ) ON [PRIMARY]
  10.  
  11. CREATE TABLE [dbo].[tblDetail](
  12. [DetailID] [INT] IDENTITY(1,1) NOT NULL,
  13. [RefID] [INT] NULL,
  14. [Amount] [DECIMAL](18, 2) NOT NULL CONSTRAINT [DF_tblDetail_Amount] DEFAULT ((0)),
  15. CONSTRAINT [PK_tblDetail] PRIMARY KEY CLUSTERED
  16. (
  17. [DetailID] ASC
  18. )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
  19. ) ON [PRIMARY]
  20.  
  21. GO
  22. ALTER TABLE [dbo].[tblDetail] WITH CHECK ADD CONSTRAINT [FK_tblDetail_tblCompany] FOREIGN KEY([RefID])
  23. REFERENCES [dbo].[tblCompany] ([RefID])
  24. ON UPDATE CASCADE
  25. ON DELETE CASCADE
  26. GO
  27. ALTER TABLE [dbo].[tblDetail] CHECK CONSTRAINT [FK_tblDetail_tblCompany]


CODE :
  1. Dim intID as Integer
  2.  
  3. adoconn.BeginTrans
  4. with adoRsMaster
  5. .Open "Select * from tblCompany Where 1=2",adoconn,adOpenDynamic,adLockOptimistic
  6. addnew
  7. !CompanyName="Sample"
  8. !Amount =2500
  9. .update
  10. intID = !RefID
  11. end with
  12. with adorsDetail
  13. .Open "Select * from tblDetail Where 1=2",adoconn,adOpenDynamic,adLockOptimistic
  14. .addnew
  15. !RefID = intID
  16. !Amount = 2500
  17. .update
  18. end with
  19. adoconn.commitTrans
Last edited by Ezzaral; Jul 3rd, 2009 at 1:33 pm. Reason: Added [code] [/code] tags. Please use them to format any code that you post.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,125
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 550
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: The INSERT statement conflicted with the FOREIGN KEY constraint

 
0
  #2
Jul 3rd, 2009
Please wrap your posts in [code] tags in the future.

Can you post the raw SQL you're using here? I created your tables and was able to insert OK.

  1. INSERT INTO tblCompany (CompanyName, Amount) VALUES ('Sample', 2500)
  2. DECLARE @id INT
  3. SET @id = Cast(SCOPE_IDENTITY() as INT)
  4.  
  5. INSERT INTO tblDetail (RefId, Amount) VALUES (@id, 2500)
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 6
Reputation: ksenthilbabu is an unknown quantity at this point 
Solved Threads: 0
ksenthilbabu ksenthilbabu is offline Offline
Newbie Poster

Re: The INSERT statement conflicted with the FOREIGN KEY constraint

 
0
  #3
Jul 4th, 2009
Hey sknake,
Thanks for your Reply,


I Found that the adoMaster!RefID is returning the value not from the specified table but it is returning then max of IDENTITY Value from the Database.

For Example
Table Name: tblCompany has Primary(identity) Column RefID with last Value of 10
Table Name: tblDetail has Primary(identity) Column DetailID with Last Value 20
Table Name: tblValue has primary(identity) Column ValueID with Last Value 50

After Updating tblCompany with a new record adoMaster!RefID is returning Value 51 which is the next identity Value of tblValue Table instead of returning 11 Which is the next identity Value of tblCompany.

I found this when i Inserted SQL statement after adomaster.update
SQL Statement Inserted :
  1. adoID.Open "Select Max(RefID) from tblCompany", adoConn, adOpenForwardOnly, adLockReadOnly
  2. iRefID = adoID(0).Value

Here adoID(0).Value is returning 11 that has to be return by adomaster!RefID after Updating with new record.

Please guide me
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC