简体   繁体   English

C#代码未执行

[英]C# Code Not Executing

I have a C#/WPF application with a tabbed interface that has been behaving strangely. 我有一个带有选项卡式界面的C#/ WPF应用程序,该程序的行为一直很奇怪。 After thinking originally my problems were related to the TabControl, I now believe that it's something different and I'm completely stuck. 在最初考虑到我的问题与TabControl有关之后,我现在认为它与众不同,并且完全陷入困境。 The following method is just supposed to pull some data out of the database and load a couple of WPF ComboBoxes. 以下方法只是应该从数据库中提取一些数据并加载几个WPF ComboBox。 The strange thing is that the code reaches a certain point, specifically the end of the loop that loads cboState's Item collection, and then continues on. 奇怪的是,代码到达了特定点,特别是循环的末尾,该循环加载了cboState的Item集合,然后继续。 No code placed below that loop executes, no errors are thrown than I can find or see, and no breakpoints placed below that loop ever get reached. 没有执行放置在该循环下面的代码,没有抛出超出我所能看到或看到的错误,并且没有到达放置在该循环下面的断点。 I'm completely perplexed. 我很困惑。

private void loadNewProjectTab() {
    dpDate.SelectedDate = DateTime.Now;

    cboProjectType.Items.Add("Proposal");
    cboProjectType.Items.Add("Pilot");
    cboProjectType.SelectedIndex = -1;

    string sql = "SELECT State FROM States ORDER BY ID";
    OleDbCommand cmd = new OleDbCommand(sql, connection);
    if(connection.State == ConnectionState.Closed) {
        connection.Open();
    }

    OleDbDataReader reader = cmd.ExecuteReader();
    while(reader.HasRows) {
        reader.Read();
        cboState.Items.Add(reader["State"].ToString().Trim());
    } // <-- Nothing below here executes.

    connection.Close();
}
while(reader.HasRows) { 
    reader.Read(); 
    cboState.Items.Add(reader["State"].ToString().Trim()); 
}

reader.HasRows will return true even after you've read all the rows and moved past the last one with reader.Read() ; 即使您已读取所有行并使用reader.Read()移到最后一行, reader.HasRows仍将返回true。 at that point, you'll get an exception on reader["State"] . 到那时,您将在reader["State"]上遇到异常。

Since reader.Read() returns a boolean to indicate whether there's a current row, you should skip calling reader.HasRows entirely: 由于reader.Read()返回一个布尔值以指示是否存在当前行,因此您应该完全跳过对reader.HasRows调用:

while(reader.Read()) { 
    cboState.Items.Add(reader["State"].ToString().Trim()); 
}     

Um I think is wrong your loop it should be. 嗯,我认为您的循环应该错了。

if (reader.HasRows)
{
    while(reader.Read()) 
    {         
        cboState.Items.Add(reader["State"].ToString().Trim());
    } 
}

Note that the bucle is with while(reader.Read()) 请注意,气泡在while(reader.Read())

This is your problem: 这是你的问题:

while(reader.HasRows) {
    reader.Read();
    cboState.Items.Add(reader["State"].ToString().Trim());
} 

HasRows indicates whether or not the reader retrieved anything; HasRows指示读者是否检索到任何内容; it doesn't change as you read through it (in other words, it's not analogous to an end-of-file indicator like you're using it). 当您通读它时它不会改变(换句话说,它与您正在使用它的文件结尾指示符并不相似)。 Instead, you should do this: 相反,您应该这样做:

while(reader.Read()) {
    cboState.Items.Add(reader["State"].ToString().Trim());
} 

Reader should be closed. Reader应关闭。

using(var reader = cmd.ExecuteReader())
{
    if(reader.HasRows)
    {
        while(reader.Read()) 
        {
            cboState.Items.Add(reader["State"].ToString().Trim());
        } 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM