Dataset constraint violoation with null foreign key value

M

Martin

Hi,

I'm loading a dataset from a cached viewstate version of the dataset created
on a previous http request. When I read the call ReadXml on the typed
dataset, I get:
"Failed to enable constraints. One or more rows contain values violating
non-null, unique, or foreign-key constraints"
I suspect this is due to a field in one of the tables which can be null, but
if it is not null it is a foreign key to another dataset table. At present
the field in this table is null (in the database).

What attributes of the dataset table field, or the dataset constraint should
I set to indicate null values don't violate the constraint?

Thanks
Martin
 
M

Miha Markic [MVP C#]

Hi Martin,

DataColumn.AllowDBNull perhaps.
BTW, to find out which row is causing errors, just iterate through all rows
and check DataRow.HasErrors and RowError properites.
 
M

Martin

Hi Miha,

Sounds good. I don't see that property reflected in the properties for the
field element in my typed dataset. I'm looking at the xsd file in vs.net

??

Martin
Miha Markic said:
Hi Martin,

DataColumn.AllowDBNull perhaps.
BTW, to find out which row is causing errors, just iterate through all rows
and check DataRow.HasErrors and RowError properites.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Martin said:
Hi,

I'm loading a dataset from a cached viewstate version of the dataset created
on a previous http request. When I read the call ReadXml on the typed
dataset, I get:
"Failed to enable constraints. One or more rows contain values violating
non-null, unique, or foreign-key constraints"
I suspect this is due to a field in one of the tables which can be null, but
if it is not null it is a foreign key to another dataset table. At present
the field in this table is null (in the database).

What attributes of the dataset table field, or the dataset constraint should
I set to indicate null values don't violate the constraint?

Thanks
Martin
 
M

Martin

I have tried setting EnforceConstraints false before doing ReadXml, and then
setting EnforceConstraints true after ReadXml, incase the dataset was being
populated in an order that created the problem - no success.

I also set EnforceConstraints true just before serialising the original
dataset to a string, in case it was breaking constraints there but not
reporting it. Constraints are fine at this point.

So it seems to be something to do with the (de)serialisation of the dataset

Code to serialise is
myDS.EnforceConstraints=true;
System.IO.StringWriter sw = new System.IO.StringWriter();
myDS.WriteXml(sw);
ViewState["myDS"] = sw.ToString();

To deserialise is:

System.IO.StringReader sr = new
System.IO.StringReader((string)(ViewState["MyDS"]));
myDS.EnforceConstraints=false;
myDS.ReadXml(sr);
myDS.EnforceConstraints=true; // exception thrown here
 
M

Martin

I've tried setting AllowDBNull for the Dataset, but that's made no
difference.

I have now set NullValue for the field that can be null in the xsd file for
the dataset. This seems to have broken vs.net. I am now getting "The
custom tool 'MSDataSetGenerator' failed while processing file myDS.xsd"

Can't seem to unbreak it :-(

Martin

Miha Markic said:
Hi Martin,

DataColumn.AllowDBNull perhaps.
BTW, to find out which row is causing errors, just iterate through all rows
and check DataRow.HasErrors and RowError properites.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Martin said:
Hi,

I'm loading a dataset from a cached viewstate version of the dataset created
on a previous http request. When I read the call ReadXml on the typed
dataset, I get:
"Failed to enable constraints. One or more rows contain values violating
non-null, unique, or foreign-key constraints"
I suspect this is due to a field in one of the tables which can be null, but
if it is not null it is a foreign key to another dataset table. At present
the field in this table is null (in the database).

What attributes of the dataset table field, or the dataset constraint should
I set to indicate null values don't violate the constraint?

Thanks
Martin
 
M

Martin

I've unbroken the MSDataSetGenerator by removing the NullValue attribute in
the XML view of msDS.xsd.

Still stuck on constraint execption violation after deserialising my
dataset.

Anyone?
Martin

Martin said:
I've tried setting AllowDBNull for the Dataset, but that's made no
difference.

I have now set NullValue for the field that can be null in the xsd file for
the dataset. This seems to have broken vs.net. I am now getting "The
custom tool 'MSDataSetGenerator' failed while processing file myDS.xsd"

Can't seem to unbreak it :-(

Martin

Miha Markic said:
Hi Martin,

DataColumn.AllowDBNull perhaps.
BTW, to find out which row is causing errors, just iterate through all rows
and check DataRow.HasErrors and RowError properites.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Martin said:
Hi,

I'm loading a dataset from a cached viewstate version of the dataset created
on a previous http request. When I read the call ReadXml on the typed
dataset, I get:
"Failed to enable constraints. One or more rows contain values violating
non-null, unique, or foreign-key constraints"
I suspect this is due to a field in one of the tables which can be
null,
but
if it is not null it is a foreign key to another dataset table. At present
the field in this table is null (in the database).

What attributes of the dataset table field, or the dataset constraint should
I set to indicate null values don't violate the constraint?

Thanks
Martin
 
M

Miha Markic [MVP C#]

Hi Martin,

Did you try iterating through rows and tables (HasErrors) to see where the
problem is?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Martin said:
I have tried setting EnforceConstraints false before doing ReadXml, and then
setting EnforceConstraints true after ReadXml, incase the dataset was being
populated in an order that created the problem - no success.

I also set EnforceConstraints true just before serialising the original
dataset to a string, in case it was breaking constraints there but not
reporting it. Constraints are fine at this point.

So it seems to be something to do with the (de)serialisation of the dataset

Code to serialise is
myDS.EnforceConstraints=true;
System.IO.StringWriter sw = new System.IO.StringWriter();
myDS.WriteXml(sw);
ViewState["myDS"] = sw.ToString();

To deserialise is:

System.IO.StringReader sr = new
System.IO.StringReader((string)(ViewState["MyDS"]));
myDS.EnforceConstraints=false;
myDS.ReadXml(sr);
myDS.EnforceConstraints=true; // exception thrown here

Martin said:
Hi,

I'm loading a dataset from a cached viewstate version of the dataset created
on a previous http request. When I read the call ReadXml on the typed
dataset, I get:
"Failed to enable constraints. One or more rows contain values violating
non-null, unique, or foreign-key constraints"
I suspect this is due to a field in one of the tables which can be null, but
if it is not null it is a foreign key to another dataset table. At present
the field in this table is null (in the database).

What attributes of the dataset table field, or the dataset constraint should
I set to indicate null values don't violate the constraint?

Thanks
Martin
 

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