Thomas,
Return the reader object.
Unlike dataadapter Fill (analogy select-SQL in VFP) it reads records one by one (analogy scan..endscan in VFP).
while (reader.Read())
// Do something with DataRow
It also has a close method which you should call to close the reader when you're done.
return UsersReader;
UsersReader.Close(); // Error – Unreachable code detected
After return UsersReader.Close(); line is unreachable. Simply remove it. In code where you get UsersReader object have that line to close the connection.
I don't have much idea about VB.Net but probably it's not erroring since you've CommandBehavior.CloseConnection and that imlplicitly closes the connection when done.
drUsers = cmdUserID.ExecuteReader(CommandBehavior.CloseConnection)
drUsers.Read()
Return drUsers
Here you get the object, only read first DataRow (and do nothing with it) then return the reader object.
SqlCommand selectCommand = new SqlCommand(selectStatement, GetConnection());
Here from this code GetConnection() stands as a method returning a connection object. Then:
GetConnection.Close()
should be:
GetConnection().Close()
or it's like this:
SqlConnection cn = GetConnection();
//...
cn.Close();
public void ddlFiller()
{
SqlDataReader UsersReader =
GetUsers("SELECT UserID FROM UserInfo ORDER BY UserID"); // Get reader object
while (UsersReader.Read()) // Loop through all rows
{ // Fill the ddl via UsersReader["UserID"] }
UsersReader.Close(); // Closes the connection implicitly - CommandBehavior.CloseConnection
}
public static SqlDataReader GetUsers(string strSQL)
{
SqlConnection UsersConnection = GetConnection() ;
UsersConnection.Open();
SqlCommand selectCommand = new SqlCommand(strSQL, UsersConnection);
SqlDataReader UsersReader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection);
return UsersReader;
}
Cetin