M_O_MEN
M_O_MEN

Reputation: 1

How to handle "There is no row at position 1" error

Im pretty new in ASP.NET Over here I dont have any rows at Rows[1][...] But i want to check whether there is anything there or not if there is no Row at Row[1][...] then i want to proceed so I handled it like this in the ternary operator in the line Statement = (dtEntryFlag.Rows[counter]["EntryFlag"].ToString()==""?"": dtEntryFlag.Rows[counter]["EntryFlag"].ToString()); Below is the rest of the code including this line

 DataTable dtEntryFlag= objdalTransactionEntry.GetRentInvoiceEntryFlag(Sessions.Name.UserId);
      string Statement="";
      if (dtEntryFlag.Rows.Count>0)
      {
         Statement = (dtEntryFlag.Rows[counter]["EntryFlag"].ToString()==""?"": dtEntryFlag.Rows[counter]["EntryFlag"].ToString()); 
///the line above that needs fixing
      }

Upvotes: 0

Views: 569

Answers (1)

NCCSBIM071
NCCSBIM071

Reputation: 1205

One way to fix this issue is to not access the row entirely if it doesn't exist. Check in if condition.

DataTable dtEntryFlag = objdalTransactionEntry.GetRentInvoiceEntryFlag(Sessions.Name.UserId);
string Statement = "";
if (counter < dtEntryFlag.Rows.Count)
{
    Statement = (dtEntryFlag.Rows[counter]["EntryFlag"].ToString() == "" ? "" : dtEntryFlag.Rows[counter]["EntryFlag"].ToString());
}

Upvotes: 1

Related Questions