示例#1
0
        private void AddButton_Click(object sender, EventArgs e)
        {
            const string error    = "You have provided invalid details. Please try again.";
            var          quantity = Convert.ToInt32(QuantityUpDown.Text);
            var          prodCode = ProductCodeComboBox.Text;

            _connection = DB_Connect.connect();
            _connection.Open();
            _sda = new SqlDataAdapter(@"SELECT * FROM[Products] WHERE[ProductCode] = '" + prodCode + "'", _connection);
            _dt  = new DataTable();
            _sda.Fill(_dt);
            dataGridView1.Rows.Clear();

            if (_dt.Rows.Count == 1 && quantity != 0)
            {
                foreach (DataRow unused in _dt.Rows)
                {
                    NumOfItems = NumOfItems + 1;
                }

                _command = new SqlCommand(@"INSERT INTO[Ordered Items]([OrderID], [ProductCode], [Quantity]) VALUES
                   ('" + _randCode + "', '" + prodCode + "', '" + quantity + "')", _connection);
                _command.ExecuteNonQuery();
                _command = new SqlCommand(@"INSERT INTO[Cart]([ProductCode], [Quantity]) VALUES
                   ('" + prodCode + "', '" + quantity + "')", _connection);
                _command.ExecuteNonQuery();
                _sda = new SqlDataAdapter(@"SELECT * FROM[Cart]", _connection);
                _dt  = new DataTable();
                _sda.Fill(_dt);
                dataGridView1.Rows.Clear();

                foreach (DataRow item in _dt.Rows)
                {
                    var n = dataGridView1.Rows.Add();
                    dataGridView1.Rows[n].Cells[0].Value = item["ProductCode"].ToString();
                    dataGridView1.Rows[n].Cells[1].Value = item["Quantity"].ToString();
                }
            }
            else
            {
                MessageBox.Show(error);
                QuantityUpDown.Value = 0;
                QuantityUpDown.Focus();
            }

            _sda.Dispose();
            _connection.Close();
        }