I have a Windows Forms data entry applet for entering data into a small SQL Server database. I keep seeing this error when trying to save my new record after clicking AddNewItem
button on the binding navigator component.
My code on clicking the save button on the binding navigator looks like this:
private void btnSave_Click(object sender, EventArgs e)
{
try
{
this.Validate();
int currentPosition = this.witsStatusDBDataSet.TestCase.Rows.Count - 1;
WitsStatusDBDataSet.TestCaseRow row = (WitsStatusDBDataSet.TestCaseRow)witsStatusDBDataSet.TestCase.Rows[currentPosition];
row.AcceptChanges();
witsStatusDBDataSet.TestCase.AcceptChanges();
this.testCaseBindingSource.EndEdit();
int current = witsStatusDBDataSet.TestCase.Rows.Count - 1;
testCaseTableAdapter.Update(this.witsStatusDBDataSet.TestCase.Rows[current]);
WitsStatusDBEntry.WitsStatusDBDataSetTableAdapters.TableAdapterManager manager = new TableAdapterManager();
manager.UpdateAll(witsStatusDBDataSet);
SysTimer = new System.Timers.Timer(2500);
statusLabel1.Text = "Updated successfully.";
SysTimer.Start();
}
catch(Exception exc)
{
string msg = exc.Message + " : " + exc.StackTrace;
Clipboard.SetText(msg);
MessageBox.Show(msg);
}
}
If I enter the data manually in SQL Server Mgmt Studio, the binding navigator successfully loads it and I can use Move Next and Move Previous successfully.
But if I have a brand-new database that has just been deployed, with no records, I get this error.
I checked StackOverflow for similar issues, but nothing seemed to be the same situation.
I re-coded the method, based on mason's comment. Here is the working code:
private void btnSave_Click(object sender, EventArgs e)
{
try
{
WitsStatusDBDataSet.TestCaseRow row = null;
this.Validate();
int currentPosition = this.witsStatusDBDataSet.TestCase.Rows.Count - 1;
if(currentPosition == -1)
{
row = AddRowToDataTable(testCase: witsStatusDBDataSet.TestCase);
}
if (row == null)
{
currentPosition = this.witsStatusDBDataSet.TestCase.Rows.Count - 1;
}
row.AcceptChanges();
witsStatusDBDataSet.TestCase.AcceptChanges();
this.testCaseBindingSource.EndEdit();
testCaseTableAdapter.Update(row);
testCaseTableAdapter.InsertCase(row.Title, row.IsAutomated, row.Description, row.State, row.Area, row.Iteration, row.Priority,
row.Severity, row.Owner, row.CreatedDate, row.ModifiedDate, row.TFS_Case_ID, row.TFSInstance);
WitsStatusDBEntry.WitsStatusDBDataSetTableAdapters.TableAdapterManager manager = new TableAdapterManager();
manager.Connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=WitsStatusDB;Integrated Security=True");
manager.UpdateAll(witsStatusDBDataSet);
SysTimer = new System.Timers.Timer(2500);
statusLabel1.Text = "Updated successfully.";
SysTimer.Start();
}
catch(Exception exc)
{
string msg = exc.Message + " : " + exc.StackTrace;
Clipboard.SetText(msg);
MessageBox.Show(msg);
}
}
Notice I had to call "AddRowToDataDable()" - that method simply transfers the contents of the Windows Form to the TestCaseRow object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.