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 List<SPCSInvoice> TestInvoices()
        {
            List<SPCSInvoice> invoices = new List<SPCSInvoice>();

            SPCSConnection connection = new SPCSConnection();
            {
                connection.Open();

                if (connection.IsConnected)
                {
                    SPCSCustomer c = new SPCSCustomer();
                    {
                        c.CustomerName = "FooBar AB";
                        c.CustomerNr = c.GetId();
                        c.CustomerReference = "Jonas Grimfelt";
                        c.SaveIfNotAvailable();
                        Console.WriteLine("Customer-nr: " + c.CustomerNr);
                    }

                    SPCSProject p = new SPCSProject();
                    {
                        p.ProjectName = "ABC-123";
                        p.ProjectNr = p.GetId();
                        p.ProjectCustomerNr = c.CustomerNr;
                        p.ProjectStartTime = DateTime.Today;
                        p.ProjectEndTime = DateTime.Today;
                        p.SaveIfNotAvailable();
                        Console.WriteLine("Project-nr: " + p.ProjectNr);
                    }

                    SPCSInvoice i = new SPCSInvoice();
                    {
                        i.InvoiceDate = DateTime.ParseExact("2007-07-14", "yyyy-MM-dd", null);

                        //i.InvoiceCustomerNr = c.CustomerNr;
                        i.Customer = c;
                        i.InvoiceCustomerOrderNr = "123AABB1234";

                        //i.InvoiceOurProject = p.ProjectNr;
                        i.Project = p;
                        i.InvoiceOurReferenceName = "Foo Bar";

                        SPCSInvoiceRow r1 = new SPCSInvoiceRow();
                        {
                            r1.InvoiceRowArticleText = "Lorem ipsum A";
                            r1.InvoiceRowPriceEach = 3.51;
                            r1.InvoiceRowQuantity = 425;
                            r1.InvoiceRowUnit = "Pieces";
                        }
                        SPCSInvoiceRow r2 = new SPCSInvoiceRow();
                        {
                            r2.InvoiceRowArticleText = "Lorem ipsum B";
                            r2.InvoiceRowPriceEach = 3.51;
                            r2.InvoiceRowQuantity = 300;
                            r2.InvoiceRowUnit = "Hr";
                        }
                        i.InvoiceRows.Add(r1);
                        i.InvoiceRows.Add(r2);
                    }

                    invoices.Add(i);
                }

                connection.Close();
                connection.Dispose();
            }

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