Wesley
Wesley

Reputation: 669

C# TableAdapter.Update() is using UPDATE rather than INSERT

SELECT        WO_BreakerRail.ID, indRailType.RailType, indRailType.RailCode, WO_BreakerRail.CreatedPieces, WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged, 
                         WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop, COALESCE (WO_BreakerRail.Date, @date) AS Date
FROM            indRailType LEFT OUTER JOIN
                         WO_BreakerRail ON indRailType.RailCode = WO_BreakerRail.RailCode AND WO_BreakerRail.Date = @date

I have a DataGridView that is bound to a table adapter. This is the select query I use to fill my adapter. The ID column is going to be null where there are no matching entries from WO_BreakerRail. The update command works fine when I update a row that has data in it. But if I update a row that has no matching info in WO_BreakerRail, it fails because the table adapter thinks it should be updating because I've modified rows in it's row collection, but WO_BreakerRail doesn't have that row. Is there a way to tell the table adapter to use the insert command when ID is null rather than the update command?

Upvotes: 1

Views: 1891

Answers (2)

Partha Choudhury
Partha Choudhury

Reputation: 534

For the rows that do not have data, set the rowstate to new by calling: dataRow.SetAdded().

Upvotes: 2

Anthony
Anthony

Reputation: 1709

I think the following is what your missing in your add method.

   // ... in your method where your inserting 
        DataRow row = MyDataTable.NewRow();
        // .... add data to row ....
        MyDataTable.Rows.Add(row);

Upvotes: 0

Related Questions