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);

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.