ntnunk
ntnunk

Reputation: 53

C# Code Not Executing

I have a C#/WPF application with a tabbed interface that has been behaving strangely. After thinking originally my problems were related to the TabControl, I now believe that it's something different and I'm completely stuck. The following method is just supposed to pull some data out of the database and load a couple of WPF ComboBoxes. 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. 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();
}

Upvotes: 1

Views: 1483

Answers (4)

mynkow
mynkow

Reputation: 4548

Reader should be closed.

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

Upvotes: 2

phoog
phoog

Reputation: 43036

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(); at that point, you'll get an exception on reader["State"].

Since reader.Read() returns a boolean to indicate whether there's a current row, you should skip calling reader.HasRows entirely:

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

Upvotes: 3

Adam Robinson
Adam Robinson

Reputation: 185593

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

Upvotes: 2

Hector Sanchez
Hector Sanchez

Reputation: 2317

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

Upvotes: 2

Related Questions