I have a small dataset which has most of its schema defined at design time, but I add a few columns to one of its tables at runtime. My problem is how do I reset those changes?
The dataset is not linked to any datasource. It just has its own table which I “manually” populate. The static columns have been added to the datatable at design time. However, I add a few columns as runtime based on a list of categories. Like this:
For Each aCategory In theCategories
theDataTable.Columns.Add(aCategory.CategoryName, System.Type.GetType("System.Boolean"))
Next
The dataset is used in a small dialog that pops up within my application. I add the dynamic columns when the dialog is opened by using the Load event. Everything works great the first time. But if you close the dialog and reopen it, there are problems. My startup routine tries adding the dynamic columns again, but an error is thrown saying the column already exists. I have tried a lot of things and did some reading but I can’t figure out a way to reset the dataset when the dialog is closed. The only solution is to close the whole application and reopen it. I have tried the following in the Leave event of the dialog form:
theDataSet.Reset()
theDataSet.Dispose()
theDataSet.RejectChanges()
None of these seem to make a difference and the error continues. I have also tried applying the same methods to the table in addition to the dataset. Such as:
theDataSet.Tables(0).Reset()
etc.
Any ideas on how to completely obliterate the dataset when the form closes? I gave my examples in VB, but I can digest answers in either VB or C#.
I have answered my own question. It turns out that my problem was not really anything to do with datasets or datatables but rather dialog boxes.
I was calling the form in which the dataset was used as follows:
After reading this article: http://msdn.microsoft.com/en-us/library/aw58wzka.aspx I realized that the dispose method is only automatically called if you use .show not .showdialog. So i changed the code that called the form to:
And it solved the problem. Now all the objects associated with the form are properly disposed when the form closes. Which means that the dataset is reset to its design time default, or rather a new one is created when the form is opened again.