Reputation: 10321
I have 2 datagridviews containing one table, to give some more space for inserting text. To add, I don't want to show the first 2 columns, they contain Primary key and Foreign key, I have found a way to add the foreign key just before saving (primary is auto-increment). Now when saving this, all works fine. But when I want to delete a row....
Just before the dataset is send back to the database, I add those foreign keys like this, now I have to do it, else the adapt.Update()
will throw an error saying that the column can't be null.
private void SaveAnalyseTable()
{
dgvAnalyse1.EndEdit();
dgvAnalyse2.EndEdit();
for (int x = 0; x < dgvAnalyse1.Rows.Count-1; x++) //add to table, -1 else it will try to insert Id into an empty row (out of index error)
{
if (x != dgvAnalyse1.Rows.Count) //if it is the last one, don't execute
{
if (string.IsNullOrEmpty(patientsDataset.Tables["Analyse"].Rows[x]["PatientId"].ToString()) && (patientsDataset.Tables["Analyse"].Rows[x].RowState != DataRowState.Deleted)) .
{
patientsDataset.Tables["Analyse"].Rows[x]["PatientId"] = currentPatient.PatientId;
}
}
}
}
In the above code, I can't add those foreign numbers to the datagridview since the column isn't there The error is thrown in the code above here, when I'm trying to delete a row like this:
private void btnAnalyseDelete_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in dgvAnalyse1.SelectedRows)
{
dgvAnalyse1.Rows.RemoveAt(item.Index);
int index = item.Index;
if (index != -1)
{
patientsDataset.Tables["Analyse"].Rows[index].Delete();
}
}
SaveAllData(); //even when saving the whole dataset to the database, still get the error
}
So: first saving, then deleting doesn't work, it throws the error right away. deleting before saving it does work, it just can't be the same index, so it has to do something with the row in the dataset I guess.
Editing -> updateDataSet -> SaveToDatabase -> Delete row -> error.
Editing -> UpdateDataSet -> deleteRow ->updateDataSet -> SaveToDatabase -> fine unless same index
I'm sure that when saving the data, it will re-fill the dataset of tables.
Upvotes: 0
Views: 5955
Reputation: 216293
When you call SaveAnalyseTable
you have a test to avoid DataRowState.Deleted
rows and rows with null or empty values for PatientID
column but your logic should consider the effect of short circuit evaluation of the && operator
In your case, the test for the PatientID
being null or empty happens before the test for DataRowState.Deleted
status and thus it is executed also in case of deleted rows. The operator && first tries to verify the first part of the expression (PatientID
is null or empty) but the check for deleted happens only after the first one. So the first test happens every time and this test could potentially try to access a DataRowState.Deleted
row.
You need only to reverse the order in which you test the row for null or empty
if (patientsDataset.Tables["Analyse"].Rows[x].RowState != DataRowState.Deleted &&
string.IsNullOrEmpty(patientsDataset.Tables["Analyse"].Rows[x]["PatientId"].ToString())
Upvotes: 2