By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,482 Members | 3,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,482 IT Pros & Developers. It's quick & easy.

INSERT statement conflicted with COLUMN FOREIGN KEY

P: n/a
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("s trConn"))
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("s trConn"))
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
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

"Tim::.." <myatix_at_hotmail.com> wrote in message
news:A0**********************************@microsof t.com...
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("s trConn"))
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("s trConn"))
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

Nov 19 '05 #2

P: n/a
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Tim::.." <myatix_at_hotmail.com> wrote in message
news:A0**********************************@microsof t.com...
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("s trConn"))
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("s trConn"))
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

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.