Ben Santiago

I am trying to find out if a DataTable object has any changes that need to be saved to it's SQL Database using the Update() method. I looked throughout the properties of the DataTable object and can't find a Dirty property. But I did find a GetChanges() method. Problem is whenever I execute the GetChanges() method, which claims to return a DataTable of records that have been modified, I get the following error:

MissingMethodException was unhandled

No parameterless constructor defined for this object.

Does anyone know why this is Or better yet, anyone know how I can accomplish what I am trying to do




Re: Visual Basic Language Problem with GetChanges() from a DataTable Object

DMan1

How are you calling this method...please show the local code you are using!




Re: Visual Basic Language Problem with GetChanges() from a DataTable Object

Ben Santiago

Ok...here is my code line that crashes...

If Me.DataSet.Tables("tblCodes").GetChanges().Rows.Count > 0 Then

The Objects are as follows:
     Me = Form
     DataSet = Custom DataSet Object (Inheriting From System.Data.DataSet)
     Tables("tblCodes") = Custom DataTable Object (Inheriting From System.Data.DataTable)

All I need to know is if ANY changes have been made to the Table that have not been pushed up to the SQL Database yet.  Any ideas

 

 






Re: Visual Basic Language Problem with GetChanges() from a DataTable Object

DMan1

Try breaking out your code...something along these lines should work:

' Create variable for temporary DataSet.

Dim changesDataSet As DataSet

If Me.dataSet.HasChanges() Then

changesDataSet = DataSet.GetChanges()

End If

If Me.changesDataSet.Tables("tblCodes").Rows.Count > 0 Then

You can also use the row state parameter to get specific types of changes






Re: Visual Basic Language Problem with GetChanges() from a DataTable Object

Ben Santiago

Me.DataSet.HasChanges() returns a FALSE, even when changes have been made.




Re: Visual Basic Language Problem with GetChanges() from a DataTable Object

DMan1

Are you calling 'AcceptChanges' before has changes




Re: Visual Basic Language Problem with GetChanges() from a DataTable Object

Ben Santiago

No, wasn't aware I needed to. The Table appears to have the data that was entered on the Grid, why would I need to AcceptChanges




Re: Visual Basic Language Problem with GetChanges() from a DataTable Object

DMan1

You don't...it is a common error though...getChanges only gets changes from the last time AcceptChanges was called...Do you have any code in the grid that updates the database




Re: Visual Basic Language Problem with GetChanges() from a DataTable Object

Ben Santiago

The only time I call Update() is when I click the Done button on my form, or in another routine I have that switches data viewed in the grid, which happens AFTER this portion of code that is crashing.  So in answer to your question, before this line of code runs, Update() has not been called. 

And also I do not use AcceptChanges() anywhere.

Could there possibly be some sort of problem because I am using Custom versions of the DataSet & DataTable object   Could the inherited object I am using not properly inheriting the HasChanges() and GetChanges() methods






Re: Visual Basic Language Problem with GetChanges() from a DataTable Object

Ben Santiago

Ok, since no one had an answer I continued my search. After digging for what seemed like forever, I figured out what was happening, so for those people encountering the same problem, here is your solution...

There is a few things GetChanges() does in order to return a DataTable filled with modified rows. The first thing it does is call Clone(). This method creates a new instance of the original DataTable, along with all the fields within the table. It then uses this empty DataTable and fills it with all the rows that have been modified and returns that back to the calling line of code.

Problem is that when Clone() is called, it instantiates a new copy of the object, using the Parameterless Constructor New() of the object. If you did not define this constructor, then you encounter the error "MissingMethodException was unhandled. No parameterless constructor defined for this object." Now in my case, I could not allow my derived DataTable to instantiate without the parameters I set in the Constructor, so how was I going to get this to work I thought. Well this is how.

I overrode the Clone() method of my derived DataTable, I then created a new instance of the same derived DataTable using the Activator.CreateInstance() method, and I passed to it the ObjectType and an Array with all the parameters needed. This insures that your class is recreated using the Parametric Contructor you forst designed. Now that you have a new derived DataTable that is identical to the current DataTable (minus the data) you have to create the fields. So I created a simple For Each loop that iterated through the Me.Columns collection, and added them to the cloned table (ObjClonedTable.Columns.Add(objOriginalTable.ColumnName, objOriginalTable.DataType)). Once this was done, you return the cloned table, and there, you are done.