How to delete a row from a typed dataset and save it to a database?

Posted by Zingam on Stack Overflow See other posts from Stack Overflow or by Zingam
Published on 2012-06-22T09:11:30Z Indexed on 2012/06/22 9:15 UTC
Read the original article Hit count: 179

Filed under:
|
|
|
|

I am doing this as a small project to learn about disconnected and connected models in .NET 4.0 and SQL Server 2008 R2.

I have three tables:

Companies (PK CompanyID) 
Addresses (PK AddressID, FK CompanyID) 
ContactPersons (PK ContactPersonID, FK CompanyID)

CompanyID is assigned manually by the users. The other IDs are auto-generated. Companies has a one-to-many relationship with ContactPerson. I have set any changes to cascade.

I display all records in Companies in a DataGridView and when a row is clicked, the corresponding records in ContactPersons are displayed in a second DataGridView.

I have successfully implemented updating and inserting new records but I completely fail in my attempts to delete rows and save the changes to the database.

I us a typed dataset.

If I use this:

DataRow[] contactPersonRows = m_SoldaCompaniesFileDataSet.ContactPersons.Select("ContactPersonID = "
                                                  + this.m_CurrentContactPerson.ContactPersonID);

m_SoldaCompaniesFileDataSet.ContactPersons.Rows.Remove(contactPersonRows[0]);

The records are displayed properly in the DataGridView but are not saved in the database later.

If I use this:

DataRow row = m_SoldaCompaniesFileDataSet.ContactPersons.Rows.Find(this.m_CurrentContactPerson.ContactPersonID);
row.Delete();

The records are set but I get an exeception: DeletedRowInaccessibleException, when I try to refresh the DataGridView. The exception pop-s up in the auto-generated dataset.design file.

I am pretty much stuck at this point since yesterday. I cannot find anything anywhere that remotely resembles my problem. And I cannot understand actually what is going on.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about database