Last line of a datatable asp.net

All we need is an easy explanation of the problem, so here it is.

I have a problem when I’m trying to a loop in a DataTable that a dataset contains.

I’m doing a loop like this:

for(int i = 0; i<ds.Tables[0].Rows.Count - 1  ; i++)

The problem is that I can’t get the value of the last line with this one, but if I try to get rid of the "-1" and do a loop on the whole table, I’ll have an out of range exception.

This out of range exception is because I have to check if the value of a line "i" is equal to the value of a line "i+1", like this:

if (ds.Tables[0].Rows[i]["Release_No"] != ds.Tables[0].Rows[i + 1]["Release_No"])

So if I do it in a loop, when the index is on the last line it will check if the last line is equal to i+1, and it’s out of the table.

So I was trying to check if the index is on the last line, then just get the value of the last line, but it seems like it doesn’t work.

if(ds.Tables[0].Rows.IndexOf(ds.Tables[0].Rows[i]) == ds.Tables[0].Rows.Count)

If anyone has an idea, let me know, and of course if it is not clear enough let me know, I’ll give more information and more code.

Thanks for your help and your time!

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Check if it’s the last record, first.

I like to refactor code to read as close to sentence form as possible, explaining what you want it to do using named variables and methods, and that often gets me unlocked.

Try to make each line of code do one thing, and one thing only, like check if it is the last row:

var data = ds.Tables[0].Rows;
var lastRow = data.Count - 1;

for(int i = 0; i < lastRow ; i++)
{
   if (i == lastRow){
      // This is the last row. Handle the last row here.
   } 
   else
   {
      // Handle all other rows here
      var currentRecord = data[i];
      var nextRecord = data[i + 1];
      if (currentRecord["Release_No"] != nextRecord["Release_No"])
      {
         // Handle unique Releases...
      }
   }

}

Method 2

Use less than or equal to like this

for(int i = 0; i<=ds.Tables[0].Rows.Count - 1 ; i++)

I hope this may get what you want.

Method 3

Something like this is better ?

        var lastRow = data.Count - 1;
        var data = ds.Tables[0].Rows;

        for(int i = 0; i< lastRow; i++)
        {
            testFirstCum = Convert.ToInt32(ds.Tables[0].Rows[i]["EDI_Accum_Quantity"]);

            if ( i == lastRow)
            {
                if (DBNull.Value.Equals(data[i]))
                {
    
                   
                    quantity = 0;
                }
                else
                {
                    quantity = Convert.ToInt32(data[i]);

                    testFirstCum = testFirstCum + quantity;
                    System.Diagnostics.Debug.WriteLine(quantity);
                    System.Diagnostics.Debug.WriteLine(testFirstCum);
                }

            }
            else
            {
                var col = ds.Tables[0].Columns;
                var currentRecord = data[i];
                var nextRecord = data[i + 1];
                if(currentRecord["Release_No"] != nextRecord["Release_No"])
                {
                    for (int j = col[2].Ordinal; j < col.Count; j++)
                    {
                        if (DBNull.Value.Equals(data[i][j]))
                        {

                            quantity = 0;
                        }
                        else
                        {
                            quantity = Convert.ToInt32(data[i][j]);

                            testFirstCum = testFirstCum + quantity;
                            System.Diagnostics.Debug.WriteLine(quantity);
                            System.Diagnostics.Debug.WriteLine(testFirstCum);
                        }
                    }

                }
            }
        }

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

Leave a Reply