private void txtSearch_TextChanged(object sender, EventArgs e)
 {
     if (txtSearch.Text == null)
     {
         packages = PackageDB.DisplayPackagesInGrid();
         packageDataGridView.DataSource = packages; //packages is the list to hold the list of packages
     }
     else
     {
         packages = PackageDB.SearchByText(txtSearch.Text);
         packageDataGridView.DataSource = packages;
     }
 }
        /// <summary>
        /// Clicking on Row within the grid, populates the data in the controls places on the form from the Grid.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnDeletePkg_Click_1(object sender, EventArgs e)
        {
            Package pkgObj = new Package();

            try
            {
                if (txtPackageId.Text != "")
                {
                    pkgObj.PackageId = Convert.ToInt32(txtPackageId.Text);
                    PutPackageData(pkgObj);
                    var packageCount = PackageDB.CheckBeforeDelete(pkgObj.PackageId);
                    if (packageCount > 0)
                    {
                        MessageBox.Show("Can't Delete Package. Please delete dependent tables first");
                    }
                    else
                    {
                        DialogResult result = MessageBox.Show("Are you sure you want to delete this item from database?\n" +
                                                              "Delete action cannot be undone.", "Confirm deletion", MessageBoxButtons.OKCancel);

                        if (result.ToString() == "OK")
                        {
                            bool success = PackageDB.PackageDelete(pkgObj);
                            if (success)
                            {
                                MessageBox.Show("Package has been Deleted");
                            }
                        }
                    }



                    Refresh();

                    packages = PackageDB.DisplayPackagesInGrid();
                    packageDataGridView.DataSource = packages; //packages is the list to hold the list of packages
                }
                else
                {
                    MessageBox.Show("Please select the recprd to delete from the grid", "Select Error");
                }
            }

            catch (Exception ex)
            {
                //MessageBox.Show(ex.GetType().ToString() + ex.Message);
                MessageBox.Show("Can't delete the record now as it is beeing accessed by someone else", "Delete Error");
            }
        }
        private void frmPackages_Load(object sender, EventArgs e)
        {
            try
            {
                // packageDataGridView.DataSource = PackageDB.DisplayPackagesInGrid();
                packages = PackageDB.DisplayPackagesInGrid();
                packageDataGridView.DataSource = packages; //packages is the list to hold the list of packages

                pkrPkgStartDate.Checked = false;
                pkrPkgEndDate.Checked   = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occured while loading page", "Connection Error");
            }
        }
        /// <summary>
        /// Update method updates the Packages table with the modified data from the form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdatePkg_Click(object sender, EventArgs e)
        {
            Package newPackage = new Package();//Created new package object which is empty for now

            try
            {
                if (txtPackageId.Text != "")
                {
                    if (Validator.IsPresent(txtPkgName) && (Validator.IsPresent(txtPkgBasePrice)) && (Validator.isNonNegative(txtPkgBasePrice, "Base Price")))
                    {
                        if (pkrPkgStartDate.Checked == true && pkrPkgEndDate.Checked == true)
                        {
                            if (pkrPkgEndDate.Value.Date.CompareTo(pkrPkgStartDate.Value.Date) <= 0)
                            {
                                MessageBox.Show("Start Date should be less than End Date", "Date Selection Error", MessageBoxButtons.OK);
                            }
                            else
                            {
                                PutPackageData(newPackage);                          //  calls the putpackagedata() method to populate rest of the properties
                                newPackage.PackageId = int.Parse(txtPackageId.Text); //populates the PackageID property of the object

                                bool success = PackageDB.PackageUpdate(package, newPackage);
                                if (success)
                                {
                                    MessageBox.Show("Package Updated Successfully", "Package Update");
                                    Refresh();

                                    packages = PackageDB.DisplayPackagesInGrid();
                                    packageDataGridView.DataSource = packages; //packages is the list to hold the list of packages
                                }
                            }
                        }

                        else
                        {
                            PutPackageData(newPackage);                          //  calls the putpackagedata() method to populate rest of the properties
                            newPackage.PackageId = int.Parse(txtPackageId.Text); //populates the PackageID property of the object

                            bool success = PackageDB.PackageUpdate(package, newPackage);
                            if (success)
                            {
                                MessageBox.Show("Package Updated Successfully", "Package Update");
                                Refresh();

                                packages = PackageDB.DisplayPackagesInGrid();
                                packageDataGridView.DataSource = packages; //packages is the list to hold the list of packages
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Package Name & Base Price have to entered");
                    }
                }
                else
                {
                    MessageBox.Show("please select a record from the grid to update", "selection error");
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.GetType().ToString() + ex.Message);
                MessageBox.Show("Can't update the record now as it is beeing accessed by someone else", "Update Error");
            }
        }
        /// <summary>
        /// Add Package method adds the package into the package table, PackageID textbox  on the form is disabled as it's auto-incremented
        /// in the database table.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddPkg_Click_1(object sender, EventArgs e)
        {
            Package pkgObj = new Package();


            try
            {
                if (Validator.IsPresent(txtPkgName) && (Validator.IsPresent(txtPkgBasePrice) && (Validator.isNonNegative(txtPkgBasePrice, "Base Price"))))
                {
                    var pkg = packages.SingleOrDefault(pk => pk.PkgName.ToLower() == txtPkgName.Text.ToLower()); //checks if same package name already exists in database. returns null if no duplicate found else returns >0
                    if (pkg == null)
                    {
                        if (pkrPkgStartDate.Checked == true && pkrPkgEndDate.Checked == true)
                        {
                            if (pkrPkgEndDate.Value.Date.CompareTo(pkrPkgStartDate.Value.Date) <= 0)
                            {
                                MessageBox.Show("Start Date should be less than End Date", "Date Selection Error", MessageBoxButtons.OK);
                            }
                            else
                            {
                                this.PutPackageData(pkgObj);

                                PackageDB.PackageAdd(pkgObj);
                                MessageBox.Show("Package Added Successfully");
                                Refresh();
                                // packageDataGridView.DataSource = PackageDB.DisplayPackagesInGrid();
                                packages = PackageDB.DisplayPackagesInGrid();
                                packageDataGridView.DataSource = packages; //packages is the list to hold the list of packagesckages
                            }
                        }

                        else
                        {
                            this.PutPackageData(pkgObj);

                            PackageDB.PackageAdd(pkgObj);
                            MessageBox.Show("Package Added Successfully");
                            Refresh();
                            // packageDataGridView.DataSource = PackageDB.DisplayPackagesInGrid();
                            packages = PackageDB.DisplayPackagesInGrid();
                            packageDataGridView.DataSource = packages; //packages is the list to hold the list of packagesckages
                        }
                    }
                    else
                    {
                        MessageBox.Show("Package Id: " + pkg.PackageId + "\nPackage Name: " + pkg.PkgName + " already Exsits");
                        txtPkgName.Text = "";
                        txtPkgName.SelectAll();
                    }
                }
                else
                {
                    MessageBox.Show("Package Name & Base Price have to entered");
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }