Exemplo n.º 1
0
        public void addChemicalsAndEquipments(chemicalsAndEquipments obj)
        {
            string query = "IF OBJECT_ID('dbo.ChemicalsAndEquipments', 'U') IS NULL ";

            query += "BEGIN ";
            query += "CREATE TABLE [dbo].[ChemicalsAndEquipments](";
            query += "[Name] VARCHAR(250)  NOT NULL CONSTRAINT pkId PRIMARY KEY,";
            query += "[Date] VARCHAR(25) NOT NULL,";
            query += "[Price] VARCHAR(100) NOT NULL,";
            query += "[Quantity] VARCHAR(20) NOT NULL,";
            query += "[EmployeeNic] VARCHAR(25)  NOT NULL CONSTRAINT fpkempId FOREIGN KEY (EmployeeNic) REFERENCES dbo.Employee(EmployeeNic),";
            query += "[Reference] VARCHAR(1000) ,";
            query += ")";
            query += " END";
            Console.WriteLine(obj.Name);
            String sqlQuery = "INSERT INTO dbo.ChemicalsAndEquipments(Name,Date,Price,Quantity,EmployeeNic,Reference) " +
                              "VALUES ('" + obj.Name + "','" + obj.Date + "','" + obj.Price + "','" + obj.Quantity + "','" + obj.User + "','" + obj.Reference + "')";

            getConnection();
            SqlCommand cmd = new SqlCommand(query, getConnection());

            cmd.ExecuteNonQuery();
            SqlCommand cmd1 = new SqlCommand(sqlQuery, getConnection());

            cmd1.ExecuteNonQuery();
            getConnection().Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (core.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Chemical or ID Field!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (name.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Name Field!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (price.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Price Field!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (quantity.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Quantity Field!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                DialogResult answer;
                answer = MessageBox.Show("Are you sure want to add Employee?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (answer == DialogResult.Yes)
                {
                    //******database connect ******
                    try
                    {
                        string date1 = date.Value.ToString();
                        if (core.Text.Equals("Chemical") || core.Text.Equals("Equipment"))
                        {
                            //float cPrice = float.Parse(price.Text);
                            string cPrice     = price.Text;
                            string cQuantity  = quantity.Text + cmbQuantity.Text;
                            string cReference = reference.Text;
                            string cName      = name.Text;

                            DBconnection dbconnection = new DBconnection();

                            if (dbconnection.findChemicalEquipment(cName))
                            {
                                string currentQuantity = dbconnection.getCurrentQuantity(name.Text);
                                int    q           = Convert.ToInt32(currentQuantity) + Convert.ToInt32(cQuantity);
                                string newQuantity = q.ToString();
                                dbconnection.updateChemicalAndEquipment(cName, newQuantity);
                            }
                            else
                            {
                                chemicalsAndEquipments chemicalsAndequipments = new chemicalsAndEquipments(date1, cName, cPrice, cQuantity, global.UserID, cReference);

                                dbconnection.addChemicalsAndEquipments(chemicalsAndequipments);
                            }
                            availableEquipmentsAndChemicals();
                            clearFileds();
                        }
                        else
                        {
                            MessageBox.Show("Enter valid Type(Chemical or Equipment)", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error:\n" + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Console.WriteLine("Error: \n" + ex);
                    }
                }
            }
        }