There is no row at position 5 show error

719 Views Asked by At

I'm Using C Sharp

I Get This error after running the debugger to see what is wrong with the code:

{"There is no row at position 5."} System.Exception {System.IndexOutOfRangeException}

        dt = new DataTable();

        dt = objReport.USERWISEACCOUNTINGINFORMATIONWITHINBOUNDOUTBOUND(strDomainName, 0, fromDate, toDate);
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {

                DataTable dt1 = dt.AsEnumerable().Take(4).CopyToDataTable();



              int[] YPointMember = new int[dt1.Rows.Count];
               DateTime[] XPointMember = new DateTime[dt1.Rows.Count];
                for (int count = 0; count < dt1.Rows.Count; count++)
                {
                    YPointMember[count] = Convert.ToInt32(dt1.Rows[count]["INBOUND"]);
                    XPointMember[count] = Convert.ToDateTime(dt1.Rows[count]["connectdatetime"]);

                }
                Chart1.Series[0].Points.DataBindXY(YPointMember, XPointMember);
                Chart1.Series[0].BorderWidth = 1;
                Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:HH";
                Chart1.ChartAreas[0].AxisY.LabelStyle.Format = "h : h";
                Chart1.ChartAreas[0].AxisY.Interval = 2;
                Chart1.ChartAreas[0].AxisY.IntervalType = DateTimeIntervalType.Hours;
                Chart1.ChartAreas[0].AxisY.IntervalOffset = 0;

            }

        }
1

There are 1 best solutions below

2
Spider man On

the problem is here.

DataTable dt1 = dt.AsEnumerable().Take(4).CopyToDataTable();

Take(4) is trying to take 4 rows from table. if there are no four rows in table it will give the same error.