コード例 #1
0
        private void SubmitEdits_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(EditName.Text) && !string.IsNullOrWhiteSpace(EditDescription.Text) && !string.IsNullOrWhiteSpace(EditPrice.Text) && !string.IsNullOrWhiteSpace(EditQuantity.Text))
            {
                try
                {
                    connection.Open();

                    MySqlCommand cmd = new MySqlCommand();
                    cmd.CommandText = "update items set ITEM_NAME=@ItemName,ITEM_PRICE=@ItemPrice,ITEM_DESCRIPTION=@ItemDescription,ITEM_QUANTATY=@ItemQuantity where ITEM_ID=@ItemId";
                    cmd.Parameters.AddWithValue("@ItemName", EditName.Text);
                    cmd.Parameters.AddWithValue("@ItemPrice", EditPrice.Text);
                    cmd.Parameters.AddWithValue("@ItemDescription", EditDescription.Text);
                    cmd.Parameters.AddWithValue("@ItemQuantity", EditQuantity.Text);
                    cmd.Parameters.AddWithValue("@ItemId", EditId.Text);
                    cmd.Connection = connection;
                    cmd.ExecuteNonQuery();
                    MySqlCommand Com = new MySqlCommand("Select * from items ", connection);

                    MySqlDataAdapter adp = new MySqlDataAdapter(Com);
                    DataSet          ds  = new DataSet();
                    adp.Fill(ds, "LoadDataBinding");
                    EditDataGrid.DataContext = ds;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    connection.Close();
                    MessageBox.Show("Successfully updated", "Success");
                    EditName.Clear();
                    EditDescription.Clear();
                    EditPrice.Clear();
                    EditQuantity.Clear();
                    EditId.Clear();
                }
            }
            else
            {
                MessageBox.Show("Cannot Edit Empty Row ", "ERROR");
            }
        }
コード例 #2
0
        private void DeleteEdits_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(EditName.Text) && !string.IsNullOrWhiteSpace(EditDescription.Text) && !string.IsNullOrWhiteSpace(EditPrice.Text) && !string.IsNullOrWhiteSpace(EditQuantity.Text))
            {
                MessageBoxResult choice = MessageBox.Show("delete selected item ?", "confirmation", MessageBoxButton.YesNo);
                if (choice == MessageBoxResult.Yes)
                {
                    try
                    {
                        connection.Open();
                        MySqlCommand cmd = new MySqlCommand("delete from items where ITEM_ID='" + int.Parse(EditId.Text) + "'", connection);
                        cmd.ExecuteNonQuery();
                        MySqlCommand Com = new MySqlCommand("Select * from items ", connection);

                        MySqlDataAdapter adp = new MySqlDataAdapter(Com);
                        DataSet          ds  = new DataSet();
                        adp.Fill(ds, "LoadDataBinding");
                        EditDataGrid.DataContext = ds;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    finally
                    {
                        connection.Close();
                        EditId.Clear();
                        EditName.Clear();
                        EditQuantity.Clear();
                        EditPrice.Clear();
                        EditDescription.Clear();
                        MessageBox.Show("item deleted", "confirmed");
                    }
                }
            }
            else
            {
                MessageBox.Show("Select a row to delete", "ERROR");
            }
        }