private void CheckCustomer()
 {
     if (comboBoxCustomername.SelectedIndex > -1 &&
         comboBoxCustomername.SelectedIndex <
         customerList.Count)
     {
         if (comboBoxCustomername.SelectedValue != null)
         {
             BAPISCUDAT itemSelected = customerList[(string)comboBoxCustomername.SelectedValue];
             textboxAddress.Text           = itemSelected.STREET;
             textBoxCity.Text              = itemSelected.CITY;
             textboxPhone.Text             = itemSelected.PHONE;
             textBoxPostalCode.Text        = itemSelected.POSTCODE;
             comboBoxCountry.SelectedValue = itemSelected.COUNTR;
         }
     }
     else if (comboBoxCustomername.Items.Contains(comboBoxCustomername.Text))
     {
         comboBoxCustomername.SelectedIndex = comboBoxCustomername.Items.IndexOf(comboBoxCustomername.Text);
         if (comboBoxCustomername.SelectedValue != null)
         {
             BAPISCUDAT itemSelected =
                 customerList[(string)comboBoxCustomername.SelectedValue];
             textboxAddress.Text           = itemSelected.STREET;
             textBoxCity.Text              = itemSelected.CITY;
             textboxPhone.Text             = itemSelected.PHONE;
             textBoxPostalCode.Text        = itemSelected.POSTCODE;
             comboBoxCountry.SelectedValue = itemSelected.COUNTR;
         }
     }
 }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            bool hasNoError = true;

            errorProvider.Clear();

            if (comboBoxCustomername.Text.Trim().Length == 0)
            {
                errorProvider.SetError(comboBoxCustomername, "Customer Name is required");
                hasNoError = false;
            }
            if (textBoxCity.Text.Trim().Length == 0)
            {
                errorProvider.SetError(textBoxCity, "City is required");
                hasNoError = false;
            }

            if (!_hasChanges)
            {
                errorProvider.SetError(dataGridViewEvent, "Not all event has flight information");
                hasNoError = false;
            }
            if (hasNoError)
            {
                #region verify item
                _itineraryList = new List <FlightConnectionList>();
                foreach (DataRow dr in dtEventFlight.Rows)
                {
                    FlightConnectionList itemList = new FlightConnectionList();
                    itemList.AgencyNumber           = dr["FlightAgency"].ToString();
                    itemList.FlightConnectionNumber = dr["FlightConnection"].ToString();
                    itemList.FlightDate             = dr["FlightDate"].ToString();
                    if (itemList.FlightDate.Length == 0)
                    {
                        errorProvider.SetError(dataGridViewEvent, "Not all event has flight information");
                        return;
                    }
                    itemList.EventID = Convert.ToInt32(dr["EventID"]);
                    _itineraryList.Add(itemList);
                }
                _country        = (string)comboBoxCountry.SelectedValue;
                _language       = (string)comboBoxLanguage.SelectedValue;
                _customerName   = comboBoxCustomername.Text.Trim();
                _customerNumber = string.Empty;

                if (comboBoxCustomername.SelectedIndex > -1 &&
                    comboBoxCustomername.SelectedIndex <
                    customerList.Count)
                {
                    if (comboBoxCustomername.SelectedValue != null)
                    {
                        _customerNumber = customerList[(string)comboBoxCustomername.SelectedValue].CUSTOMERID;
                    }
                }
                else if (comboBoxCustomername.Items.Contains(_customerName))
                {
                    comboBoxCustomername.SelectedIndex = comboBoxCustomername.Items.IndexOf(_customerName);
                    if (comboBoxCustomername.SelectedValue != null)
                    {
                        BAPISCUDAT itemSelected =
                            customerList[(string)comboBoxCustomername.SelectedValue];
                        _customerNumber = itemSelected.CUSTOMERID;
                    }
                }
                #endregion

                PseudoProgressForm progress = new PseudoProgressForm();
                progress.ProgressLabel = "Saving to SAP...";
                BackgroundWorker bgWorker = new BackgroundWorker();

                string dob =
                    string.Format("{0:yyyy-MM-dd}", dateTimePicker.Value);
                string city       = textBoxCity.Text.Trim();
                string address    = textboxAddress.Text.Trim();
                string phone      = textboxPhone.Text.Trim();
                string postalCode = textBoxPostalCode.Text.Trim();

                bgWorker.DoWork +=
                    delegate(object senderWorker, DoWorkEventArgs eWorker)
                {
                    #region savecustomer
                    SAPCustomer customer = new SAPCustomer(Config.SAPUserName, Config.SAPPassword);
                    if (_customerNumber.Length == 0)
                    {
                        if (customer.CreateFromData(
                                city,
                                _country,
                                _customerName,
                                "P",
                                "none",
                                _language,
                                address,
                                phone,
                                postalCode))
                        {
                            //get customer number of inserted customer
                            bool found = false;
                            while (!found)
                            {
                                customer.GetList();
                                if (customer._customerList.Length > 0)
                                {
                                    //search last 4
                                    for (int i = customer._customerList.Length - 1;
                                         i > (customer._customerList.Length - 5);
                                         i--)
                                    {
                                        if (customer._customerList[i].CUSTNAME.Trim() == _customerName.Trim())
                                        {
                                            _customerNumber = customer._customerList[i].CUSTOMERID;
                                            found           = true;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            _errorCustomer = customer._bapiReturn;
                            eWorker.Cancel = true;
                        }
                    }
                    #endregion

                    #region create flight trip
                    if (_customerNumber.Length > 0)
                    {
                        SAPFlightTrip flightTrip =
                            new SAPFlightTrip(Config.SAPUserName, Config.SAPPassword);
                        for (int index = 0; index < _itineraryList.Count; index++)
                        {
                            FlightConnectionList itemList = _itineraryList[index];
                            string classType = "Y";
                            if (dictPackageType.ContainsKey(itemList.EventID))
                            {
                                classType = dictPackageType[itemList.EventID];
                            }
                            string tripNumber;
                            string travelAgencyNumber;
                            if (flightTrip.CreateTrip(
                                    itemList.AgencyNumber,
                                    classType,
                                    _customerNumber,
                                    itemList.FlightConnectionNumber,
                                    "",
                                    itemList.FlightDate,
                                    "",
                                    "none",
                                    dob,
                                    _customerName,
                                    out travelAgencyNumber,
                                    out tripNumber))
                            {
                                itemList.TripNumber   = tripNumber;
                                _itineraryList[index] = itemList;
                            }
                            else
                            {
                                _errorFlight   = flightTrip._bapiFlTripReturn;
                                eWorker.Cancel = true;
                            }
                        }
                    }
                    #endregion
                };

                bgWorker.RunWorkerCompleted +=
                    delegate(object senderWorker, RunWorkerCompletedEventArgs eWorker)
                {
                    progress.Close();

                    int eventAttendeeID = 0;
                    SAPEventAttendeeReadWrite eventAttendee =
                        new SAPEventAttendeeReadWrite(Config._dbConnectionName);
                    DateTime dateCreate;
                    eventAttendee.Insert(
                        _packageID,
                        _customerNumber,
                        dateTimePicker.Value,
                        out dateCreate,
                        out eventAttendeeID);

                    foreach (FlightConnectionList item in _itineraryList)
                    {
                        int eventAttendMapID = 0;
                        SAPEventAttendeeAgencyMapReadWrite eventMap =
                            new SAPEventAttendeeAgencyMapReadWrite(Config._dbConnectionName);
                        eventMap.Insert(
                            eventAttendeeID,
                            item.EventID,
                            item.AgencyNumber,
                            item.TripNumber,
                            out eventAttendMapID);
                    }
                    Message.DisplayInfo("Thanks for purchasing the package.");     // Total Ticket Price: " + e.Result.ToString());
                    Close();
                };

                bgWorker.RunWorkerAsync();
                progress.ShowDialog();
            }
        }