how to handle System.IndexOutOfRangeException: There is no row at position 21

i have a string which consist of data in XML format.it consist of different tag like in VENUE tag which have 50 data & ARTIST tag which consist of 20 data. My problem is when i bind that data in data set & adding that data in a data row i got error There is no row at position 21 Here is my code

      for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
        {
            DataRow dr = dt.NewRow();
            dr["Venue"] = ds.Tables[1].Rows[i][1].ToString();
            dr["Address"] = ds.Tables[1].Rows[i][2].ToString();
            dr["City"] = ds.Tables[1].Rows[i][4].ToString();
            dr["State"] = ds.Tables[1].Rows[i][5].ToString();
            dr["ZipCode"] = ds.Tables[1].Rows[i][6].ToString();
            dr["Performer"] = ds.Tables[2].Rows[i][0].ToString();
         }

1 for VENUE & 2 For ARTIST. How i handle this error because i want both VENUE & ARTIST data in a data table. Help me on this...

EDIT

i done this like

       if(ds.Tables[2].Rows.Count>0)
          { 
            dr["Performer"] = ds.Tables[2].Rows[i][0].ToString();
          }
         else
          {
            dr["Performer"]="";
           }

but this doesn't work i gives me same error

EDIT what about this

          if (ds.Tables[2].Rows.Count > i)
            {
                dr["Performer"] = ds.Tables[2].Rows[i][0].ToString();
            }
            else
            {
                dr["Performer"] = "no performer";
            }
Jon Skeet
people
quotationmark

Currently you're assuming that Tables[2] has as many rows as Tables[1], due to these two pieces of your code:

for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
{
    ...
    dr["Performer"] = ds.Tables[2].Rows[i][0].ToString();
}

It's not clear what you're actually trying to do with the performer information, but it sounds like you may want to perform a join query, and work just on the joined rows.

people

See more on this question at Stackoverflow