Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataSet inherit problem

Status
Not open for further replies.

sbdSolna

Programmer
Aug 19, 2004
24
DK
I have a DataSet called DataHolder, that holdes several tables. The DataHolder class is a UserControl, that also holds the connection to the database.
When the Dataholder starts it fills in different SqlDataAdapters with commands used for selects, deletes and updates of different tables.
Here is the problem, one of the tables has no inserts and therefore I use this code.

Code:
if (this.dataHolder.HasChanges(DataRowState.Modified)) {
    DataSet tempDS = this.dataHolder.GetChanges (DataRowState.Modified);
  if  tempDS.Tables.ContainsDataHolder.TableNameProdDataPre)){ 			this.dataHolder.SqlPreviewDataAdapter.Update (tempDS, DataHolder.TableNameProdDataPre);
						this.dataHolder.AcceptChanges();
 }
}

That code does not work the statement this.dataHolder.GetChanges(DataRowState.Modified) goes through the Constructor of DataHolder.

If I do like this the code works ok

Code:
if (this.dataHolder.HasChanges(DataRowState.Modified)) {
    DataSet tempDS = this.dataHolder.GetChanges();
    if (tempDS.Tables.Contains(DataHolder.TableNameProdDataPre)) {
					this.dataHolder.SqlPreviewDataAdapter.Update(tempDS, DataHolder.TableNameProdDataPre);
						this.dataHolder.AcceptChanges();
  }
}

Do anyone know why?

Thanks in advance.

 
Post the declaration of your GetChanges Function. It looks to me like there isnt one that takes an additional parameter.

When you say it doesnt work, what happens, any error messages? What happens if you step through it in the debugger?

K
 
Here is an example from Msdn on HasChanges and GetChanges


Code:
   if(ds.HasChanges(DataRowState.Modified | DataRowState.Added)& ds.HasErrors){
      // Use GetChanges to extract subset.
      xSet = ds.GetChanges(DataRowState.Modified|DataRowState.Added);
      PrintValues(xSet, "Subset values");
      // Insert code to reconcile errors. In this case, we'll reject changes.
      foreach(DataTable xTable in xSet.Tables){
         if (xTable.HasErrors){
            foreach(DataRow xRow in xTable.Rows){
               //Console.WriteLine(xRow["Item"]);
                  if((int)xRow["Item",DataRowVersion.Current ]> 100){
                  xRow.RejectChanges();
                  xRow.ClearErrors();
               }
            }
         }
      }

From the example:

xSet = ds.GetChanges(DataRowState.Modified|DataRowState.Added);


Yes GetChanges can take several parameters but I'm only using one.

When I step through the code, with F11. I come down to the constructor of DataHolder that is inherit from the DataSet. But the code works with GetChanges with no parameters

 
So what happens when your code runs? Nothing gets put into the dataset?

K
 
You can't see the problem, if you not debug the code with.
But DataHolder also has a other function. That is to switch between production and test tables.
I you only work in production is no problem, but if you work with test, the constructor of DataHolder going to switch to production tables during GetChanges(), and that is a really big problem.
 
in the first sample code I notice that you have sintax error with parenthesis:
Code:
if (this.dataHolder.HasChanges(DataRowState.Modified))
{
	DataSet tempDS = this.dataHolder.GetChanges (DataRowState.Modified);
	if  [COLOR=red]([/color red]tempDS.Tables.ContainsDataHolder.TableNameProdDataPre) //[COLOR=red])[/color red]
	{            
		this.dataHolder.SqlPreviewDataAdapter.Update (tempDS, DataHolder.TableNameProdDataPre);
		this.dataHolder.AcceptChanges();
	}
}

but try with this added code lines:
Code:
			if (this.dataHolder.HasChanges(DataRowState.Modified))
			{
				DataSet tempDS = new DataSet();
				tempDS = (DataSet)this.dataHolder.GetChanges(DataRowState.Modified);
				if (tempDS.Tables.ContainsDataHolder.TableNameProdDataPre)
				{            
					this.dataHolder.SqlPreviewDataAdapter.Update(tempDS, DataHolder.TableNameProdDataPre);
					this.dataHolder.AcceptChanges();
				}
			}
[code]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top