private void AddButton_Click(object sender, EventArgs e)
        {
            if (ItemNameTextBox.Text != string.Empty && AmountInStkTextBox.Text != string.Empty && SupplierNameTextBox.Text != string.Empty && DateTextBox.Text != string.Empty)
            {
                DBC.getConnection();

                using (SQLiteConnection con = new SQLiteConnection(DBC.connectionString))
                {
                    con.Open(); // open database
                                // select supplierID using inputted supplier name
                    SQLiteCommand    cmdSup = new SQLiteCommand(string.Format(@"SELECT SupplierID FROM Supplier WHERE Name='{0}'", SupplierNameTextBox.Text), con);
                    SQLiteDataReader reader = cmdSup.ExecuteReader();

                    if (reader.HasRows)
                    {
                        reader.Read();
                        int sID = reader.GetInt32(0); // store PK value
                        reader.Close();               // close data reader
                                                      // insert data into Stock table
                        SQLiteCommand cmd = new SQLiteCommand(@"INSERT INTO Stock(ItemName, AmountInStock, DataLastUpdated, SupplierID) VALUES (@Name, @AIS, @DLU, @S)", con);
                        cmd.Parameters.AddWithValue("@Name", ItemNameTextBox.Text);
                        cmd.Parameters.AddWithValue("@AIS", AmountInStkTextBox.Text);
                        cmd.Parameters.AddWithValue("@DLU", DateTextBox.Text);
                        cmd.Parameters.AddWithValue("@S", sID);
                        cmd.ExecuteNonQuery();

                        MessageBox.Show("Data has been added"); // exception handling
                    }
                    else // if supplier name inputted is not in Supplier table then shows following message and sends user to add upplier info
                    {
                        MessageBox.Show("Supplier doesn't exist. Please input the supplier details before adding stock data.");
                        Suppliers sup = new Suppliers();
                        this.Close(); // close form
                        sup.Show();   // show supplier form
                    }

                    con.Close(); // close database
                }
            }
            else
            {
                foreach (Control c in this.Controls)
                {
                    if (c is TextBox)
                    {
                        TextBox textBox = c as TextBox;
                        if (textBox.Text == string.Empty)
                        {
                            if (textBox == ItemNameTextBox)
                            {
                                MessageBox.Show("Enter Item Name");
                            }
                            else if (textBox == AmountInStkTextBox)
                            {
                                MessageBox.Show("Enter the Amount in Stock");
                            }
                            else if (textBox == SupplierNameTextBox)
                            {
                                MessageBox.Show("Enter the name of the Supplier");
                            }
                            else if (textBox == DateTextBox)
                            {
                                MessageBox.Show("Enter the date");
                            }
                        }
                    }
                }
            }
        }
示例#2
0
        private void SupplierButton_Click(object sender, EventArgs e)
        {
            Suppliers S = new Suppliers();

            S.Show();
        }