/// <summary>
        /// Iterator to pass data back to calling form via a delegate
        /// </summary>
        /// <param name="ct">CancellationToken</param>
        /// <returns></returns>
        public IEnumerable <object[]> LoadCustomers(CancellationToken ct)
        {
            int    RecordCount = this.RowCount();
            Person personArray = new Person();

            // prepare our delegate
            CustomersForm.UpdateDataTableDelegateProgress updateDelegate =
                delegate(int CurrentPosition, int Total, Person[] person)
            {
                callingForm.UpDateDataTable(CurrentPosition, Total, person);
            };

            using (OleDbConnection cn = new OleDbConnection {
                ConnectionString = Builder.ConnectionString
            })
            {
                using (OleDbCommand cmd = new OleDbCommand {
                    CommandText = "SELECT Identifier, FirstName, LastName FROM People;", Connection = cn
                })
                {
                    cn.Open();

                    OleDbDataReader Reader = cmd.ExecuteReader();

                    if (Reader.HasRows)
                    {
                        while (Reader.Read())
                        {
                            personArray.Identifier = Reader.GetFieldValue <int>(0);
                            CurrentIdentifier      = personArray.Identifier;
                            personArray.FirstName  = Reader.GetFieldValue <string>(1);
                            personArray.LastName   = Reader.GetFieldValue <string>(2);

                            ItemArray = new Person[] { personArray };

                            // return data to calling form
                            yield return(ItemArray);


                            // invoke calling form delegate
                            updateDelegate(this.CurrentIdentifier, this.RowCount(), ItemArray);
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Checks if a given field (column) in the database is null at the record with the given username.
        /// </summary>
        /// <param name="studentNumber">The student number to query.</param>
        /// <param name="field">The field/column in the database to check if currently null or not.</param>
        /// <returns>
        /// True if the record returns null for the given student number or if the field at the record
        /// with the given student number is null. Returns false if otherwise.
        /// </returns>
        private bool IsFieldNullAtSUNumber(string studentNumber, string field)
        {
            String       query   = "SELECT " + field + " FROM " + TABLE_NAME + " WHERE " + STUDENT_NO_COL + "='" + studentNumber + "';";
            OleDbCommand command = new OleDbCommand(query);

            try
            {
                using (OleDbConnection connection = new OleDbConnection(this.connString))
                {
                    // Set the Connection to the new OleDbConnection.
                    command.Connection = connection;
                    connection.Open();
                    OleDbDataReader reader = command.ExecuteReader();

                    // Open the connection and execute the insert command.
                    try
                    {
                        // Always call Read before accessing data.
                        reader.Read();
                        if (reader.GetFieldValue <Object>(0) != DBNull.Value)
                        {
                            return(false);
                        }
                        else
                        {
                            return(true);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return(true);
                    }
                    finally
                    {
                        // Always call Close when done reading.
                        reader.Close();
                    }
                    // The connection is automatically closed when the
                    // code exits the using block.
                }
            }
            catch (Exception)
            {
                return(true);
            }
        }