From f014f22ecf75919f4b3c3fcb846482367bac33a4 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Tue, 13 Apr 2021 18:18:44 -0500 Subject: [PATCH] Added support for updating category properties. --- .vs/InventoryTest/v16/.suo | Bin 41472 -> 36864 bytes InventoryTest/CategoryManager.Designer.cs | 219 +++++++++++++++++++++- InventoryTest/CategoryManager.cs | 157 +++++++++++++++- 3 files changed, 368 insertions(+), 8 deletions(-) diff --git a/.vs/InventoryTest/v16/.suo b/.vs/InventoryTest/v16/.suo index c016f50e6e506e8464a46c66997499e4df007eab..df123e956ecd02a247d2cf18f68a248d1a3476b7 100644 GIT binary patch delta 1321 zcmZoT!ql*UX@Y?)0}Kc-Fz_)jFfcJNF#P-f|365Waq>eJ@r?;{7$-61a2gyG64`BV zol$OcDbr%cdSM0z21y1629N>&|NsBb#=yV;qCt8=G%EuG0~b^b#0Ozf1_lNh1_lNW z1_lNR1_lOc1_lNx1_lOx1_lNZ1_lN!Ipu6 z!D;eC7GXhW1_lOKs7i4L1_mC8_6r~%E6iUh4D~S^l8-@#fqV_}G0e9hF%~>N<{UIW z=0)-GWJeb1i3t}rak;SYm@zOgs6m5ba-)^XAr0jLph4PPXAm?l3=joutB^^zNl zgVcz3ka_?!4=4?R(iqJA$tBu7oAiz_PCnvovnYsZkpa^tofC{4uw)8$^B3O$Mjmc7 zhfOZ=58#nwU|;~HdYEaOznETN8sP})R@noF=TyBL^n=C=!F6l?or^YnD%C-2W| z44gLqd0*aTzu=d`+Raso{p>1fCW* zsVQljbwgtr8JA2p2rZaAC){*$SJ=YI7sK@@2ZVNQ?uzkXJ201V@`HSZ zO&TuPy}rncX;BcLXtsrbQ(zo6L?dVt9;u6*PS?FfeTX zZ^_HTY+_-8-;?%jEQ>0bHl=YgtC$!kni`myC+nsdnwaaFBwHBiCYf7W>ZVy(SQw@l zm>VRg7~$~irmE|VlPA{fpM1S~*W`fO4V&7gGKryj8{~Zk2AAWL3wk;y=eJvJ&YM1o zX;aK4MzKnG@aP27#Doo#m~uD|?A-_RHQQoFU6`*yJ_Z>C!fa6AgV-?4LUI^z46ZQXfrh~* zE*BOa1*Ev1JW)txatYsgUIt+X1_pU(ph86X19(7D0gFD6;N~y<0ZcrgQ~-)_Q1F07 zON1^kVi6VTWt{xTIcjsX#4^UsjADlwdALFGfn>?%FXB8*n`B(LusBMMct@!RF!Sg# zFff1|26NQp678N%Mkg3K#E|S`UC6+|aA)&R?bD1r`bbF)0+=>MpgDK*mq;F_O=d?Jc|Zvn z*(TOiph(>OQ9P*0-D+}Pwc_T@A-;_KDXD3Rr8y<>CAkI876vBB-tJ{rH#SYNNU<hXZ`4U1w}(A8>gs+h`zJrIsV4Jn^IJw#@5e|6!cvWYlNu3_5Lww;mDX!DMC j52nd~EZjD`_3UB<8; CategoryIds { get; } = new(); - private static Dictionary SubCategoryIds { get; } = new(); + 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) { @@ -30,18 +33,62 @@ namespace InventoryTest 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) @@ -57,7 +104,7 @@ namespace InventoryTest private void CategoryPropertyNameTextBox_TextChanged(object sender, EventArgs e) { - CreateCategoryPropertyButton.Enabled = !string.IsNullOrEmpty(CategoryPropertyNameTextBox.Text); + CreateCategoryPropertyButton.Enabled = CategoryComboBoxManager.Items.Count > 0 && !string.IsNullOrEmpty(CategoryPropertyNameTextBox.Text); } private void SubCategoryComboBoxManager_SelectedIndexChanged(object sender, EventArgs e) @@ -74,6 +121,7 @@ namespace InventoryTest 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); @@ -138,6 +186,7 @@ namespace InventoryTest private void LoadCategoryProperties() { CategoryPropertiesListBox.Items.Clear(); + CategoryPropertyIds.Clear(); using var connection = new SqlConnection(ConnectionString); using var command = new SqlCommand("SELECT * FROM GetCategoryProperties(@CategoryID)", connection); @@ -148,12 +197,23 @@ namespace InventoryTest 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); @@ -164,25 +224,38 @@ namespace InventoryTest 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("CreateCategoryPropertyByName", connection) { CommandType = CommandType.StoredProcedure }; + 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(); @@ -195,11 +268,12 @@ namespace InventoryTest CategoryPropertyNameTextBox.Focus(); } - private void CreateSubCategoryButton_Click(object sender, EventArgs e) + private void CreateSubCategoryPropertyButton_Click(object sender, EventArgs e) { using var connection = new SqlConnection(ConnectionString); - using var command = new SqlCommand("CreateSubCategoryPropertyByName", connection) { CommandType = CommandType.StoredProcedure }; + 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(); @@ -216,6 +290,7 @@ namespace InventoryTest { 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(); @@ -283,7 +358,75 @@ namespace InventoryTest 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; } } }