Rows.Add and DataSet Constraints

C

Christopher Weaver

I'm trying to insert a new Row within an existing Table within an existing
DataSet using the following:

DataRow NewTaskRow = dsTaskActivities.Tables["Tasks"].NewRow();
dsTaskActivities.Tables["Tasks"].Rows.Add(NewTaskRow);

I'm using the code from the help text to remove the constraints within
dsTaskActivities before executing the Add

public static void ClearConstraints(DataSet ds)
{
foreach (DataTable t in ds.Tables)
t.Constraints.Clear();
}

However, I receive this error message:

Remove foreign key constraint 'TaskActivities' first.

'TaskActivities' is the name of a Relation in dsTaskActivities that defines
the master / detail relationship between the two Tables within the
dsTaskActivities . So I inserted this before the call to ClearConstraints:

dsTaskActivities.Relations.Remove("TaskActivities");

This additional line of code did not raise an exception, but it didn't
remove the foreignkey constraint either. I'm still receiving the same error
message.

Is there another way to go about this? I'm simply trying to insert a new
record in the DataSet on the master table side. I'm not including a value
in the unique ID field because it is added automatically by the server.

Any suggestions will be greatly appreciated.
 
R

Rodger Constandse

Hi,

Not sure if this is what you want, but you can turn off constraint checking at
the DataSet level while you do your operation, and then turn it back on once
everything is set up.

dsTaskActivities.EnforceConstraints = false;

.... do your stuff ...

dsTaskActivities.EnforceConstraints = true;

Best regards,

Rodger

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDiagramEditor.com>
 
C

Christopher Weaver

This is precisely what I was looking for. When I couldn't find it I went
with the constraint removal route. Thank you!


Rodger Constandse said:
Hi,

Not sure if this is what you want, but you can turn off constraint
checking at the DataSet level while you do your operation, and then turn
it back on once everything is set up.

dsTaskActivities.EnforceConstraints = false;

... do your stuff ...

dsTaskActivities.EnforceConstraints = true;

Best regards,

Rodger

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDiagramEditor.com>

Christopher said:
I'm trying to insert a new Row within an existing Table within an
existing DataSet using the following:

DataRow NewTaskRow = dsTaskActivities.Tables["Tasks"].NewRow();
dsTaskActivities.Tables["Tasks"].Rows.Add(NewTaskRow);

I'm using the code from the help text to remove the constraints within
dsTaskActivities before executing the Add

public static void ClearConstraints(DataSet ds)
{
foreach (DataTable t in ds.Tables)
t.Constraints.Clear();
}

However, I receive this error message:

Remove foreign key constraint 'TaskActivities' first.

'TaskActivities' is the name of a Relation in dsTaskActivities that
defines the master / detail relationship between the two Tables within
the dsTaskActivities . So I inserted this before the call to
ClearConstraints:

dsTaskActivities.Relations.Remove("TaskActivities");

This additional line of code did not raise an exception, but it didn't
remove the foreignkey constraint either. I'm still receiving the same
error message.

Is there another way to go about this? I'm simply trying to insert a new
record in the DataSet on the master table side. I'm not including a
value in the unique ID field because it is added automatically by the
server.

Any suggestions will be greatly appreciated.
 

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