C# – Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints

asp.netcsql

Hey there. i am having a problem. i want to get info on a package from database and get the software linked to that package..

my db looks like this.

create table Package
(
    PackageID int identity,
    PackageName varchar(50) not null,
    PackageDiscription varchar(max) not null,
    primary key (PackageID)
)



create table Software
(
    SoftwareID varchar(50) not null,
    SoftwareName varchar(50) not null,
    DownloadPath varchar(100) not null,
    PackageID int,
    Primary key (SoftwareID),
    Foreign Key (PackageID) references Package on delete cascade on update cascade
)

what i am trying to do is this:

protected void BtnPackageSelect_Click(object sender, EventArgs e)
{
    SoftwareTableAdapters.PackageTableAdapter p = new SoftwareTableAdapters.PackageTableAdapter();
    SoftwareTableAdapters.SoftwareTableAdapter s = new SoftwareTableAdapters.SoftwareTableAdapter();

    foreach(var item in p.GetSelectedPackage(DropDownList1.SelectedValue))
    {
        string PackageName = item.PackageName;
        int PackageID = item.PackageID;
        string PackageDiscription = item.PackageDiscription;

        Label1.Text = "Name: " + PackageName;
        Label1.Visible = true;
        Label2.Text = "ID: " + PackageID;
        Label2.Visible = true;
        Label3.Text = "Discription: " + PackageDiscription;
        Label3.Visible = true;
        ListBox2.Visible = true;

        foreach (var software in s.GetSelectedIndexSoftware(PackageID)) <----- this is there the error come.
        {
            ListBox2.Items.Add(software.SoftwareName);
        }
    }
}

the method GetSelectedIndexSoftware(packageID) looks like this:

SELECT SoftwareName
FROM Software
WHERE (PackageID = @PackageID)

all i got in the database now is:

insert into Package values('1', '1')
insert into Package values('2', '2')

insert into Software values('1', '1', '1', null)
insert into Software values('2', '2', '2', null)
insert into Software values('3', '3', '3', null)
insert into Software values('4', '4', '4', null)

where all software's null have been updated with 1

am fairly new to coding so am a bit lost.. hopeone someone can help me out to find the problem

——EDIT 1—–
i have now tryed to alter the create of Software to not allow null, and then create a package first called not linked where all software that are not yet linked to a package will be linked to. On create it is created with 1 in PackageID.
It didnt help.

——EDIT 2—–
i have now tryed to not have a foreign key on Software table.
didnt helt either

Best Answer

I think the reason you are getting an error is because you did not assign a value to the PackageID of the Software table. Therefore, when you do a "... s.GetSelectedIndexSoftware(PackageID)" in the for loop you are getting null. In your insert statements, you are not assigning a value to PackageID.

insert into Software values('1', '1', '1', null)

should be

insert into Software values('1', '1', '1', n)

where n is an id value from the PackageID column in the Package table.

If that does not work. You might want to try to make the SoftwareID column as an integer identity column on the Software table.

Related Topic