0
myCommand = New SqlCommand(
"INSERT INTO tblBook(BookCode, BookTitle, Author, PublishingYear, 
                     Price, EnterDate, CatID, RackID, Amount) 
VALUES('" & txtBookCode.Text & "','" & 
            txtTitle.Text & "','" & 
            txtAuthor.Text & "','" & 
            txtPublishYear.Text & "','" & 
            txtPrice.Text & "', #" & 
            txtEnterDate.Text & "#, " & 
            txtCategory.ValueMember & "," & 
            txtRack.ValueMember & "," & 
            txtAmount.Text & ")"
, myConnection)

The error was:

The INSERT statement conflicted with the FOREING KEY constraint "FK_tblBook_tblCategory". The conflict occurred in database "CIEDC", table "dbo.tblCategory", column 'CatID'. The statement has been terminated.

Was that because of my database's relationships?

How can this be solved?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tepken Vannkorn
  • 211
  • 3
  • 4
  • 7
  • Please start using parameters, such as @BookTitle instead of '" & txtTitle.Text & "'. Any book title with a single quote will cause your insert to fail, plus avoid SQL Injection. – LarsTech Jun 15 '11 at 16:23
  • Yep you are right! it is because of the sql relationship, if possible post information about your tables and I can provide more specific help – coderforlife Jun 15 '11 at 16:17
  • It's CREATE TABLE [dbo].[tblCategory]([CatID][int] IDENTITY(1,1) NOT NULL, [RackID][int] NOT NULL, [CategoryName][varchar](50) NULL, CONSTRAINT [PK_tblCategory] PRIMARY KEY CLUSTERED ([CatID] ASC) – Tepken Vannkorn Jun 15 '11 at 16:31

2 Answers2

2

It appears that the value of txtCategory.ValueMember does not correspond to a valid category ID in your database.

Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
0

I think there are two cases:

1.

In your table dbo.tblBook, it has a column named CatID act as foreign key reference to dbo.tblCategory table. It seems that you're trying to use CatID property that haven't been created yet.if so, go back and create a CatID first.

2.

Maybe there are more than one foreign key that related to your table. if you're using sql server so below sql statement will help you find all of them, check it out and drop the leftover, good luck!

    USE <database_name>;  
GO  
SELECT   
    f.name AS foreign_key_name  
   ,OBJECT_NAME(f.parent_object_id) AS table_name  
   ,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name  
   ,OBJECT_NAME (f.referenced_object_id) AS referenced_object  
   ,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name  
   ,is_disabled  
   ,delete_referential_action_desc  
   ,update_referential_action_desc  
FROM sys.foreign_keys AS f  
INNER JOIN sys.foreign_key_columns AS fc   
   ON f.object_id = fc.constraint_object_id   
--WHERE f.parent_object_id = OBJECT_ID('HumanResources.Employee'); 
GO 
SELECT 
f.name AS foreign_key_name 
,OBJECT_NAME(f.parent_object_id) AS table_name 
,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name 
,OBJECT_NAME (f.referenced_object_id) AS referenced_object 
,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name 
,is_disabled 
,delete_referential_action_desc 
,update_referential_action_desc 
FROM sys.foreign_keys AS f 
INNER JOIN sys.foreign_key_columns AS fc 
ON f.object_id = fc.constraint_object_id 
--WHERE f.parent_object_id = OBJECT_ID('HumanResources.Employee');
J.Doe
  • 11
  • 4