943,957 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 8984
  • MS SQL RSS
Jul 3rd, 2009
0

The INSERT statement conflicted with the FOREIGN KEY constraint

Expand Post »
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.


sql Syntax (Toggle Plain Text)
  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 :
vb Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ksenthilbabu is offline Offline
6 posts
since Jun 2009
Jul 3rd, 2009
0

Re: The INSERT statement conflicted with the FOREIGN KEY constraint

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.

sql Syntax (Toggle Plain Text)
  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)
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 4th, 2009
0

Re: The INSERT statement conflicted with the FOREIGN KEY constraint

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 :
MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ksenthilbabu is offline Offline
6 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: String or binary data would be truncated.
Next Thread in MS SQL Forum Timeline: MS SQL 2005 DTS Trouble...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC