cardiffcoder
This row already belongs to another table

It does, that’s the whole point - I’m trying to copy a row from one datatable to another.

Funnily, we can’t just use the (dt is DataTable) “dt.Rows.Add(<datarow>)” method; probably because the datarow contains identifying information relevant to the original datatable - so we have to use “dt.ImportRow(<datarow>)” instead.

Just remember that kids, you can’t just take things that aren’t yours and pretend they are, you have to import them ;)

One of the most elementary mistakes that I have seen fellow coders make, in .NET anyway, is trying to loop through data within objects using a count of rows/columns. The row number of the datarows does not start at the same number as the start of the loop through a reflection of .Count.

If you are looping through a set of datarows it will start at 0, as objects are zero-based indexed.

If you count the number of datarows then want to loop through them all, it will count them as you and I would, starting from 1.

So, if you are looping through a set of datarows and want to get through them all, from the beginning to the number of rows you must loop from 0 to the number of rows minus 1 (- 1) or else you will get an ArgumentOutOfRangeException because it tries to find columns 1, 2, 3 in the dataset instead of 0, 1, 2.

My favourite mistake, databinding too early.

If there is one mistake I make almost every single time when working with webforms it is this one, databinding controls during the OnLoad event. This is particularly perilous for DropDownLists and RadioButtonLists and I’ll tell you why …

OnLoad is fired very early on in the ASP.NET page lifecycle and what this does is wipe out any selections you made before postback because the control has been cleared and then re-populated, clearning SelectedValue, SelectedItem etc.

I’ve seen many different ways that people databind controls to overcome this, but the plain, simple and easiest way is to just databind later, after you have dealt with the SelectedValue or SelectedItem properties. Personally, I databind during PreRender.