Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi it is my code but i have error!
(error say "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Person_Groups". The conflict occurred in database "PhoneBookDB", table "dbo.Groups", "GroupID".
The statement has been terminated.


C#
  protected void btnEntrance_Click(object sender, EventArgs e)
        {
            PhoneBookDB.GroupsDataTable group = new PhoneBookDB.GroupsDataTable();
            PhoneBookDBTableAdapters.GroupsTableAdapter groupDA = new 


PhoneBookDBTableAdapters.GroupsTableAdapter();
            groupDA.Fill(group);
            groupDA.Insert(ddlRelation.SelectedItem.Text);
        
            PhoneBookDB.PersonDataTable person = new PhoneBookDB.PersonDataTable();
            PhoneBookDBTableAdapters.PersonTableAdapter personAD = new PhoneBookDBTableAdapters.PersonTableAdapter();
            personAD.Fill(person);
            personAD.Insert(txtFirstName.Text, txtLastName.Text, txtPhone.Text, param(), txtAddress.Text, txtOtherPhone.Text, txtFax.Text,Convert.ToInt16(ddlRelation.SelectedValue));
            
        }

        public bool param()
    {
        if (rdbtnMan.Checked)
        {
            return true;
        }
       
        else
        {
            return false;
        }
    }

and it is my table
SQL
CREATE TABLE [dbo].[Person](
    [PersonID] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](50) NOT NULL,
    [LastName] [nvarchar](50) NOT NULL,
    [Phone] [nchar](20) NOT NULL,
    [Sex] [bit] NOT NULL,
    [Address] [text] NULL,
    [OtherPhone] [nchar](20) NULL,
    [Fax] [nchar](20) NULL,
    [GroupID] [int] NOT NULL(it is forignkey from group),

and group table
SQL
CREATE TABLE [dbo].[Groups](
    [GroupID] [int] IDENTITY(1,1) NOT NULL,
    [Relation] [nvarchar](20) NOT NULL,
Posted
Updated 3-May-13 13:29pm
v4
Comments
CHill60 3-May-13 19:31pm    
Most of the columns on your Person table are defined as NOT NULL. Step through your code and confirm that all of the contents of personAD.Insert(txtFirstName.Text, txtLastName.Text, txtPhone.Text, param(), txtAddress.Text, txtOtherPhone.Text, txtFax.Text,Convert.ToInt16(ddlRelation.SelectedValue)); that need to be populated actually are

1 solution

You need to have the GroupID before you can insert it to the Person table. Right now, you are doing everything in one transaction, which causes the problem. The GroupID is an Identity field (auto-incremented), so you need to to insert that first, return it to the front-end, and use that ID when inserting the Person information.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900