Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用dt.rows在标签中显示多行_C#_Ms Access 2016 - Fatal编程技术网

C# 使用dt.rows在标签中显示多行

C# 使用dt.rows在标签中显示多行,c#,ms-access-2016,C#,Ms Access 2016,在带有标签的表中,使用if-else显示不存在的行时遇到问题 我目前使用这个parentsID(以New会话登录)和两个孩子(两行)登录,dt.rows[0]和dt.rows[1]没有问题,除了我在dt.rows[2]处出错之外有此错误:System.IndexOutOfRangeException:位置2没有行。对于有三个子项的父SID,没有错误。同样,如果我使用只有一个子项的parentsID登录,那么它会显示位置1没有行。 我该如何解决这个问题?我的英语不好,如果有语法错误,对不起 我有一

在带有标签的表中,使用if-else显示不存在的行时遇到问题

我目前使用这个
parentsID
(以
New
会话登录)和两个孩子(两行)登录,
dt.rows[0]
dt.rows[1]
没有问题,除了我在
dt.rows[2]处出错之外
有此
错误:System.IndexOutOfRangeException:位置2没有行。
对于有三个子项的
父SID
,没有错误。同样,如果我使用只有一个子项的
parentsID
登录,那么它会显示
位置1没有行。

我该如何解决这个问题?我的英语不好,如果有语法错误,对不起

我有一张名为
family
的表格,里面有每个人。所有行都有
childID
(其用户名)以及
parentsID
。一些
孩子
可以是其他
孩子的
父母

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

        }

使用for循环来迭代
数据表
内容可能会避免类似以下示例的
索引自动失效异常
麻烦:

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

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

欢迎任何建议。

哦,我的天啊。非常感谢你!标记为答案的。先生,非常感谢您的帮助!
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 = "-";
             }
         }
     }
}