コード例 #1
0
        private void BtnAddItem_Click(object sender, EventArgs e)
        {
            using (AddItemForm addItemForm = new AddItemForm(this))
            {
                addItemForm.ShowDialog();

                if (addItemForm.CanProceed)
                {
                    string item            = addItemForm.Item.Replace("_", " ");
                    string itemTranslation = addItemForm.Translation.Replace("_", " ");

                    using (SqlConnection connection = new SqlConnection(this._connectionString))
                    {
                        using (SqlCommand sqlCommand = new SqlCommand("MyDictionaryDB.uspAddItem", connection))
                        {
                            sqlCommand.CommandType = CommandType.StoredProcedure;

                            sqlCommand.Parameters.AddWithValue("@word", item);
                            sqlCommand.Parameters.AddWithValue("@WordTranslation", itemTranslation);

                            try
                            {
                                connection.Open();

                                sqlCommand.ExecuteNonQuery();

                                new ToolTip().Show($"Item {addItemForm.Item} has been added successfuly.",
                                                   this.btnAddItem,
                                                   this.btnAddItem.Location.X,
                                                   this.btnAddItem.Location.Y,
                                                   5000);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("Could not add item and translation" +
                                                Environment.NewLine +
                                                ex.Message);
                            }
                            finally
                            {
                                connection.Close();
                            }
                        }
                    }
                }

                RefreshListBox();
                RefreshNoWrittenWordsWarning();
            }
        }