简体   繁体   English

无法启用约束。 一行或多行包含违反非null,唯一或外键约束的值

[英]Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints

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: 方法GetSelectedIndexSoftware(packageID)如下所示:

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 所有软件的null都已用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. ------编辑1 -----我现在已经尝试将软件的创建改为不允许null,然后创建一个名为not linked的包,其中所有尚未链接到包的软件将是链接到。 On create it is created with 1 in PackageID. 在创建时,它在PackageID中创建为1。 It didnt help. 它没有帮助。

------EDIT 2----- i have now tryed to not have a foreign key on Software table. ------编辑2 -----我现在试图在Software表上没有外键。 didnt helt either 也没有停止

I think the reason you are getting an error is because you did not assign a value to the PackageID of the Software table. 我认为您收到错误的原因是因为您没有为Software表的PackageID分配值。 Therefore, when you do a "... s.GetSelectedIndexSoftware(PackageID)" in the for loop you are getting null. 因此,当您在for循环中执行“... s.GetSelectedIndexSoftware(PackageID)”时,您将获得null。 In your insert statements, you are not assigning a value to PackageID. 在您的插入语句中,您没有为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. 其中n是Package表中PackageID列中的id值。

If that does not work. 如果这不起作用。 You might want to try to make the SoftwareID column as an integer identity column on the Software table. 您可能希望尝试将SoftwareID列作为Software表上的整数标识列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法启用约束。 一行或多行包含违反非空、唯一或外键约束的值 - Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints 这正是错误:无法启用约束。 一行或多行包含违反非空、唯一或外键约束的值 - Exactly this is the error: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints 无法启用约束。 一或多个行包含违反非null,唯一或外键约束的值。 ASP.NET C# - Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. asp.net c# 无法启用约束。 一或多个行包含违反非null,唯一或外键约束的值。 MyDataset.Tables [0]。合并 - Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. MyDataset.Tables[0].Merge C#SQL:无法启用约束。 一或多个行包含违反非空,唯一或外键约束的值 - C# SQL: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints DataTable.Load,一行或多行包含违反非空、唯一或外键约束的值 - DataTable.Load, One or more rows contain values violating non-null, unique, or foreign-key constraints 操作失败:无法更改关系,因为一个或多个外键属性不可为空 - The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable 操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系。 - The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. 操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系asdf - The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable, asdf 无法启用约束。 使用数据表适配器时 - Failed to enable constraints. When using a data table adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM