Once you define a DataColumn as primary key column, it's Unique becomes true
and AllowDBNull becomes false automatically.
Thanks, I was playing around and this indeed appears to be the case. However, I
am still having problems trying to delete a row from my DataSet.
Here is the DataTable definition:
DataTable tblProject = database.Tables.Add("TableProject");
tblProject.Columns.Add("ProjectName", typeof(string));
[more columns added...]
//primary key definition
DataColumn project_ID = tblProject.Columns.Add("Project_ID", typeof(Int32));
tblProject.PrimaryKey = new DataColumn[] { tblProject.Columns["Project_ID"] };
project_ID.AutoIncrement = true;
project_ID.AutoIncrementSeed = 1;
project_ID.AutoIncrementStep = 1;
Here is how I am trying to delete the row:
[drPrj is a DataRow object]
if (drPrj != null)
{
drPrj.Delete();
dtPrj.AcceptChanges();
}
This results in an apparent key violation - "Project_ID cannot be null"
"TableProject" is the parent table and "TableConfiguration" is the child table.
Here is the FK definition:
DataColumn parentCol =
database.Tables["TableProject"].Columns["Project_ID"];
DataColumn childCol =
database.Tables["TableConfiguration"].Columns["Project_ID"];
ForeignKeyConstraint fkPC =
new ForeignKeyConstraint("ProjectConfigurationFK", parentCol, childCol);
fkPC.DeleteRule = Rule.Cascade;
fkPC.AcceptRejectRule = AcceptRejectRule.Cascade;
database.Tables["TableConfiguration"].Constraints.Add(fkPC);
If you see anything wrong here, please let me know...
Why am I getting that error?
Full text of error:
System.Data.NoNullAllowedException was unhandled
Message="Column 'Project_ID' does not allow nulls."
Source="System.Data"
Thanks in advance.