I have the following function:
private void populate()
{
String connectionString = Properties.Settings.Default.Database;
String selectString = "select artikelnummer, omschrijving from Artikels";
SqlDataReader reader = null;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(selectString);
reader = command.ExecuteReader();
connection.Open();
int x = 0;
while (reader.Read())
{
String item = String.Empty;
item = Convert.ToString(reader["Artikelnummer"]) + "\t" + Convert.ToString(reader["Omschrijving"]);
x++;
listboxGeselecteerd.Items.Add(item);
}
}
Everything that follows after reader = command.ExecuteReader(); is skipped.
Is there anything I’ve done wrong?
UPDATE: Moved the connection.Open(); to the right spot. Now, when I reach that line, my output shows Step into: Stepping over non-user code 'System.Data.SqlClient.SqlConnection.Open
And then skips the rest of the function.
My money is on a method higher up in the call stack eating the exception because this should’ve thrown one because the connection hadn’t been opened. That’s your biggest problem.
The next problem you have is that your connection is not associated with the SqlCommand so even if it were opened, it wouldn’t matter.
Finally,
connection.Open();needs to be before ExecuteReader.In addition to that, you really ought to be using
usingblocks.How about some simple “printf”-style debugging and posting the exception you get?
Given the exception text from the comments (
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll) looks more like the message you’d get just before the real message showed up, I would catch a SqlException and examine the InnerException(s).