This is a migrated thread and some comments may be shown as answers.

You cannot vote on your own post 0 The INSERT statement conflicted with the FOREIGN KEY constraint

0 Answers 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 01 Aug 2012, 09:58 PM
I receive this error while trying to perform an INSERT with EntityDataSource and the RadGrid. The odd thing is that the value is in fact inserted, but the error persists. Perhaps it has something to do w/ the EF or a problem with the reference value for the User object.

I need to manually set the value for UserID when creating a Scenario and when I try to set it in RadGridScenarios_InsertCommand event I get the error. If I do not manually set via code and manually set it in the UI there is no error (unless I reference a UserID which doesn't exist in Users table), but only when I try to set via code.

Before you ask... Yes UserID actual exists in the Users table and Scenarios.UserID references Users.UserID.

SQL Code:

CREATE TABLE Users
(
    UserID                    int                IDENTITY PRIMARY KEY            NOT NULL
    ,FirstName                varchar(30)                            NOT NULL
    ,LastName                varchar(30)                            NOT NULL
    ,WindowsLogin            varchar(30)                            NOT NULL
    ,EmailAddress            varchar(30)
    ,[Disabled]                bit                DEFAULT 0
    ,Administrator            bit                DEFAULT 0
)
GO

INSERT INTO Users(FirstName, LastName, WindowsLogin) VALUES('MyFirst', 'MyLast', 'MMyLast')

GO

CREATE TABLE Scenarios
(
    ScenarioID            int IDENTITY PRIMARY KEY NOT NULL
    ,Title                            varchar(30) NOT NULL
    ,Notes                            varchar(1000)
    ,UserID                            int REFERENCES Users(UserID) NOT NULL
    ,DateCreated                    datetime DEFAULT(GETDATE())
)


ASPX Code
<telerik:RadGrid 
            ID="RadGridScenarios"
            runat="server" 
            AutoGenerateColumns="False" 
            CellSpacing="0" 
            DataSourceID="EntityDataSourceScenarios" 
            GridLines="None" 
            AllowAutomaticDeletes="True" 
            AllowAutomaticInserts="True" 
            AllowAutomaticUpdates="True"
            OnInsertCommand="RadGridScenarios_InsertCommand">

            <MasterTableView 
                DataKeyNames="ScenarioID" 
                DataSourceID="EntityDataSourceScenarios"
                CommandItemDisplay="Top">
                <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>

                <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>

                <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>

                <Columns>
                    <telerik:GridBoundColumn 
                        DataField="Title" 
                        FilterControlAltText="Filter Title column" 
                        HeaderText="Title" 
                        SortExpression="Title" 
                        UniqueName="Title">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn 
                        DataField="Notes" 
                        FilterControlAltText="Filter Notes column" 
                        HeaderText="Notes" 
                        SortExpression="Notes" 
                        UniqueName="Notes">
                    </telerik:GridBoundColumn>
                </Columns>

                <EditFormSettings>
                    <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
                </EditFormSettings>

            </MasterTableView>

            <FilterMenu EnableImageSprites="False"></FilterMenu>

        </telerik:RadGrid>

C# Code
protected void RadGridScenarios_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            Hashtable values = new Hashtable();
            item.ExtractValues(values);
            Scenario scenario = new Scenario();
            item.UpdateValues(scenario);
            scenario.UserID = 1 //this.Database.Users.Single(u => u.WindowsLogin == username).UserID;
      }

Thanks,

Steve

No answers yet. Maybe you can help?

Tags
Grid
Asked by
Steve
Top achievements
Rank 1
Share this question
or