//****************************** //* * //* InsertUpdateEntity_Click * //* * //****************************** // Insert or update the entity in cloud storage. private void InsertUpdateEntity_Click(object sender, RoutedEventArgs e) { String action = "update entity"; if (IsAddNew) { action = "insert entity"; } // Construct entity ElasticTableEntity entity = new ElasticTableEntity(); entity.RowKey = RowKey.Text; entity.PartitionKey = PartitionKey.Text; int fieldId; String fieldName, fieldType, fieldValue; foreach (KeyValuePair <int, TextBox> field in fieldNames) { fieldId = field.Key; TextBox nameTextBox = field.Value; ComboBox typeComboBox = fieldTypes[fieldId]; TextBox valueTextBox = fieldValues[fieldId]; fieldName = nameTextBox.Text; if (String.IsNullOrEmpty(fieldName)) { MessageBox.Show("Cannot " + action + ": '" + fieldName + "' is not a valid propert name", "Invalid Property Name"); return; } ComboBoxItem item = typeComboBox.SelectedItem as ComboBoxItem; fieldType = item.Content as String; fieldValue = valueTextBox.Text; switch (fieldType) { case "Guid": { Guid guidValue; if (Guid.TryParse(fieldValue, out guidValue)) { entity[fieldName] = guidValue; } else { MessageBox.Show("Cannot update entity: " + fieldName + " does not contain a valid GUID value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "String": entity[fieldName] = fieldValue; break; case "Binary": { try { string hexValues = fieldValue; string[] hexValuesSplit = hexValues.Split(' '); byte[] bytes = new byte[hexValuesSplit.Length]; int offset = 0; foreach (String hex in hexValuesSplit) { bytes[offset++] = (byte)Convert.ToInt32(hex, 16); } entity[fieldName] = bytes; } catch (Exception ex) { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid hexadecimal bytes representation: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Boolean": { bool boolValue = false; switch (fieldValue.ToLower()) { case "1": case "true": case "yes": case "on": fieldValue = "True"; break; case "0": case "false": case "no": case "off": fieldValue = "False"; break; } if (Boolean.TryParse(fieldValue, out boolValue)) { entity[fieldName] = boolValue; } else { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid boolean value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "DateTime": { DateTime dateValue; if (DateTime.TryParse(fieldValue, out dateValue)) { entity[fieldName] = dateValue; } else { MessageBox.Show("Cannot update entity: " + fieldName + " does not contain a valid DateTime value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Double": { double doubleValue = 0; if (Double.TryParse(fieldValue, out doubleValue)) { entity[fieldName] = doubleValue; } else { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid double-precision value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Int32": { int intValue = 0; if (Int32.TryParse(fieldValue, out intValue)) { entity[fieldName] = intValue; } else { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid Int32 value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Int64": { Int64 intValue = 0; if (Int64.TryParse(fieldValue, out intValue)) { entity[fieldName] = intValue; } else { MessageBox.Show("Cannot " + action + ": " + fieldName + " does not contain a valid Int64 value: " + fieldValue, "Invalid Value"); this.Cursor = Cursors.Arrow; return; } } break; case "Null": // Type "Null" means, do not add to entity. break; default: MessageBox.Show("Cannot " + action + ": unknown type '" + fieldType + "'"); this.Cursor = Cursors.Arrow; return; } } // next field try { if (IsAddNew) { // Insert entity and keep dialog open. this.Cursor = Cursors.Wait; Table.Execute(TableOperation.Insert(entity)); RecordsAdded++; Message.Text = "Records Added: " + RecordsAdded.ToString(); CmdClose.Content = new TextBlock() { Text = "Close" }; this.Cursor = Cursors.Arrow; RowKey.Focus(); } else { // Update entity and close dialog. this.Cursor = Cursors.Wait; entity.ETag = "*"; //Table.Execute(TableOperation.Merge(entity)); Table.Execute(TableOperation.Replace(entity)); RecordsUpdated++; Message.Text = "Records Updaed: " + RecordsUpdated.ToString(); CmdClose.Content = new TextBlock() { Text = "Close" }; this.Cursor = Cursors.Arrow; //RowKey.Focus(); DialogResult = true; } } catch (Exception ex) { this.Cursor = Cursors.Arrow; if (IsAddNew) { Message.Text = "Error inserting record: " + ex.Message; } else { Message.Text = "Error updating record: " + ex.Message; } RowKey.Focus(); } }