Today I was refactoring some sql procs along with the corresponding .net code that called the procs.
My primary focus was to implement some new business rules (ways to get totals) in the proc. In doing so, I completely re-wrote the proc. In the end the return values (column names) were the same, but the column order was different. To my amazement when I ran the application the numbers where in the wrong fields.
As I started to track down the error I stumbled across this ugly evil.
Estimate = (int)dataTable.Rows[0][0];
InProgress = (int)dataTable.Rows[0][2] + (int)dataTable.Rows[0][1];
Please people don't access data in a data table by hardcoding the index value. Yes I know this is the 'fastest' way get the data out, but it is also the most brittle. For less brittle code try the following. Yes I know that if the column name changes your code breaks. But at least in this case the application will throw an exception, not return the wrong value
Estimate = (int)dataTable.Rows[0][ dataTable.Columns.IndexOf("Estimated") ];
Sorry, just rate it when i have to fix evil code by lazy developers.
Till next time,