I am trying to write a loop that will use the add.row function to add a number of rows that will be decided by user input. The button action submits info to a couple other tables also, and that function is working properly.

This code will execute, but instead of repeating until it is equal to purchaseCount it only records one entry just like it would if it was outside of the do, while phrase.

do
                {
                    InventoryModuleDb3DataSet.BacksideLedgerRow newBacksideLedgerRow = inventoryModuleDb3DataSet.BacksideLedger.NewBacksideLedgerRow();

                    newBacksideLedgerRow.PurchaseDate = purchaseDate;
                    newBacksideLedgerRow.Type = purchaseType;
                    newBacksideLedgerRow.CostPerUnit = avgCost;

                    inventoryModuleDb3DataSet.BacksideLedger.Rows.Add(newBacksideLedgerRow);

                    purchaseCount++;

                } while (purchaseCount > purchaseUnits);

Dani AI

Generated

reported that the loop steps through in the debugger but only a single BacksideLedger entry is visible afterwards. already caught the do/while condition issue and and suggested verifying the unit/count values — good first checks. The critical diagnostic is to split the problem into two questions: (A) does the loop actually create multiple rows in memory, and (B) are multiple rows being persisted to the underlying store.

Check the in-memory result and each row's state before any save/refresh. For a quick test, log the table count and each row's RowState right after the loop to confirm multiple Added rows exist:

Debug.WriteLine("Before: " + table.Rows.Count);
/* run the add-loop */
Debug.WriteLine("After: " + table.Rows.Count);
foreach (DataRow r in table.Rows)
    Debug.WriteLine(r.RowState);

If the in-memory count is correct but only one record ends up in the database, likely causes are: AcceptChanges being called too early (which clears the Added state) — see DataTable.AcceptChanges; calling Fill/refresh after adding rows and before Update; an Update call that returns fewer rows than expected — check the DataAdapter/TableAdapter Update return value (DataAdapter.Update); or ASP.NET page lifecycle/code that recreates the DataSet on postback. If the in-memory count itself is wrong, watch the loop variables and any early returns or exceptions being swallowed during iteration.

Recommended Answers

All 7 Replies

I just tried this and it also doesn't work. Is it not possible to make the rows.add function repeat? if not, what can I do to create a number of entries based on user input?

for (int purchaseCount = 0; purchaseCount < purchaseUnits; purchaseCount++)
                {
                    InventoryModuleDb3DataSet.BacksideLedgerRow newBacksideLedgerRow = inventoryModuleDb3DataSet.BacksideLedger.NewBacksideLedgerRow();

                    newBacksideLedgerRow.PurchaseDate = purchaseDate;
                    newBacksideLedgerRow.Type = purchaseType;
                    newBacksideLedgerRow.CostPerUnit = avgCost;

                    inventoryModuleDb3DataSet.BacksideLedger.Rows.Add(newBacksideLedgerRow);
                }

Your first loop seemed broken, since you had the loop condition backwards. This second version seems like it should work, but it is a mystery to me.

What happens when you debug your code?

When I debug everything seems normal like it will work. When I enter values into the my text boxes and submit the info populates all of the tables as it is supposed to. It also populates this table in the format it is supposed to, but only one record per click is sent no matter how high the units value goes.

When I look at it bloc by bloc the value transfers into the count variable, and it even seems to pass through the correctly, but only completes the row.add function once.

Well, what happens when you set a breakpoint and walk through? I mean, what is Rows.Add doing?

okay so when i set a breakpoint above either of these arguments i get the same thing. It runs through once, like it should, writes the data to the table.

Then, it looks back at the argument, realizes it hasn't completed, and starts through the loop again. Again it calls back to the data designer and looks like it should be writing to the table. It finishes through the block, doesn't recognize the ++ this time and leaves the block as though it had completed it successfully. I get no error or additional information.

Can you set the purchaseUnits value artificially high and try stepping through it? This code genuinely looks like it should work just fine.

The only thing that I can think is that if you're using a custom Data Access Layer, there could be some limitation or bug within that.

I don't think there is a limitation on Rows.Add. I would verify that purchaseUnits is greater than 1 when hitting the loop. Also if you copy/pasted that code, why do you have InventoryModuleDb3DataSet at the top and inventoryModuleDb3DataSet through the rest of the code. I'm guessing it's a property but you should be consistent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.