Good afternoon fine DaniWeb folks!
I am about to pull my hair out on this one and I'd like to know if someone knows of a simple or obvious "gotcha!" for this problem.
I've got a form with some detail fields on it (below)
When a user attempts to navigate off of the record shown, a method called "Save" is called, which prompts the user if they wish to save the record before it is changed.
This is the text of that method (please forgive the rough code, it's very much a work in progress):
/// <summary>
/// Checks if changes have been made and saves those changes to the datasource.
/// </summary>
/// <param name="showPrompts">If True, shows a prompt to the user asking them to confirm changes if changes are found.
/// If set to False, no prompts are shown.</param>
/// <returns>True if the save operation completed. False if the operation was canceled.</returns>
private bool Save(bool showPrompts)
{
DialogResult savePrompt = System.Windows.Forms.DialogResult.Yes;
if (ActiveAccountID == -1)
{
createdDateTimePicker.Value = DateTime.Now;
savePrompt = MessageBox.Show("Would you like to save your changes?", "Save", MessageBoxButtons.YesNoCancel);
if (savePrompt == System.Windows.Forms.DialogResult.No)
{
dSDCDataSet.Accounts.RejectChanges();
return true;
}
else if (savePrompt == System.Windows.Forms.DialogResult.Cancel)
{
return false;
}
}
else if (dSDCDataSet.Accounts.FindByID(ActiveAccountID).HasVersion(DataRowVersion.Proposed))
{
savePrompt = MessageBox.Show("Would you like to save your changes?", "Save", MessageBoxButtons.YesNoCancel);
if (savePrompt == System.Windows.Forms.DialogResult.No)
{
dSDCDataSet.Accounts.RejectChanges();
return true;
}
else if (savePrompt == System.Windows.Forms.DialogResult.Cancel)
{
return false;
}
}
else
{
return true;
}
lastUpdatedDateTimePicker.Value = DateTime.Now;
// validate the form
this.Validate();
// call EndEdit for each binding source.
accountsBindingSource.EndEdit();
// update the data.
tableAdapterManager.UpdateAll(dSDCDataSet);
return true;
}
Here's the issue: the method works...sometimes. The symptoms of this hair-pulling problem are:
1. On a few occasions when a change is made, it successfully saves the data to the SQL server, I can verify it using SSMS and SSP. But when I try debugging it again, it may fail without any rhyme or reason.
2. More often than not, if the change IS successful and I attempt to change the same record again without closing the application the update and all successive ones fail.
Things I found out though debugging:
1. The UpdateAll method is being called and is throwing no error.
2. The UpdateAll method, when failing to update, is returning 0 (I would expect 1, or more).
3. If I set a breakpoint and run dSDCDataSet.Accounts.FindByID(ActiveAccountID).HasVersion(DataRowVersion.Proposed)
in the immediate window, it returns true, even if the update fails. It correctly returns false when the update succeeds.
4. The Validate method is returning true
5. When the save operation fails, and you navigate back to the record that you changed and then navigate away without making any changes again, you receive the save prompt.
6. When the update fails, no attempt to connect to the SQL server is made, even when UpdateAll is called. I found this out through SSP which shows no login or logout audit entry, nor is a connection established.
Things I've tried:
1. Opening the SQL connection before the UpdateAll method is called and then closing it after. No difference.
2. Running a while loop forcing the UpdateAll method to repeat until it succeeds. (I know, I know...silly, but I was willing to try anything!) No difference, except what appeared to be an infinite loop.
3. Clearing all of the data in the dataset and refilling it after a successful update. No difference.
Is there some obvious thing I'm missing?