Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Usually, I use FormClosing event to check if there is any pending change in database tables. If yes, I call the TableAdapter.Update(Datatable) method and DataTable.AcceptChanges() before exit, to commit all pending changes.

For this purpose I have a SaveData() function.

In the form's menu I've added two buttons, [Exit] button and [Save and Exit] button. The [Exit] button simply closes the form, FormClosing is fired and if necessary save current data. [Save and Exit] calls SaveData() function and closes the form, and, FormClosing if fired again.

I don't know why, but even if I called SaveData() , Datatable.GetChanges() returns always values. As far as I know, AcceptChanges() must mark all updated records.

I'm using a typed dataset object generated with V.S.

Do you know if there Is any issue with Datatable.AcceptChanges() with typed dataset?

Thanks.

C#
//-----------------------------------
// Bussines logic class
//-----------------------------------

// retunrs true if there are pending changes in orders table
public bool OrdersChanged
{
	get
	{
		DataTable c = this.orders.GetChanges();
		return (c == null || c.Rows.Count <= 0) ? false : true;
	}
}


// commit database changes
public bool UpdateAll()
{
	try
	{
		ta_orders.Update(this.orders);
		this.orders.AcceptChanges();
	}
	catch (Exception ex)
	{
		this.last_error = ex.Message;
		return false;
	}

	return true;
}


//-----------------------------------
// Winform methods
//-----------------------------------


// this function is called from OnFormClosing's event
protected override void AditionalOnClosingActions(FormClosingEventArgs e)
{
	base.AditionalOnClosingActions(e);

	// ends any pending grid edit
	this.Validate();
	this.gridQuestions.EndUpdate();

	// ends binding source edit
	this.bl_order.bsOrders.EndEdit();

	// ask for confirmation
	if (this.bl_order.OrdersChanged)
	{
		string msg = this.Text + "\n\nSave pending changes?";
		if (MessageBox.Show(msg, "CMF", MessageBoxButtons.YesNo) == DialogResult.Yes)
		{
			SaveData();
		}
	}
}

// commit database changes
protected override bool SaveData()
{
	base.SaveData();

	bool result = false;

	try
	{
		this.Validate();

		this.bl_order.UpdateAll();

		if (this.bl_order.last_error != "")
			throw new Exception(this.bl_order.last_error);

		// refresh order's list
		SendParentRequest("refreshOrdersList", null);

		result = true;

	}
	catch (Exception ex)
	{
		MessageBox.Show(ex.Message);
	}

	return result;
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900