private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (_sourcePackage.Count > 0)
            {
                PackageList deletedPackage =
                    _sourcePackage[_packageCurrentIndex];

                if (deletedPackage.PackageTag)
                {
                    return;
                }

                if (Message.DeleteMessage(deletedPackage.PackageName)
                    == DialogResult.Yes)
                {
                    SAPPackageEventMapReadWrite eventMapRW =
                        new SAPPackageEventMapReadWrite(Config._dbConnectionName);

                    SAPDataSetPackageEventMap ds =
                        eventMapRW.SelectByPackageID(deletedPackage.PackageID);

                    foreach (SAPDataSetPackageEventMap.PackageEventMapRow row in ds.PackageEventMap.Rows)
                    {
                        eventMapRW.Delete(row.PackageEventMapID);
                    }

                    SAPPackageReadWrite packageRW =
                        new SAPPackageReadWrite(Config._dbConnectionName);

                    packageRW.Delete(deletedPackage.PackageID);
                    _sourcePackage.RemoveAt(_packageCurrentIndex);
                    dataGridViewPackage.RowCount = _sourcePackage.Count;
                }
            }
        }
        private void buttonEdit_Click(object sender, EventArgs e)
        {
            if (_packageCurrentIndex >= 0)
            {
                PackageList currentPackage =
                    _sourcePackage[_packageCurrentIndex];

                PackageForm newPackageForm = new PackageForm();

                if (currentPackage.PackageTag)
                {
                    newPackageForm._isReadOnly = true;
                    //return;
                }

                newPackageForm._packageID = currentPackage.PackageID;
                if (newPackageForm.ShowDialog(this) == DialogResult.OK)
                {
                    if (newPackageForm._packageItem != null)
                    {
                        _sourcePackage[_packageCurrentIndex] = newPackageForm._packageItem;
                    }
                }
                newPackageForm.Dispose();
            }
        }
        private void dataGridViewPackage_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            if (_sourcePackage.Count == 0)
            {
                return;
            }

            PackageList currentPackage =
                _sourcePackage[e.RowIndex];

            switch (dataGridViewPackage.Columns[e.ColumnIndex].Name)
            {
            case "ColumnName":
                e.Value = currentPackage.PackageName;
                break;

            case "ColumnDescription":
                e.Value = currentPackage.PackageDescription;
                break;

            case "ColumnPoster":
                e.Value = currentPackage.PackagePhoto;
                break;

            case "ColumnEdit":
                if (currentPackage.PackageTag)
                {
                    e.Value = Resources.icoedit_disabled;
                }
                else
                {
                    e.Value = Resources.icoedit;
                }
                break;

            case "ColumnDelete":
                if (currentPackage.PackageTag)
                {
                    e.Value = Resources.icodelete_disabled;
                }
                else
                {
                    e.Value = Resources.icodelete;
                }
                break;
            }
        }
        private void ManagePackage_Load(object sender, EventArgs e)
        {
            #region Populate Grid
            _sourcePackage =
                new List <PackageList>();

            List <PackageList> usedPackage   = new List <PackageList>();
            List <PackageList> unUsedPackage = new List <PackageList>();

            SAPEventAttendeeReadWrite eventAttendeeRW =
                new SAPEventAttendeeReadWrite(Config._dbConnectionName);

            SAPPackageReadWrite packageRW =
                new SAPPackageReadWrite(Config._dbConnectionName);

            using (SAPDataReaderPackage rdrPackage =
                       packageRW.ReaderSelectAll())
            {
                if (rdrPackage.DataReader != null &&
                    rdrPackage.DataReader.HasRows)
                {
                    SAPDataSetEventAttendee.EventAttendeeDataTable dtEventAttendee =
                        eventAttendeeRW.SelectAll().EventAttendee;
                    while (rdrPackage.DataReader.Read())
                    {
                        PackageList item = new PackageList();
                        item.PackageName        = rdrPackage.PackageName;
                        item.PackageDescription = rdrPackage.PackageDescription;
                        item.PackagePhoto       = rdrPackage.PackageImage;
                        item.PackageID          = rdrPackage.PackageID;

                        DataRow[] rows =
                            dtEventAttendee.Select("packageid = " + item.PackageID.ToString());

                        if (rows != null && rows.Length > 0)
                        {
                            item.PackageTag = true;
                            usedPackage.Add(item);
                        }
                        else
                        {
                            item.PackageTag = false;
                            unUsedPackage.Add(item);
                        }
                    } //while (rdrPackage.DataReader.Read());

                    _sourcePackage.AddRange(unUsedPackage);
                    _sourcePackage.AddRange(usedPackage);
                }
            }

            //SAPEventAttendeeReadWrite

            #endregion

            #region Create Data Grid
            dataGridViewPackage.VirtualMode = true;

            DataGridViewTextBoxColumn columnName =
                new DataGridViewTextBoxColumn();
            columnName.HeaderText = "Name";
            columnName.Name       = "ColumnName";
            columnName.Width      = 100;
            dataGridViewPackage.Columns.Add(columnName);

            DataGridViewTextBoxColumn columnDesc =
                new DataGridViewTextBoxColumn();
            columnDesc.HeaderText   = "Description";
            columnDesc.Name         = "ColumnDescription";
            columnDesc.Width        = 150;
            columnDesc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridViewPackage.Columns.Add(columnDesc);

            //create icon edit column
            DataGridViewImageColumn editColumn =
                new DataGridViewImageColumn();
            editColumn.Image      = Resources.icoedit;
            editColumn.Width      = 25;
            editColumn.Name       = "ColumnEdit";
            editColumn.HeaderText = string.Empty;
            editColumn.SortMode   = DataGridViewColumnSortMode.NotSortable;

            //create icond delete column
            DataGridViewImageColumn deleteColumn = new DataGridViewImageColumn();
            deleteColumn.Image      = Resources.icodelete;
            deleteColumn.Width      = 25;
            deleteColumn.Name       = "ColumnDelete";
            deleteColumn.HeaderText = string.Empty;
            deleteColumn.SortMode   = DataGridViewColumnSortMode.NotSortable;

            dataGridViewPackage.Columns.Insert(2, editColumn);
            editColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;

            dataGridViewPackage.Columns.Insert(3, deleteColumn);
            deleteColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;

            #endregion

            dataGridViewPackage.RowCount = _sourcePackage.Count;

            pictureBoxNewPackage.Image = Resources.iconew;

            //set first image on poster panel
            if (_sourcePackage.Count > 0)
            {
                pictureBoxPoster.Image =
                    UtilityHelper.ByteToImage(_sourcePackage[0].PackagePhoto);
            }
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            bool hasNoError = true;

            _packageItem = new PackageList();
            int  packageID = _packageID;
            int  packageEventMapID;
            bool isSuccess = false;

            #region Check Image file
            string fileName  = openFileDialog.FileName;
            byte[] byteImage = null;

            if (File.Exists(fileName))
            {
                byteImage = UtilityHelper.FileToByte(fileName);
            }
            else if (_packageID > 0)
            {
                byteImage = _imageSelected;
            }
            else
            {
                byteImage = UtilityHelper.BitmapToByte(Resources.blank);
            }
            #endregion

            if (textBoxName.Text.Trim().Length == 0)
            {
                errorProvider.SetError(textBoxName, "Package name is required.");
                hasNoError = false;
            }
            if (textBoxDescription.Text.Trim().Length == 0)
            {
                errorProvider.SetError(textBoxDescription, "Package description is required.");
                hasNoError = false;
            }
            if (dataGridViewEvents.RowCount == 0)
            {
                errorProvider.SetError(dataGridViewEvents, "At least one event is required.");
                hasNoError = false;
            }
            if (byteImage == null)
            {
                errorProvider.SetError(pictureBoxPoster, "Poster is required.");
                hasNoError = false;
            }

            if (!hasNoError)
            {
                return;
            }

            using (SWPTransactionDBConnection dbTransaction =
                       new SWPTransactionDBConnection(Config._dbConnectionName))
            {
                try
                {
                    SAPPackageReadWrite packageRW =
                        new SAPPackageReadWrite(dbTransaction);
                    SAPPackageEventMapReadWrite packageEventRW =
                        new SAPPackageEventMapReadWrite(dbTransaction);
                    if (_packageID == 0)
                    {
                        packageRW.Insert(
                            textBoxName.Text,
                            textBoxDescription.Text,
                            byteImage,
                            out packageID);
                        foreach (AssociatedEventList item in _sourceEvent)
                        {
                            packageEventRW.Insert(
                                packageID,
                                item.EventID,
                                out packageEventMapID);
                        }
                    }
                    else
                    {
                        packageRW.Update(
                            _packageID,
                            textBoxName.Text,
                            textBoxDescription.Text,
                            byteImage);

                        foreach (AssociatedEventList item in _removedEvent)
                        {
                            packageEventRW.Delete(item.PackageMapID);
                        }

                        foreach (AssociatedEventList item in _sourceEvent)
                        {
                            if (item.IsNewID)
                            {
                                packageEventRW.Insert(
                                    _packageID,
                                    item.EventID,
                                    out packageEventMapID);
                            }
                        }
                    }
                    dbTransaction.Transaction.Commit();
                    isSuccess = true;
                }
                catch (Exception ex)
                {
                    dbTransaction.Transaction.Rollback();

                    ErrorForm errorForm =
                        new ErrorForm(ex.Message, ex.ToString());
                    errorForm.ShowDialog(this);
                    errorForm.Close();
                }
                finally
                {
                    dbTransaction.Transaction.Dispose();
                }
            }
            if (isSuccess)
            {
                _packageItem.PackageName        = textBoxName.Text;
                _packageItem.PackageDescription = textBoxDescription.Text;
                _packageItem.PackagePhoto       = byteImage;
                _packageItem.PackageID          = packageID;
                DialogResult = DialogResult.OK;
                Close();
            }
        }