Primary Key in Dynamic DataSet

J

JSheble

I have a dynamically created dataset, created thusly:

DataTable dtTime = new DataTable("times");
DataColumn [] dtColumns = new DataColumn[7];
dtColumns[0] = new DataColumn("id", System.Type.GetType("System.String"));
dtColumns[0].AutoIncrement = true;
dtColumns[0].AutoIncrementSeed = 1;
dtColumns[1] = new DataColumn("date",
System.Type.GetType("System.String"));
dtColumns[2] = new DataColumn("proj",
System.Type.GetType("System.String"));
dtColumns[3] = new DataColumn("name",
System.Type.GetType("System.String"));
dtColumns[4] = new DataColumn("desc",
System.Type.GetType("System.String"));
dtColumns[5] = new DataColumn("time",
System.Type.GetType("System.String"));
dtColumns[6] = new DataColumn("saved",
System.Type.GetType("System.Boolean"));
dtTime.Columns.AddRange(dtColumns);
dtTime.PrimaryKey = new DataColumn [] { dtColumns[0] };
this._TimeSheets.Tables.Add(dtTime);

The first column is an autoincrementing ID field, and is also set to be
the primary key.

Later on in my code, in an attempt to either add a new record (row) or
update an existing row, I'm using the following code:

oTask = oFrm.Task;
object [] newRow = new object[7];

if(oTask.TaskID == 0)
{
// add a new row
newRow[0] = null;
}
else
{
// should update already existing row
newRow[0] = oTask.TaskID;
}

newRow[1] = oTask.TaskDate;
newRow[2] = oTask.TaskProject;
newRow[3] = oTask.TaskName;
newRow[4] = oTask.TaskDescription;
newRow[5] = oTask.TaskTime;
newRow[6] = oTask.TaskReported;

this._TimeSheets.Tables["times"].LoadDataRow(newRow, true);

However, on the LoadDataRow call, I'm getting the following exception:

An unhandled exception of type 'System.Data.ConstraintException' occurred
in system.data.dll

Additional information: Column 'id' is constrained to be unique. Value
'1' is already present.

Well of course the id already has a value of '1', I want to update this
row, which is what the LoadDataRow is supposed to do. If it find the row,
based on the primary key, it updates it. Otherwise is inserts a new row.
So why would I be getting this error?? And how can I overcome it??

Thanx!
 
J

JSheble

I guess I found my own answer... dunno why this happens, but if I change
the line:

this._TimeSheets.Tables["times"].LoadDataRow(newRow, true);

to

this._TimeSheets.Tables["times"].BeginLoadData();
this._TimeSheets.Tables["times"].AcceptChanges();
this._TimeSheets.Tables["times"].LoadDataRow(newRow, true);
this._TimeSheets.Tables["times"].EndLoadData();

it works as expected... "'Curiouser and curiouser', said Alice..."

I have a dynamically created dataset, created thusly:

DataTable dtTime = new DataTable("times");
DataColumn [] dtColumns = new DataColumn[7];
dtColumns[0] = new DataColumn("id",
System.Type.GetType("System.String"));
dtColumns[0].AutoIncrement = true;
dtColumns[0].AutoIncrementSeed = 1;
dtColumns[1] = new DataColumn("date",
System.Type.GetType("System.String"));
dtColumns[2] = new DataColumn("proj",
System.Type.GetType("System.String"));
dtColumns[3] = new DataColumn("name",
System.Type.GetType("System.String"));
dtColumns[4] = new DataColumn("desc",
System.Type.GetType("System.String"));
dtColumns[5] = new DataColumn("time",
System.Type.GetType("System.String"));
dtColumns[6] = new DataColumn("saved",
System.Type.GetType("System.Boolean"));
dtTime.Columns.AddRange(dtColumns);
dtTime.PrimaryKey = new DataColumn [] { dtColumns[0] };
this._TimeSheets.Tables.Add(dtTime);

The first column is an autoincrementing ID field, and is also set to be
the primary key.

Later on in my code, in an attempt to either add a new record (row) or
update an existing row, I'm using the following code:

oTask = oFrm.Task;
object [] newRow = new object[7];

if(oTask.TaskID == 0)
{
// add a new row
newRow[0] = null;
}
else
{
// should update already existing row
newRow[0] = oTask.TaskID;
}

newRow[1] = oTask.TaskDate;
newRow[2] = oTask.TaskProject;
newRow[3] = oTask.TaskName;
newRow[4] = oTask.TaskDescription;
newRow[5] = oTask.TaskTime;
newRow[6] = oTask.TaskReported;

this._TimeSheets.Tables["times"].LoadDataRow(newRow, true);

However, on the LoadDataRow call, I'm getting the following exception:

An unhandled exception of type 'System.Data.ConstraintException'
occurred in system.data.dll

Additional information: Column 'id' is constrained to be unique. Value
'1' is already present.

Well of course the id already has a value of '1', I want to update this
row, which is what the LoadDataRow is supposed to do. If it find the
row, based on the primary key, it updates it. Otherwise is inserts a
new row. So why would I be getting this error?? And how can I overcome
it??

Thanx!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top