I have two tables one is a book table and the other is a reservation table. i have set the book ID from the book table as a fk in the reserver table but i keep getting this error
"System.Data.InvalidConstraintException was unhandled
Message: An unhandled exception of type 'System.Data.InvalidConstraintException' occurred in System.Data.dll
Additional information: ForeignKeyConstraint FK_BookTable_ReservationTable requires the child key values (0) to exist in the parent table."
i have added a book to my book table with the id 0 and it save successfully but when i try to add a reserver to the reservation table with the book id 0 i get this exception. i am not sure why it throwing this exception when i have a Book ID 0 in the parent table. can anyone help me please.
Here is the code for the relation table
vb Code:
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Reservation_Table
Private Shared instance As Reservation_Table
Public Shared ReadOnly Property ReservationTableInstance() As Reservation_Table
Get
If instance Is Nothing Then
instance = New Reservation_Table
End If
Return instance
End Get
End Property
Private Sub ReservationTableBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.ReservationTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Librarymanagementdatabase2DataSet)
End Sub
Private Sub Reservation_Table_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Librarymanagementdatabase2DataSet.ReservationTable' table. You can move, or remove it, as needed.
Me.ReservationTableTableAdapter.Fill(Me.Librarymanagementdatabase2DataSet.ReservationTable)
'TODO: This line of code loads data into the 'Librarymanagementdatabase2DataSet.ReservationTable' table. You can move, or remove it, as needed.
Me.ReservationTableTableAdapter.Fill(Me.Librarymanagementdatabase2DataSet.ReservationTable)
End Sub
Private Sub ReservationTableDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)
End Sub
Private Sub ReservationTableBindingNavigatorSaveItem_Click_1(sender As Object, e As EventArgs) Handles ReservationTableBindingNavigatorSaveItem.Click
Me.Validate()
Me.ReservationTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Librarymanagementdatabase2DataSet)
End Sub
Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
databaseconnection.cn.Open()
databaseconnection.cm.Connection = databaseconnection.cn
Try
ReservationTableBindingSource.AddNew()
Reserver_NameTextBox.Select()
databaseconnection.cm.CommandText = "INSERT INTO [dbo].[ReservationTable] ([Reserver Name], [Book ID], [Book Name], [Check out Date], [Check In Date]) VALUES ('" & Reserver_NameTextBox.Text & "', '" & Book_IDTextBox.Text & "', '" & Book_NameTextBox.Text & "', '" & Check_out_dateDateTimePicker.Value.Date & "', '" & Check_in_dateDateTimePicker.Value.Date & "')"
databaseconnection.cm.ExecuteNonQuery()
databaseconnection.cn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
ReservationTableBindingNavigatorSaveItem.PerformClick()
Try
Me.Validate()
Me.ReservationTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Librarymanagementdatabase2DataSet)
ReservationTableBindingSource.AddNew()
Reserver_NameTextBox.Select()
Catch ex As Exception
End Try
End Sub
End Class
here is the relation source code for the reservation table
vb Code:
CREATE TABLE [dbo].[ReservationTable] (
[Reserver ID] INT IDENTITY (1, 1) NOT NULL,
[Reserver Name] VARCHAR (50) NULL,
[Book ID] INT NOT NULL,
[Book Name] VARCHAR (50) NULL,
[Check out date] DATE NULL,
[Check in date] DATE NULL,
PRIMARY KEY CLUSTERED ([Reserver ID] ASC),
CONSTRAINT [FK_ReservationTable_ToTable] FOREIGN KEY ([Book ID]) REFERENCES [dbo].[BookTable] ([Book ID])
);
here the code for the book table
vb Code:
Imports System.Data.SqlClient
Imports System.Data.Sql
Public Class BookTable
Private Shared instance As BookTable
Public Shared ReadOnly Property BookTableInstance() As BookTable
Get
If instance Is Nothing Then
instance = New BookTable
End If
Return instance
End Get
End Property
'Dim cn As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='c:\users\eugen\documents\visual studio 2015\Projects\Library management 11\Library management 11\Librarymanagementdatabase2.mdf';Integrated Security=True")
'Dim cm As SqlCommand
Private Sub BookTableBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.BookTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Librarymanagementdatabase2DataSet)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Librarymanagementdatabase2DataSet.BookTable' table. You can move, or remove it, as needed.
Me.BookTableTableAdapter.Fill(Me.Librarymanagementdatabase2DataSet.BookTable)
'TODO: This line of code loads data into the 'Librarymanagementdatabase2DataSet.BookTable' table. You can move, or remove it, as needed.
Me.BookTableTableAdapter.Fill(Me.Librarymanagementdatabase2DataSet.BookTable)
End Sub
Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
BookTableBindingNavigatorSaveItem.PerformClick()
If Book_NameTextBox.Text = Nothing Then
Book_NameTextBox.Text = "unknown"
End If
If AuthorTextBox.Text = Nothing Then
AuthorTextBox.Text = "unknown"
End If
If ISBNTextBox.Text = Nothing Then
ISBNTextBox.Text = "unknow"
End If
If Book_ConditionTextBox.Text = Nothing Then
Book_ConditionTextBox.Text = "Unknown"
End If
If GenreTextBox.Text = Nothing Then
GenreTextBox.Text = "Uknown"
End If
Try
Me.Validate()
Me.BookTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Librarymanagementdatabase2DataSet)
BookTableBindingSource.AddNew()
Book_NameTextBox.Select()
Catch ex As Exception
End Try
End Sub
Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
databaseconnection.cn.Open()
databaseconnection.cm.Connection = databaseconnection.cn
Try
BookTableBindingSource.AddNew()
Book_NameTextBox.Select()
databaseconnection.cm.CommandText = "INSERT INTO [dbo].[BookTable] ([Book Name], [Author], [ISBN], [Book Condition], [Genre] [Copies]) VALUES ('" & Book_NameTextBox.Text & "', '" & AuthorTextBox.Text & "', '" & ISBNTextBox.Text & "', '" & Book_ConditionTextBox.Text & "', '" & GenreTextBox.Text & "' , '" & CopiesTextBox.Text & "' ,)"
databaseconnection.cm.ExecuteNonQuery()
databaseconnection.cn.Close()
Catch ex As Exception
End Try
End Sub
Private Sub BookTableBindingNavigatorSaveItem_Click_1(sender As Object, e As EventArgs) Handles BookTableBindingNavigatorSaveItem.Click
Me.Validate()
Me.BookTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Librarymanagementdatabase2DataSet)
End Sub
End Class
the relation source code for the Book table
vb Code:
CREATE TABLE [dbo].[BookTable] (
[Book ID] INT IDENTITY (-1, -1) NOT NULL,
[Book Name] VARCHAR (50) NULL,
[Author] VARCHAR (50) NULL,
[ISBN] INT NULL,
[Book Condition] VARCHAR (50) NULL,
[Genre] VARCHAR (50) NULL,
[Copies] INT NULL,
PRIMARY KEY CLUSTERED ([Book ID] ASC)
);