【问题标题】:There Is No Row At Position 5位置 5 没有行
【发布时间】:2013-02-15 10:04:26
【问题描述】:

我正在使用 C Sharp

运行调试器查看代码有问题后出现此错误:

{“位置 5 处没有行。”} System.Exception {System.IndexOutOfRangeException}

   public DataSet FindData(string ID, string pass)
    {
        InitializeConnection();
        m_oCn.Open();
        DataSet thisDataSet = new DataSet();
        DataSet foundDataSet = new DataSet();
        try
        {
            m_oDA.Fill (thisDataSet, "Login");
            for (int n = 0; 0 < thisDataSet.Tables["Login"].Rows.Count ; n++)
            {
                if (thisDataSet.Tables["Login"].Rows[n]["UserName"].ToString () == ID)
                {
                    if (thisDataSet.Tables["Login"].Rows[n]["Password"].ToString () == pass)
                    {
                        m_oDA.Fill(foundDataSet,n,1,"Login");


                    }
                }
            }
        }
        catch 
        {
        }
        finally
        {
            m_oCn.Close();
            m_oCn = null;
        }
        return foundDataSet;

【问题讨论】:

    标签: c# ado.net dataset


    【解决方案1】:

    如果你的循环有缺陷:

    for (int n = 0; 0 < thisDataSet.Tables["Login"].Rows.Count ; n++)
    

    不应该是这样的:

    for (int n = 0; n < thisDataSet.Tables["Login"].Rows.Count ; n++)
    

    【讨论】:

      【解决方案2】:
      for (int n = 0; 0 < thisDataSet.Tables["Login"].Rows.Count; n++)
      

      您的 for 循环的条件检查零是否小于行数,这意味着您的循环将无限期地运行(或直到您访问不存在的索引)。您可能的意思是检查 n 是否小于行数:

      for (int n = 0; n < thisDataSet.Tables["Login"].Rows.Count; n++)
      

      【讨论】:

      • 谢谢,我现在明白了..谢谢你的解释。 (Y)
      猜你喜欢
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多