//Search method.
        private void searchProducts()
        {
            List <int> SearchResults = new List <int>();
            string     selectQuery;

            selectQuery = "SELECT Products.ProductID, ProductTypes.ProductType, Products.ProductName, Products.YearlyPremium " +
                          "FROM Products JOIN ProductTypes ON Products.ProductTypeID = ProductTypes.ProductTypeID " +
                          "WHERE " + GlobalVariables.searchCretia;

            SqlConnection conn = ConnectionManager.DatabaseConnection();
            SqlDataReader rdr  = null;

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(selectQuery, conn);
                rdr = cmd.ExecuteReader();

                //read form table and fill in to list
                while (rdr.Read())
                {
                    SearchResults.Add(int.Parse(rdr["ProductID"].ToString()));
                }

                if (rdr != null)
                {
                    rdr.Close();
                }
                conn.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Unsuccessful");
            }

            //If list is empty.
            if (SearchResults.Count == 0)
            {
                MessageBox.Show("Sorry no Product Records where found", "No Result Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cbSerarchType.ResetText();
                txtSearchPremium.Clear();
                return;
            }
            //If list contains ID's and ask yser if thats what they will like to search for.
            else
            {
                var mbresults = MessageBox.Show("Product Records Found at ID: " + string.Join(" , ", SearchResults) + Environment.NewLine + "Click OK to Display or Cancel", "Results Found", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                //Create a new frmsale and invoke searchSaleResults().
                if (mbresults == DialogResult.OK)
                {
                    frmProducts pResults = new frmProducts();
                    pResults.Show();
                    pResults.Focus();
                    pResults.BringToFront();
                    pResults.Text = "Returned Search from Products";
                    pResults.searchProductResults();
                    this.Close();

                    //Close the Sale Form.
                    frmProducts closeProducts = (frmProducts)Application.OpenForms["frmProducts"];
                    closeProducts.Close();
                }
                else
                {
                    this.Close();
                }
            }
        }
Esempio n. 2
0
        //Show frmProducts form.
        private void productsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form frm1 = new frmProducts();

            frm1.Show();
        }