0

I am building data table like this below and when i trying to assign a values to database on row by row basis i am getting error like this

there is no row at position 0

and this is my code:

    public DataTable BuildCertInfo()
    {
        DataTable dtCertInfo = new DataTable(TABLE);
        DataColumnCollection columns;
        columns = dtCertInfo.Columns;

        AddColumn(dtCertInfo, ROW_NO, "System.Int32", false, 0);
        AddColumn(dtCertInfo, LIC_ID, "System.Int32", false, 0);
        AddColumn(dtCertInfo, SYS_ID, "System.Int32", false, 0);

         AddColumnString(dtCertInfo, SYS_PART_ID);
         ...............
         ...............
          return dtCertInfo;
    }

and i am calling above method like this

  DataTable dt = objCertificate.BuildCertInfo();

and then i am looping through the gridview and assigning values to data table like below ..

 for (int i = 0; i < gvPRCertInfo.Rows.Count; i++)
 {

     dt.Rows[i][Certificate.SYS_PART_ID] = systemPartID; // here I am getting error
     ................
     ................
  }

I am not sure where i am doing wrong could any please point me in right direction that would very grateful to me

many thanks in advance ...

Glory Raj
  • 17,397
  • 27
  • 100
  • 203

1 Answers1

1

You don't create any row, that's why you get the error. Try something like this:

for (int i = 0; i < gvPRCertInfo.Rows.Count; i++)
{
    dt.Rows.Add(dt.NewRow());
    dt.Rows[i][Certificate.SYS_PART_ID] = systemPartID;
    ....
}
Palle Due
  • 5,929
  • 4
  • 17
  • 32