Ok, I can't figure this one out. I have a grid with a combo box that has the following code:
On update I recieve the following error:
It is trying to put a value of 0 into the table when the referenced table does not contain a record with an ID of 0. The following is the insert code:
The Day_of_Week_ID is not a required field on the Master table. HELP! Thanks!
Brenda
Code:
</asp:TemplateField>
<asp:TemplateField HeaderText="Day of the Week" SortExpression="Day_of_Week">
<EditItemTemplate>
<asp:DropDownList ID="EditDay_of_Week" runat="server"
DataSourceID="EditDay_of_WeekDataSource" DataTextField="Day_of_Week"
DataValueField="Day_of_Week_ID" SelectedValue='<%# Bind("Day_of_Week_ID") %>'
AppendDataBoundItems="true">
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:ObjectDataSource ID="EditDay_of_WeekDataSource" runat="server"
OldValuesParameterFormatString="{0}" SelectMethod="GetDay_of_Week"
TypeName="DayofWeekBLL"></asp:ObjectDataSource>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="NewDay_of_Week_ID" runat="server" DataSourceID="Day_of_WeekDataSource"
DataTextField="Day_of_Week" DataValueField="Day_of_Week_ID" Height="16px" SkinID="BlueGV"
Width="74px" SelectedValue='<%# Bind("Day_of_Week_ID") %>'
AppendDataBoundItems="true">
<asp:ListItem Value="">--Select--</asp:ListItem>
</asp:DropDownList>
<asp:ObjectDataSource ID="Day_of_WeekDataSource" runat="server"
OldValuesParameterFormatString="{0}" SelectMethod="GetDay_of_Week"
TypeName="DayofWeekBLL"></asp:ObjectDataSource>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("Day_of_Week") %>'></asp:Label>
</ItemTemplate>
<FooterStyle VerticalAlign="Top" />
<ItemStyle Width="75px" />
Code:
{"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Tbl_Recurring_Event_Tbl_Day_of_Week". The conflict occurred in database "Brenda_Test", table "dbo.Tbl_Day_of_Week", column 'Day_of_Week_ID'. The statement has been terminated."}
Code:
Protected Sub ObjectDataSource1_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles ObjectDataSource1.Inserting
Dim NewDay_of_Week_ID As DropDownList = gv.FooterRow.FindControl("NewDay_of_Week_ID")
Dim Day_of_Week As Nullable(Of Short) = Nothing
If Not String.IsNullOrEmpty(NewDay_of_Week_ID.SelectedValue) Then
Day_of_Week = NewDay_of_Week_ID.SelectedValue
e.InputParameters("Day_of_Week_ID") = Convert.ToInt32(NewDay_of_Week_ID.SelectedValue)
End If
End Sub
Code:
<System.ComponentModel.DataObjectMethodAttribute _
(System.ComponentModel.DataObjectMethodType.Insert, True)> _
Public Function AddRecurring_Event( _
ByVal Recurring_Event_ID As Integer, ByVal Day_of_Week As String) _
As Boolean
Dim Recurring_Events As New CablesLog.Tbl_Recurring_EventDataTable()
Dim Recurring_Event As CablesLog.Tbl_Recurring_EventRow = Recurring_Events.NewTbl_Recurring_EventRow
Recurring_Event.Day_of_Week_ID = Day_of_Week_ID
Recurring_Events.AddTbl_Recurring_EventRow(Recurring_Event)
Dim rowsAffected As Integer = Adapter.Update(Recurring_Events)
Return rowsAffected = 1
End Function
Brenda