private void clear_Click(object sender, EventArgs e)
 {
     Cust_search.Clear();
     CustName.Clear();
     PhoneNumber.Clear();
     Cust_address.Clear();
     custEmail.Clear();
     Product_search.Clear();
     Product_Name.Clear();
     ProductID.Clear();
     Total_Amt.Text = "0";
     SubTotal.Clear();
     Discount.Text = "0";
     GST.Clear();
     PaidAmount.Clear();
     ReturnAmount.Clear();
     Quantity.Text            = "0";
     Amount.Text              = "0";
     AddedProducts.DataSource = null;
 }
        private void Cust_search_TextChanged(object sender, EventArgs e)
        {
            String keyword = Cust_search.Text;

            if (keyword == "")
            {
                Cust_search.Clear();
                CustName.Clear();
                PhoneNumber.Clear();
                Cust_address.Clear();
                custEmail.Clear();
                return;
            }

            Connect connectObj = new Connect();

            con = connectObj.connect();
            DataTable dt = new DataTable();


            try
            {
                String         sql  = "SELECT CNAME,ADDRESS,PHONE_NUMBER,EMAIL,C_ID FROM CUSTOMER WHERE C_ID LIKE '%" + keyword + "%' OR CNAME LIKE '%" + keyword + "%';";
                SqlDataAdapter adpt = new SqlDataAdapter(sql, con);

                adpt.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    CustName.Text     = dt.Rows[0]["CNAME"].ToString();
                    PhoneNumber.Text  = dt.Rows[0]["PHONE_NUMBER"].ToString();
                    Cust_address.Text = dt.Rows[0]["ADDRESS"].ToString();
                    custEmail.Text    = dt.Rows[0]["EMAIL"].ToString();
                    cid = dt.Rows[0]["C_ID"].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Captions", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Save_Click(object sender, EventArgs e)
        {
            //Create the orders
            try
            {
                Connect connectObj = new Connect();
                con = connectObj.connect();
                SqlCommand cmd = new SqlCommand("Insert into MYORDER (ORD_ID,CID,DATE,AMOUNT) values(@oid,@cid,@date,@amount);", con);

                cmd.Parameters.AddWithValue("@oid", OrderID.Text);
                //cmd.Parameters.AddWithValue("@pid", textBox8.Text);
                cmd.Parameters.AddWithValue("@cid", cid);
                cmd.Parameters.AddWithValue("@date", date.Text);
                cmd.Parameters.AddWithValue("@amount", Total_Amt.Text);

                int i = cmd.ExecuteNonQuery();
                //If count is equal to 1, than show frmMain form
                if (i != 0)
                {
                    MessageBox.Show("Order Insertion Successful!", "Captions", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Order Insertion Failed", "Captions", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Captions", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
            //Insert into orderDetails
            try
            {
                int j;

                Connect connectObj = new Connect();
                con = connectObj.connect();


                for (int i = 0; i < transactionDT.Rows.Count; i++)
                {
                    SqlCommand cmd = new SqlCommand("Insert into ORDER_DETAILS (ord_id,pid,quantity) values(@oid,@pid,@quantity);", con);

                    cmd.Parameters.AddWithValue("@oid", OrderID.Text);
                    cmd.Parameters.AddWithValue("@pid", transactionDT.Rows[i]["Product ID"].ToString());
                    cmd.Parameters.AddWithValue("@quantity", transactionDT.Rows[i]["Quantity"].ToString());


                    j = cmd.ExecuteNonQuery();
                }
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Captions", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
            //Update stock table after creating order
            try
            {
                int     j;
                Connect connectObj = new Connect();
                con = connectObj.connect();

                for (int i = 0; i < transactionDT.Rows.Count; i++)
                {
                    SqlCommand cmd = new SqlCommand("UPDATE STOCK SET QUANTITY = QUANTITY - @quantity WHERE PID = @pid", con);

                    cmd.Parameters.AddWithValue("@pid", transactionDT.Rows[i]["Product ID"].ToString());
                    cmd.Parameters.AddWithValue("@quantity", transactionDT.Rows[i]["Quantity"].ToString());


                    j = cmd.ExecuteNonQuery();
                }
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }

            //Clear all the fields.
            Cust_search.Clear();
            CustName.Clear();
            PhoneNumber.Clear();
            Cust_address.Clear();
            custEmail.Clear();
            Product_search.Clear();
            Product_Name.Clear();
            ProductID.Clear();
            Total_Amt.Text = "0";
            SubTotal.Clear();
            Discount.Text = "0";
            GST.Clear();
            PaidAmount.Clear();
            ReturnAmount.Clear();
            Quantity.Text            = "0";
            Amount.Text              = "0";
            AddedProducts.DataSource = null;
        }