private void tsmiDeleteProduct_Click(object sender, EventArgs e) { if (this.lvProducts.SelectedItems.Count > 0){ int idx = this.lvProducts.SelectedItems[0].Index; System.Data.DataRow row = this.products.Rows[idx]; bool no_errors = true; int cnt = 0; try{ this.cConnection.Open(); string sQuery = "SELECT ISNULL(COUNT(*), 0) FROM Purchases.ReceiptContents WHERE ProductID = @Product"; System.Data.SqlClient.SqlCommand check_cmd = new System.Data.SqlClient.SqlCommand(); check_cmd.Parameters.AddWithValue( "@Product", row["ProductID"] ); check_cmd.Connection = this.cConnection; check_cmd.CommandTimeout = 0; check_cmd.CommandType = System.Data.CommandType.Text; check_cmd.CommandText = sQuery; cnt = (int)check_cmd.ExecuteScalar(); }catch(System.Exception ex ){ no_errors = false; MessageBox.Show(ex.Message); }finally{ this.cConnection.Close(); } if (no_errors && (cnt > 0) && (MessageBox.Show("Найдены заказы (" + cnt.ToString() + "), содержащие выбранный продукт, переназначить (это действие нельзы отменить)?", "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand) == DialogResult.Yes) ){ List<Producer.Product.OrderColumn> ord = new List<Producer.Product.OrderColumn>(); ord.Add(Producer.Product.OrderColumn.ProductName); System.Data.SqlClient.SqlCommand command = Producer.Product.Select((Guid)row["Category"], null, null, ord); command.Connection = this.cConnection; System.Data.SqlClient.SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter(command); System.Data.DataTable tbl = new System.Data.DataTable("Products"); sda.Fill(tbl); VisualControls.ListOptions options = new VisualControls.ListOptions(false); VisualControls.ElementsList elist = new VisualControls.ElementsList(options, "Товары", "ProductName", "ProductID", tbl); if (elist.ShowDialog() == DialogResult.OK){ if (elist.Selected.Length == 1){ int new_id = (int)elist.Selected[0]; string query = "UPDATE Purchases.ReceiptContents SET ProductID = @NewId WHERE ProductID = @OldId"; command = new System.Data.SqlClient.SqlCommand(); command.Connection = this.cConnection; command.Parameters.AddWithValue("@OldId", row["ProductID"]); command.Parameters.AddWithValue("@NewId", new_id); command.CommandTimeout = 0; command.CommandType = System.Data.CommandType.Text; command.CommandText = query; } } } } }
private void tsmiDeleteCategory_Click(object sender, EventArgs e) { try{ if (this.clbxTypes.SelectedItems.Count > 0){ int idx = this.clbxTypes.Items.IndexOf( this.clbxTypes.SelectedItems[0] ); System.Data.DataRow row = this.types_table.Rows[idx]; this.cConnection.Open(); string sQuery = "SELECT ISNULL(COUNT(*), 0) FROM Producer.Products WHERE Category = @Category AND Type = @Type"; System.Data.SqlClient.SqlCommand check_cmd = new System.Data.SqlClient.SqlCommand(); check_cmd.Parameters.AddWithValue("@Category", row["Category"]); check_cmd.Parameters.AddWithValue("@Type", row["TypeId"]); check_cmd.Connection = this.cConnection; check_cmd.CommandTimeout = 0; check_cmd.CommandType = System.Data.CommandType.Text; check_cmd.CommandText = sQuery; int cnt = (int)check_cmd.ExecuteScalar(); if (( cnt > 0 ) && (MessageBox.Show("Найдены товары (" + cnt.ToString() + "), содержащие выбранный продукт, переназначить (это действие нельзы отменить)?", "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Hand) == DialogResult.Yes) ){ System.Data.SqlClient.SqlCommand command = Producer.ProductTypes.Select((Guid)row["Category"] ); command.Connection = this.cConnection; System.Data.SqlClient.SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter(command); System.Data.DataTable tbl = new System.Data.DataTable("ProductTypes"); sda.Fill(tbl); VisualControls.ListOptions options = new VisualControls.ListOptions(false); VisualControls.ElementsList elist = new VisualControls.ElementsList(options, "Подкатегории", "Name", "TypeId", tbl); if (elist.ShowDialog() == DialogResult.OK) { if (elist.Selected.Length == 1){ int new_id = (int)elist.Selected[0]; string query = "UPDATE Producer.Products SET Type = @NewType WHERE Category = @Category AND Type = @OldType"; command = new System.Data.SqlClient.SqlCommand(); command.Connection = this.cConnection; command.Parameters.AddWithValue("@OldType", row["TypeId"]); command.Parameters.AddWithValue("@NewType", new_id); command.Parameters.AddWithValue("@Category", row["Category"]); command.CommandTimeout = 0; command.CommandType = System.Data.CommandType.Text; command.CommandText = query; } } } } }catch (System.Exception ex){ MessageBox.Show("Ошибка при удалении категории товаров:\n" + ex.Message); } return; }