My question: Somehow I am able to save a corrupt dataset to an xml file. With corrupt I mean a dataset that does not enforce its constraints. How do I correctly enforce it?
What did I do? I created a dataset with constraints on it. The constraint was to enforce a maximum length on a column. I created a form with a splitter, where I could select a record in a listbox on the left and edit the selected recordon the right inedit boxes. I added a save button. Where I called if(Validate()) { dataSet.WriteToXml("FileName.xml"); }
My problem: If I load data, change a record with an incorrect value and press the save button, it saves an incorrect dataset to file. If I load the file, only then it gives an error. Somehow I must perform a call that checks the constraints. Which call should do that?
- Edited byEdsger Tuesday, August 11, 2009 4:07 PM
- Edited byEdsger Tuesday, August 11, 2009 4:09 PM
-
| | Edsger Tuesday, August 11, 2009 4:06 PM | Hi Edsger,
This sounds similar to a problem I see asked abouta lot and one I just answered in another post a few mintues ago.
Data in a DataRow has several different versions. First, there's the original version. Then, when it's being edited, it has a Proposed version and once it's done being edited, that becomes the Current version. Sometimes when editing, the row is left in the Proposed state and the Edit needs to be ended programmatically.
I think this might be your problem. Here's a method I *always* call before I attempt to check for .HasChanges() before saving data:
protected virtual void CommitProposedChanges(DataSet ds)
{
if (ds == null)
return;
for (int nTable = 0; nTable < ds.Tables.Count; nTable++)
{
for (int nRow = 0; nRow < ds.Tables[nTable].Rows.Count; nRow++)
{
if (ds.Tables[nTable].Rows[nRow].HasVersion(DataRowVersion.Proposed))
{
ds.Tables[nTable].Rows[nRow].EndEdit();
}
}
}
}
~~Bonnie Berent [C# MVP] - Marked As Answer byEdsger Thursday, September 17, 2009 9:04 AM
-
| | BonnieB Sunday, August 16, 2009 2:17 PM | Do you have the DataSet.EnforceConstraints property set to True?
Hope this helps. www.insteptech.com ;
msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! | | DeborahK Tuesday, August 11, 2009 5:37 PM | Yes,DataSet.EnforceConstraints property is set to true.
this error only occurs when I directly save the dataset after setting the "illegal" value (for details see my start post). If I select another item in the list (ie change the current item in de binding source), an exception is thrown, which is what I expect.
However, calling Validate() and writing the dataset to XML seems to write an illegal dataset to file, which is undesired behavior.
| | Edsger Thursday, August 13, 2009 9:27 AM | Hi Edsger, Could you please show us your code to add that maximum length constraints? Sincerely, Kira Qian Please mark the replies as answers if they help and unmark if they don't. | | Kira Qian Friday, August 14, 2009 2:30 AM | Hi Edsger,
This sounds similar to a problem I see asked abouta lot and one I just answered in another post a few mintues ago.
Data in a DataRow has several different versions. First, there's the original version. Then, when it's being edited, it has a Proposed version and once it's done being edited, that becomes the Current version. Sometimes when editing, the row is left in the Proposed state and the Edit needs to be ended programmatically.
I think this might be your problem. Here's a method I *always* call before I attempt to check for .HasChanges() before saving data:
protected virtual void CommitProposedChanges(DataSet ds)
{
if (ds == null)
return;
for (int nTable = 0; nTable < ds.Tables.Count; nTable++)
{
for (int nRow = 0; nRow < ds.Tables[nTable].Rows.Count; nRow++)
{
if (ds.Tables[nTable].Rows[nRow].HasVersion(DataRowVersion.Proposed))
{
ds.Tables[nTable].Rows[nRow].EndEdit();
}
}
}
}
~~Bonnie Berent [C# MVP] - Marked As Answer byEdsger Thursday, September 17, 2009 9:04 AM
-
| | BonnieB Sunday, August 16, 2009 2:17 PM |
|