using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace InventoryTest { public partial class CategoryManager : Form { private string ConnectionString { get; set; } private Dictionary CategoryIds { get; } = new(); private Dictionary SubCategoryIds { get; } = new(); private Dictionary CategoryPropertyIds { get; } = new(); private Dictionary SubCategoryPropertyIds { get; } = new(); private Dictionary PropertyTypeIds { get; } = new(); public CategoryManager(string connectionString) { ConnectionString = connectionString; InitializeComponent(); CategoryComboBoxManager.SelectedIndexChanged += CategoryComboBoxManager_SelectedIndexChanged; SubCategoryComboBoxManager.SelectedIndexChanged += SubCategoryComboBoxManager_SelectedIndexChanged; CategoryPropertyNameTextBox.TextChanged += CategoryPropertyNameTextBox_TextChanged; SubCategoryPropertyNameTextBox.TextChanged += SubCategoryNameTextBox_TextChanged; NewCategoryNameTextBox.TextChanged += NewCategoryNameTextBox_TextChanged; CategoryPropertiesListBox.SelectedIndexChanged += CategoryPropertiesListBox_SelectedIndexChanged; SubCategoryPropertiesListBox.SelectedIndexChanged += SubCategoryPropertiesListBox_SelectedIndexChanged; CategoryPropertyInfoNameTextBox.TextChanged += CategoryPropertyInfoNameTextBox_TextChanged; SubCategoryPropertyInfoNameTextBox.TextChanged += SubCategoryPropertyInfoNameTextBox_TextChanged; LoadPropertyTypes(); LoadCategories(); } private void SubCategoryPropertyInfoNameTextBox_TextChanged(object sender, EventArgs e) { SubCategoryPropertyInfoUpdateButton.Enabled = SubCategoryPropertyInfoNameTextBox.TextLength > 0 && (SubCategoryPropertiesListBox.SelectedIndex != -1 && SubCategoryPropertiesListBox.SelectedItem.ToString() != SubCategoryPropertyInfoNameTextBox.Text.Trim()); } private void CategoryPropertyInfoNameTextBox_TextChanged(object sender, EventArgs e) { CategoryPropertyInfoUpdateButton.Enabled = CategoryPropertyInfoNameTextBox.TextLength > 0 && (CategoryPropertiesListBox.SelectedIndex != -1 && CategoryPropertiesListBox.SelectedItem.ToString() != CategoryPropertyInfoNameTextBox.Text.Trim()); ; } private void LoadPropertyTypes() { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("SELECT * FROM dbo.GetPropertyTypes()", connection); connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { CategoryPropertyTypeComboBox.Items.Add(reader["Property Type"]); CategoryPropertyInfoTypeComboBox.Items.Add(reader["Property type"]); SubCategoryPropertyTypeComboBox.Items.Add(reader["Property Type"]); SubCategoryTypeInfoComboBox.Items.Add(reader["Property Type"]); PropertyTypeIds.Add(reader["Property Type"].ToString(), Convert.ToInt32(reader["Property Type ID"])); } CategoryPropertyTypeComboBox.SelectedIndex = CategoryPropertyInfoTypeComboBox.SelectedIndex = SubCategoryPropertyTypeComboBox.SelectedIndex = SubCategoryTypeInfoComboBox.SelectedIndex = 3; } private void SubCategoryPropertiesListBox_SelectedIndexChanged(object sender, EventArgs e) { DeleteSubCategoryPropertyButton.Enabled = SubCategoryPropertiesListBox.SelectedIndex >= 0; SubCategoryPropertyInfoNameTextBox.Enabled = SubCategoryPropertiesListBox.SelectedIndex >= 0; SubCategoryTypeInfoComboBox.Enabled = SubCategoryPropertiesListBox.SelectedIndex >= 0; if (SubCategoryPropertyInfoNameTextBox.Enabled) SubCategoryPropertyInfoNameTextBox.Text = SubCategoryPropertiesListBox.SelectedItem.ToString(); else SubCategoryPropertyInfoNameTextBox.Text = string.Empty; } private void CategoryPropertiesListBox_SelectedIndexChanged(object sender, EventArgs e) { DeleteCategoryPropertyButton.Enabled = CategoryPropertiesListBox.SelectedIndex >= 0; CategoryPropertyInfoNameTextBox.Enabled = CategoryPropertiesListBox.SelectedIndex >= 0; CategoryPropertyInfoTypeComboBox.Enabled = CategoryPropertiesListBox.SelectedIndex >= 0; if (CategoryPropertyInfoNameTextBox.Enabled) CategoryPropertyInfoNameTextBox.Text = CategoryPropertiesListBox.SelectedItem.ToString(); else CategoryPropertyInfoNameTextBox.Text = string.Empty; } private void NewCategoryNameTextBox_TextChanged(object sender, EventArgs e) { CreateNewCategoryButton.Enabled = !string.IsNullOrEmpty(NewCategoryNameTextBox.Text); CreateNewSubCategoryButton.Enabled = !string.IsNullOrEmpty(NewCategoryNameTextBox.Text); } private void SubCategoryNameTextBox_TextChanged(object sender, EventArgs e) { CreateSubCategoryButton.Enabled = !string.IsNullOrEmpty(SubCategoryPropertyNameTextBox.Text); } private void CategoryPropertyNameTextBox_TextChanged(object sender, EventArgs e) { CreateCategoryPropertyButton.Enabled = CategoryComboBoxManager.Items.Count > 0 && !string.IsNullOrEmpty(CategoryPropertyNameTextBox.Text); } private void SubCategoryComboBoxManager_SelectedIndexChanged(object sender, EventArgs e) { LoadSubCategoryProperties(); } private void CategoryComboBoxManager_SelectedIndexChanged(object sender, EventArgs e) { SubCategoryComboBoxManager.Items.Clear(); CategoryPropertiesListBox.Items.Clear(); SubCategoryPropertiesListBox.Items.Clear(); DeleteCategoryPropertyButton.Enabled = false; DeleteSubCategoryPropertyButton.Enabled = false; var id = CategoryIds[CategoryComboBoxManager.Text]; SubCategoryIds.Clear(); SubCategoryPropertyIds.Clear(); using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("SELECT * FROM dbo.GetCategorySubCategories(@ID)", connection); command.Parameters.AddWithValue("ID", id); connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { SubCategoryComboBoxManager.Items.Add(reader["Name"]); SubCategoryIds.Add(reader["Name"].ToString(), Convert.ToInt32(reader["ID"])); } reader.Close(); if (SubCategoryComboBoxManager.Items.Count > 0) { SubCategoryComboBoxManager.Enabled = true; SubCategoryComboBoxManager.SelectedIndex = 0; ToggleSubCategoryProperty(true); } else { ToggleSubCategoryProperty(false); SubCategoryComboBoxManager.Enabled = false; } LoadCategoryProperties(); } private void LoadCategories() { CategoryComboBoxManager.Items.Clear(); SubCategoryComboBoxManager.Items.Clear(); CategoryIds.Clear(); using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("SELECT [Category ID], [Category Name] FROM Category", connection); connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { CategoryIds.Add(reader["Category Name"].ToString(), Convert.ToInt32(reader["Category ID"])); CategoryComboBoxManager.Items.Add(reader["Category Name"]); } reader.Close(); if (CategoryComboBoxManager.Items.Count > 0) { ToggleCategoryProperty(true); CategoryComboBoxManager.SelectedIndex = 0; } else ToggleCategoryProperty(false); } private void LoadCategoryProperties() { CategoryPropertiesListBox.Items.Clear(); CategoryPropertyIds.Clear(); using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("SELECT * FROM GetCategoryProperties(@CategoryID)", connection); command.Parameters.AddWithValue("CategoryID", CategoryIds[CategoryComboBoxManager.Text]); connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { CategoryPropertyIds.Add(reader["Name"].ToString(), new Property { PropertyName = reader["Name"].ToString(), PropertyId = Convert.ToInt32(reader["Property ID"]), CategoryPropertyId = Convert.ToInt32(reader["Category Property ID"]), PropertyType = reader["Property Type"].ToString(), PropertyTypeId = Convert.ToInt32(reader["Property Type ID"]) }); CategoryPropertiesListBox.Items.Add(reader["Name"]); } } private void LoadSubCategoryProperties() { SubCategoryPropertiesListBox.Items.Clear(); SubCategoryPropertyIds.Clear(); DeleteSubCategoryPropertyButton.Enabled = false; using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("SELECT * FROM GetSubCategoryProperties(@ID)", connection); command.Parameters.AddWithValue("ID", SubCategoryIds[SubCategoryComboBoxManager.Text]); connection.Open(); var reader = command.ExecuteReader(); while (reader.Read()) { SubCategoryPropertyIds.Add(reader["Name"].ToString(), new Property { PropertyName = reader["Name"].ToString(), PropertyId = Convert.ToInt32(reader["Property ID"]), CategoryPropertyId = Convert.ToInt32(reader["Sub Category Property ID"]), PropertyType = reader["Property Type"].ToString(), PropertyTypeId = Convert.ToInt32(reader["Property Type ID"]) }); SubCategoryPropertiesListBox.Items.Add(reader["Name"]); } } private void ToggleSubCategoryProperty(bool enabled) { SubCategoryPropertyNameTextBox.Enabled = enabled; SubCategoryPropertyTypeComboBox.Enabled = enabled; DeleteSubCategoryButton.Enabled = enabled; } private void ToggleCategoryProperty(bool enabled) { CategoryPropertyNameTextBox.Enabled = enabled; CategoryPropertyTypeComboBox.Enabled = enabled; } private void CreateCategoryPropertyButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("CreateCategoryProperty", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("PropertyName", CategoryPropertyNameTextBox.Text); command.Parameters.AddWithValue("PropertyTypeID", PropertyTypeIds[CategoryPropertyTypeComboBox.Text]); command.Parameters.AddWithValue("CategoryID", CategoryIds[CategoryComboBoxManager.Text]); connection.Open(); command.ExecuteNonQuery(); LoadCategoryProperties(); CategoryPropertyNameTextBox.Text = string.Empty; CategoryPropertyNameTextBox.Focus(); } private void CreateSubCategoryPropertyButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("CreateSubCategoryProperty", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("PropertyName", SubCategoryPropertyNameTextBox.Text); command.Parameters.AddWithValue("PropertyTypeID", PropertyTypeIds[SubCategoryPropertyTypeComboBox.Text]); command.Parameters.AddWithValue("SubCategoryID", SubCategoryIds[SubCategoryComboBoxManager.Text]); connection.Open(); command.ExecuteNonQuery(); LoadSubCategoryProperties(); SubCategoryPropertyNameTextBox.Text = string.Empty; SubCategoryPropertyNameTextBox.Focus(); } private void CreateNewCategoryButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("CreateCategory", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("PropertyTypeID", PropertyTypeIds[CategoryPropertyTypeComboBox.Text]); command.Parameters.AddWithValue("Category", NewCategoryNameTextBox.Text); connection.Open(); CategoryIds.Add(NewCategoryNameTextBox.Text, Convert.ToInt32(command.ExecuteScalar())); CategoryComboBoxManager.Items.Add(NewCategoryNameTextBox.Text); NewCategoryNameTextBox.Text = string.Empty; NewCategoryNameTextBox.Focus(); if (CategoryComboBoxManager.SelectedIndex == -1) CategoryComboBoxManager.SelectedIndex = 0; } private void CreateNewSubCategoryButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("CreateSubCategory", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("SubCategory", NewCategoryNameTextBox.Text); command.Parameters.AddWithValue("CategoryID", CategoryIds[CategoryComboBoxManager.Text]); connection.Open(); SubCategoryIds.Add(NewCategoryNameTextBox.Text, Convert.ToInt32(command.ExecuteScalar())); SubCategoryComboBoxManager.Items.Add(NewCategoryNameTextBox.Text); NewCategoryNameTextBox.Text = string.Empty; NewCategoryNameTextBox.Focus(); SubCategoryComboBoxManager.Enabled = true; if (SubCategoryComboBoxManager.SelectedIndex == -1) SubCategoryComboBoxManager.SelectedIndex = 0; SubCategoryPropertyNameTextBox.Enabled = true; DeleteSubCategoryButton.Enabled = true; } private void DeleteCategoryButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("DeleteCategory", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("CategoryID", CategoryIds[CategoryComboBoxManager.Text]); connection.Open(); command.ExecuteNonQuery(); LoadCategories(); } private void DeleteSubCategoryButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("DeleteSubCategory", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("SubCategoryID", SubCategoryIds[SubCategoryComboBoxManager.Text]); connection.Open(); command.ExecuteNonQuery(); SubCategoryIds.Remove(SubCategoryComboBoxManager.Text); SubCategoryComboBoxManager.Items.RemoveAt(SubCategoryComboBoxManager.SelectedIndex); } private void DeleteCategoryPropertyButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("DeleteCategoryProperty", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("CategoryPropertyID", CategoryPropertyIds[CategoryPropertiesListBox.SelectedItem.ToString()].CategoryPropertyId); connection.Open(); command.ExecuteNonQuery(); CategoryPropertyIds.Remove(CategoryPropertiesListBox.SelectedItem.ToString()); CategoryPropertiesListBox.Items.RemoveAt(CategoryPropertiesListBox.SelectedIndex); } private void DeleteSubCategoryPropertyButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("DeleteSubCategoryProperty", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("SubCategoryPropertyID", SubCategoryPropertyIds[SubCategoryPropertiesListBox.SelectedItem.ToString()].CategoryPropertyId); connection.Open(); command.ExecuteNonQuery(); SubCategoryPropertyIds.Remove(SubCategoryPropertiesListBox.SelectedItem.ToString()); SubCategoryPropertiesListBox.Items.RemoveAt(SubCategoryPropertiesListBox.SelectedIndex); } private struct Property { public string PropertyName; public int PropertyId; public int CategoryPropertyId; public int PropertyTypeId; public string PropertyType; } private void CategoryPropertyInfoUpdateButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("UpdateProperty", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("PropertyID", CategoryPropertyIds[CategoryPropertiesListBox.SelectedItem.ToString()].PropertyId); command.Parameters.AddWithValue("PropertyName", CategoryPropertyInfoNameTextBox.Text); command.Parameters.AddWithValue("PropertyTypeID", PropertyTypeIds[CategoryPropertyInfoTypeComboBox.Text]); connection.Open(); command.ExecuteNonQuery(); LoadCategoryProperties(); CategoryPropertyInfoNameTextBox.Text = string.Empty; } private void SubCategoryPropertyInfoUpdateButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("UpdateProperty", connection) { CommandType = CommandType.StoredProcedure }; command.Parameters.AddWithValue("PropertyID", SubCategoryPropertyIds[SubCategoryPropertiesListBox.SelectedItem.ToString()].PropertyId); command.Parameters.AddWithValue("PropertyName", SubCategoryPropertyInfoNameTextBox.Text); command.Parameters.AddWithValue("PropertyTypeID", PropertyTypeIds[SubCategoryTypeInfoComboBox.Text]); connection.Open(); command.ExecuteNonQuery(); LoadSubCategoryProperties(); SubCategoryPropertyInfoNameTextBox.Text = string.Empty; } } }