Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTable.Select() Property: Giving Index Out of Bound Exception

Tags:

c#

asp.net

I'm trying to select only those rows which have Parent ID = 0


int predecessor = Parent;

        StringBuilder valuePath = new StringBuilder();
        valuePath.Append(Parent.ToString());

DataRow[] drPar;

            while (true)
            {
                drPar = dt.Select("MenuID=" + predecessor);
                if (drPar != null)
                {
                    if (drPar[0]["ParentID"].ToString().Equals("0"))
                    break;
                }

drPar[0]["ParentID"].ToString().Equals("0") is giving me Out of Bound exception ..

Help Please !

like image 760
Harry47 Avatar asked Jan 16 '23 20:01

Harry47


1 Answers

DataTable.Select does not return null when there's no matching DataRow but an array with Length==0.

But apart from that, why are you using an "infinite" loop for only one statement?

So this should work:

drPar = dt.Select("MenuID=" + predecessor);
if (drPar.Length != 0)
{
    if (drPar[0]["ParentID"].ToString().Equals("0"))
    {
       // ...
    }
}
like image 166
Tim Schmelter Avatar answered Feb 16 '23 01:02

Tim Schmelter