Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Changes To DataSet Not Being Saved
Message
De
03/10/2008 11:36:48
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Divers
Thread ID:
01352069
Message ID:
01352587
Vues:
25
>The EndEdit worked fine. Thanks Bonnie.

No problem! You're welcome! =0)

>Any reason why I shouldn't iterate through all the tables & rows and call EndEdit on each row here in this method? This would put it one place as opposed to all the different areas that work with DS's.

I'd do it slightly different than you did. I used to use a method I called CommitProposedChanges (note that it only does the EndEdit if the row is in the Proposed state):
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();
			}
		}
	}
}
I have since changed the way we do this, by putting a call to a static method in a Control's Validated event handler, but depending on the way you do your DataBinding, you may or may not be able to use that. The above CommitProposedChanges method worked great, although it may be time consuming for large datasets with many rows.

Just as an FYI, if you're interested, here's the static method I use instead, the one I call from Validated event handlers:
public static void EndEdit(Binding oBinding, CurrencyManager oCurrency)
{
	if (oBinding == null || oBinding.DataSource == null || oCurrency == null)
		return;

	EndEdit(oBinding.DataSource, oCurrency.Position);
}
public static void EndEdit(object DataSource, int nRow)
{
	if (nRow < 0)
		return;

	// Originally, I was going to simply use the .EndCurrentEdit() of the
	// Currency object, but for certain controls (the ComboBox being the first
	// one where I noticed a problem), the .EndCurrentEdit() caused the 
	// DataSet.HasChanges() to be true when there aren't any changes, whereas
	// the Row.EndEdit() did not cause this to happen. 
	
	// A simple check as to whether the Row.HasVersion(DataRowVersion.Proposed)
	// before doing the .EndCurrentEdit() would get around this, but since I would
	// still have to do the work to get the Row out of the DataSource (because the
	// DataSource can be a DataTable or a DataView), I might as well use .EndEdit()

	if (DataSource is DataTable && nRow < ((DataTable)DataSource).Rows.Count)
		((DataTable)DataSource).Rows[nRow].EndEdit();
	else
	if (DataSource is DataView && nRow < ((DataView)DataSource).Count)
		((DataView)DataSource)[nRow].EndEdit();
}
~~Bonnie







>
>My data access class has this:
>
>
>public bool UpdateDataSet(DataSet oDataSet, string sTableName)
>{
>	bool bRetVal = true;
>
>	try
>	{
>		oAdapter.Update(oDataSet, sTableName);
>	}
>	catch(SqlException e)
>	{
>		oException = e;
>		bRetVal = false;
>	}
>	return bRetVal;
>}
>
>
>Any reason why I shouldn't iterate through all the tables & rows and call EndEdit on each row here in this method? This would put it one place as opposed to all the different areas that work with DS's.
>
>Maybe something like:
>
>
>foreach(DataTable oTable in oDataSet.Tables)
>{
>	foreach(DataRow oRow in oTable.Rows)
>	{
>		oRow.EndEdit();
>	}
>}
>
>
>
>
>
>
>
>
>
>
>
>
>>Kevin,
>>
>>Certain circumstances leaves a DataRow in a "Proposed" state ... which simply means that the data has not been "moved" into the "Current" state. You can see this if you check oRow.HasVersion(DataRowState.Proposed) ... it will be true. (I think that's the right syntax, this is off the top of my head).
>>
>>Anyway, in order to get it from the Proposed state, you need to Commit the Proposed changes. There are several ways to do this, but why don't you start out with this concept and see if it solves the problem, this seems to be the most foolproof methodology:
>>
>>
>>		DataRow oRow = oDataSet.Tables[0].Rows[0].EndEdit();
>>
>>		oAdapter.Update(oDataSet, sTableName);
>>
>>
>>~~Bonnie
>>
>>
>>
>>
>>>I have the dataset bound to textboxes on the form:
>>>
>>>
>>>txtTitle.DataBindings.Add("Text", oDataSet, "ItemRow.Title");
>>>txtCategory.DataBindings.Add("Text", oDataSet, "ItemRow.Category");
>>>txtAuthor.DataBindings.Add("Text", oDataSet, "ItemRow.Author");
>>>txtCatNo.DataBindings.Add("Text", oDataSet, "ItemRow.Catalogue_No");
>>>txtIsbn.DataBindings.Add("Text", oDataSet, "ItemRow.Isbn");
>>>txtPublisher.DataBindings.Add("Text", oDataSet, "ItemRow.Publisher");
>>>
>>>
>>>In SaveChanges I then pass the oDataSet variable to this code in my Data Access class:
>>>
>>>
>>>public bool UpdateDataSet(DataSet oDataSet, string sTableName)
>>>{
>>>	bool bRetVal = true;
>>>
>>>	try
>>>	{
>>>		DataRow oRow = oDataSet.Tables[0].Rows[0];
>>>		DataRowState state = oRow.RowState;                \\state = Unchanged
>>>
>>>		oAdapter.Update(oDataSet, sTableName);
>>>	}
>>>	catch(SqlException e)
>>>	{
>>>		oException = e;
>>>		bRetVal = false;
>>>	}
>>>	return bRetVal;
>>>}
>>>
>>>
>>>
>>>The DataAdapater is stored as oAdapter on the data access class. When I run this, it doesn't error, but it also doesn't save.
>>>
>>>Anyone wanna set a newbie straight?
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform