INSERT statement conflicted with COLUMN FOREIGN KEY

G

Guest

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!



''' CODE

Private _NewPageID As Long

Public Sub DGPages_Insert(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
If e.CommandName = "Insert" Then
Dim modDate As String
Dim dtNow As DateTime = DateTime.Now
Dim description As String
Dim txtdescription As TextBox
Dim title As String
Dim txtTitle As TextBox
Dim PageID As Integer
Me._NewPageID = PageID


Dim strSQL As String
modDate = dtNow.Date
'Read in the values of the TextBoxes


txtdescription = e.Item.FindControl("add_description")
description = txtdescription.Text
txtTitle = e.Item.FindControl("add_Title")
title = txtTitle.Text


'Create the appropriate SQL statement
Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("strConn"))
Dim cmd As New SqlCommand("PageAdd", Myconn)
cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

' Adds Information from the insert area within the datagrid into
tblPageContent
' and returns the record ID
Dim objModDate, objDescription, objTitle, objPageID, objOffice
As SqlParameter
objModDate = cmd.Parameters.Add("@modDate", SqlDbType.DateTime)
objDescription = cmd.Parameters.Add("@description",
SqlDbType.NVarChar)
objTitle = cmd.Parameters.Add("@title", SqlDbType.NVarChar)
objPageID = cmd.Parameters.Add("@PageID", SqlDbType.Int)

objModDate.Direction = ParameterDirection.Input
objDescription.Direction = ParameterDirection.Input
objTitle.Direction = ParameterDirection.Input
cmd.Parameters("@PageID").Direction = ParameterDirection.Output

objModDate.Value = modDate
objDescription.Value = description
objTitle.Value = title


Dim myReader As SqlDataReader = cmd.ExecuteReader()

myReader.Read()
myReader.Close()
PageID = cmd.Parameters("@PageID").Value
Myconn.Close()
DoInsert(_NewPageID, LookupOfficeID())

'Rebind the DataGrid
DGPages.EditItemIndex = -1
BindData()

End If

End Sub

Public Sub DoInsert(ByVal _NewPageID As Long, ByVal OfficeId As Long)

'open a connection and fire the insert sql here
Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("strConn"))
Dim cmd As New SqlCommand("OfficePageAdd", Myconn)
cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

' Adds the OfficeID and PageID into tblOfficePageContent
Dim objOfficeID, objPageID As SqlParameter
objOfficeID = cmd.Parameters.Add("@officeID", SqlDbType.NVarChar)
objPageID = cmd.Parameters.Add("@PageID", SqlDbType.NVarChar)

objOfficeID.Direction = ParameterDirection.Input
objPageID.Direction = ParameterDirection.Input

objOfficeID.Value = OfficeId
objPageID.Value = _NewPageID

Dim myReader As SqlDataReader = cmd.ExecuteReader()
myReader.Read()

myReader.Close()

Myconn.Close()

End Sub
 
M

Marina

Looks like you have a foreign key on a column, but you are trying to put a
value in it that does not exist in the corersponding table to which you
defined the foreign key. The same thing would happen if you tried to run the
insert in query analyzer. Make sure you are entering valid data, then try
again.
 
K

Karl Seguin

The error message is telling you that you are trying to add a row in
violation of some constraint (a foreign key). In other words,
tblPageContent has a column named PageId which is a foreign key to another
table. To insert a PageId, the value must exist in this other table...and
it obvioulsy doesn't.

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top