private List<SPCSInvoice> ReadInvoices(Connection connection)
        {
            int rowCount = connection.GetInvoiceCount();
            int rowNr = 0;

            List<SPCSInvoice> invoices = new List<SPCSInvoice>(rowCount);

            using (TypedDataReader reader = connection.GetInvoiceReader())
            {
                 using (SPCSConnection c = new SPCSConnection())
                {
                    c.Open();

                    if (c.IsConnected)
                    {
                        SPCSInvoice invoice = null;
                        SPCSCustomer customer = null;
                        SPCSProject project = null;

                        int invoiceId = 0;

                        // For each invoice
                        while (reader.Read())
                        {
                            customer = new SPCSCustomer();
                            {
                                customer.CustomerName = reader.GetString(DataBase.InvoiceColumnNameCustomerName);
                                customer.CustomerNr = customer.GetId();
                                customer.CustomerReference = string.Format("{0} {1}", reader.GetString(DataBase.InvoiceColumnNameCustomerReferenceFirstName),
                                                                    reader.GetString(DataBase.InvoiceColumnNameCustomerReferenceLastName));
                            }

                            project = new SPCSProject();
                            {
                                project.ProjectName = reader.GetString(DataBase.InvoiceColumnNameOurProject);
                                project.ProjectNr = project.GetId();
                                project.ProjectCustomerNr = customer.CustomerNr;
                                project.ProjectStartTime = DateTime.Today;
                                project.ProjectEndTime = DateTime.Today;
                            }

                            // Get invoice-head
                            invoice = new SPCSInvoice();
                            {
                                invoice.Customer = customer;
                                invoice.Project = project;

                                string date = reader.GetString(DataBase.InvoiceColumnNameDate);
                                if (!string.IsNullOrEmpty(date))
                                    invoice.InvoiceDate = DateTime.Parse(date);

                                invoiceId = reader.GetInt(DataBase.InvoiceRowColumnNameInvoiceId);
                                invoice.InvoiceCustomerOrderNr = reader.GetString(DataBase.InvoiceColumnNameCustomerOrderNr);
                                invoice.InvoiceOurReferenceName = string.Format("{0} {1}", reader.GetString(DataBase.InvoiceColumnNameOurReferenceFirstName),
                                                                    reader.GetString(DataBase.InvoiceColumnNameOurReferenceLastName));
                            }

                            // Get all invoice-rows
                            this.ReadInvoiceRows(connection, invoice, invoiceId);

                            invoices.Add(invoice);

                            Console.WriteLine(invoice.ToString());
                            this.OnProcessValueChanged(++rowNr, rowCount, ProcressAction.ReadFromDB);
                        }
                    }
                }
            }
            return invoices;
        }
        private SPCSInvoice ReadInvoice(Connection connection, string invoiceId)
        {
            SPCSInvoice invoice = null;
            SPCSCustomer customer = null;
            SPCSProject project = null;

            using (TypedDataReader reader = connection.GetInvoiceReader(int.Parse(invoiceId)))
            {
                using (SPCSConnection c = new SPCSConnection())
                {
                    c.LibraryPath = this.SPCSLibraryPath;
                    c.DatabasePath = string.Format("{0}{1}{2}", this.SPCSDatabasePath.TrimEnd('\\'), '\\', this.SPCSDatabaseName);
                    c.Open();

                    if (c.IsConnected)
                    {
                        bool result = reader.Read();

                        if (result)
                        {

                            customer = new SPCSCustomer();
                            {
                                customer.CustomerName = reader.GetString(DataBase.InvoiceColumnNameCustomerName);
                                customer.CustomerNr = customer.GetId();
                                customer.CustomerReference = string.Format("{0} {1}", reader.GetString(DataBase.InvoiceColumnNameCustomerReferenceFirstName),
                                                                    reader.GetString(DataBase.InvoiceColumnNameCustomerReferenceLastName));

                            }

                            project = new SPCSProject();
                            {
                                project.ProjectName = reader.GetString(DataBase.InvoiceColumnNameOurProject);
                                project.ProjectNr = project.GetId();
                                project.ProjectCustomerNr = customer.CustomerNr;
                                project.ProjectStartTime = DateTime.Today;
                                project.ProjectEndTime = DateTime.Today;
                            }

                            invoice = new SPCSInvoice();
                            {
                                invoice.Customer = customer;
                                invoice.Project = project;

                                invoice.InvoiceDate = reader.GetDateTime(DataBase.InvoiceColumnNameDate);

                                invoiceId = reader.GetString(DataBase.InvoiceRowColumnNameInvoiceId);
                                invoice.InvoiceCustomerOrderNr = reader.GetString(DataBase.InvoiceColumnNameCustomerOrderNr);
                                invoice.InvoiceOurReferenceName = string.Format("{0} {1}", reader.GetString(DataBase.InvoiceColumnNameOurReferenceFirstName),
                                                                    reader.GetString(DataBase.InvoiceColumnNameOurReferenceLastName));
                            }

                            this.ReadInvoiceRows(connection, invoice, int.Parse(invoiceId));
                            Console.WriteLine(invoice.ToString());

                            MessageBox.Show(invoice.ToString());
                        }
                    }
                }
            }
            return invoice;
        }