Displaying multiple rows in labels using dt.Rows

hj_chua

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();

        }
Tetsuya Yamamoto

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PDO SQL issue displaying multiple rows when using COUNT()

From Dev

displaying multiple rows in single row in sql query

From Dev

TSQL: Displaying multiple rows of results as 1 row

From Dev

Multiple rows using XSLT

From Dev

Apply using multiple rows in vector

From Dev

Delete multiple rows using IDs?

From Dev

Delete multiple rows using IDs?

From Dev

using foreach and multiple insert rows?

From Dev

Using AWK to pull multiple rows

From Dev

Retrieve multiple rows with query using AND and OR

From Dev

Delete multiple rows using Jquery

From Dev

Apply using multiple rows in vector

From Dev

Insert multiple rows using for loop

From Dev

Insert multiple rows using execute

From Dev

Output not displaying all rows

From Dev

displaying the top 3 rows

From Dev

Displaying divs in rows

From Dev

Displaying 10 divs in rows

From Dev

Displaying blog style tags side by side in multiple rows (Android)

From Dev

matlab rows to column labels

From Dev

Pandas drop rows not in labels

From Dev

Pandas drop rows not in labels

From Dev

How to create unique labels for multiple rows in geom_text?

From Dev

Create Textbox widgets in Tkinter of different length for multiple Labels in different rows

From Dev

Update Multiple columns of multiple rows using LINQ

From Dev

Update Multiple columns of multiple rows using LINQ

From Dev

row num is not displaying any rows when using between keyword

From Dev

Displaying the running balance but only the specified rows using WHERE clause in mysql

From Dev

Displaying rows from sqlite databse using QTableWidget in PyQt4