0

I'm having trouble displaying non-existent rows with if-else in a table with labels.

I'm currently logged in with this parentsID (logged in as New session) with two children (two rows) and there are no problems with dt.Rows[0] and dt.Rows[1] except I'm getting error at dt.Rows[2] with this Error: System.IndexOutOfRangeException: There is no row at position 2. As for the parentsID with three children, there's no error. Likewise, if I log in with a parentsID that has only one child then it displays There is no row at position 1.

How do I fix this? My english is not good, sorry if there are grammatical errors.

I have one table called family and it has everyone in it. All rows have childID(their username) along with parentsID. some child can be a parent of other childs.

        try
        {

            string query = "select * from family where parentsID = '" + Session["New"] + "'";
            using (OleDbCommand cmd3 = new OleDbCommand(query, con))
            {
                con.Open();
                OleDbDataReader myReader3 = null;
                myReader3 = cmd3.ExecuteReader();

                if (myReader3.HasRows)
                {
                    DataTable dt = new DataTable();
                    dt.Load(myReader3);

                    if (!DBNull.Value.Equals(dt.Rows[0]["childID"]))
                    {
                        label1.Text = dt.Rows[0]["childID"].ToString();
                        label2.Text = "$" + Convert.ToDecimal(dt.Rows[0]["childNetWorth"]).ToString("N2");
                        label3.Text = dt.Rows[0]["childName"].ToString();
                    }
                    else
                    {
                        label1.Text = "-ID-";
                        label2.Text = "-";
                        label3.Text = "-";
                    }

                    if (!DBNull.Value.Equals(dt.Rows[1]["childID"]))
                    {
                        label5.Text = dt.Rows[1]["childID"].ToString();
                        label6.Text = "$" + Convert.ToDecimal(dt.Rows[1]["childNetWorth"]).ToString("N2");
                        label7.Text = dt.Rows[1]["childName"].ToString();
                    }
                    else
                    {
                        label5.Text = "-ID-";
                        label6.Text = "-";
                        label7.Text = "-";
                    }

                    if (!DBNull.Value.Equals(dt.Rows[2]["childID"]))
                    {
                        label9.Text = dt.Rows[2]["childID"].ToString();
                        label10.Text = "$" + Convert.ToDecimal(dt.Rows[2]["childNetWorth"]).ToString("N2");
                        label11.Text = dt.Rows[2]["childName"].ToString();

                    }
                    else
                    {
                        label9.Text = "-ID-";
                        label10.Text = "-";
                        label11.Text = "-";
                    }
                }
            }
        }

        catch (Exception ex)
        {
            Response.Write("Error: " + ex.ToString());
        }
        finally
        {
            con.Close();

        }

1 Answer 1

1

Using a for loop to iterate DataTable contents may prevent such IndexOutOfRangeException hassle like this example:

if (myReader3.HasRows)
{
     DataTable dt = new DataTable();
     dt.Load(myReader3);

     for (int i = 0; i < dt.Rows.Count; i++)
     {
         // label assignments here
     }
}

Depending on dt.Rows.Count value, iteration automatically stops if i reaching the maximum count value, hence dt.Rows[i] call doesn't throwing exception.

However, as Jon Skeet mentioned in loop through an array of given labels post, it's harder to find out which label will assigned to certain value for given setup even with help of array indexes (e.g. { 1, 5, 7 } for childID labels). So far, this is the effort I can do to iterate DataTable contents then assign values into different labels as required:

if (myReader3.HasRows)
{
     DataTable dt = new DataTable();
     dt.Load(myReader3);

     for (int i = 0; i < dt.Rows.Count; i++)
     {
         if (!DBNull.Value.Equals(dt.Rows[i]["childID"]))
         {
             if (i == 0)
             {
                 label1.Text = dt.Rows[i]["childID"].ToString();
                 label2.Text = "$" + Convert.ToDecimal(dt.Rows[i]["childNetWorth"]).ToString("N2");
                 label3.Text = dt.Rows[i]["childName"].ToString();
             }
             else if (i == 1)
             {
                 label5.Text = dt.Rows[i]["childID"].ToString();
                 label6.Text = "$" + Convert.ToDecimal(dt.Rows[i]["childNetWorth"]).ToString("N2");
                 label7.Text = dt.Rows[i]["childName"].ToString();
             }
             else if (i == 2)
             {
                 label9.Text = dt.Rows[i]["childID"].ToString();
                 label10.Text = "$" + Convert.ToDecimal(dt.Rows[i]["childNetWorth"]).ToString("N2");
                 label11.Text = dt.Rows[i]["childName"].ToString();
             }
         }
         else
         {
             if (i == 0)
             {
                 label1.Text = "-ID-";
                 label2.Text = "-";
                 label3.Text = "-";
             }
             else if (i == 1)
             {
                 label5.Text = "-ID-";
                 label6.Text = "-";
                 label7.Text = "-";
             }
             else if (i == 2)
             {
                 label9.Text = "-ID-";
                 label10.Text = "-";
                 label11.Text = "-";
             }
         }
     }
}

Any suggestions welcome.

Sign up to request clarification or add additional context in comments.

1 Comment

oh my god. thank you so much! marked as answer. thank you very much sir for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.