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
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