private void RefreshLists() { var storageHandler = new SqlCeStorageHandler(); ListBoxLists.DataSource = storageHandler.GetAllLists(); ListBoxLists.DisplayMember = "Name"; }
private void RefreshItems() { var storageHandler = new SqlCeStorageHandler(); ListBoxItems.DataSource = storageHandler.GetAllItems(ListId); ListBoxItems.DisplayMember = "Name"; }
private void menuItem1_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(this.textBoxName.Text)) { MessageBox.Show("Item name cannot be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } try { if (IsNewItem) { var item = new Item() { Name = textBoxName.Text.Trim(), Description = textBoxDescription.Text.Trim(), UserID = new Guid(Settings.ClientId), ID = Guid.NewGuid(), ListID = this.ListId, Priority = ((Priority)comboBoxPriority.SelectedItem).ID, Status = ((Status)comboBoxStatus.SelectedItem).ID, ServiceMetadata = new OfflineEntityMetadata() { IsTombstone = false } }; var storageHandler = new SqlCeStorageHandler(); storageHandler.InsertItem(item, true); MessageBox.Show("Item created!", "Success", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1); } else { Item.Name = textBoxName.Text.Trim(); Item.Description = textBoxDescription.Text.Trim(); Item.Priority = ((Priority)comboBoxPriority.SelectedItem).ID; Item.Status = ((Status)comboBoxStatus.SelectedItem).ID; var storageHandler = new SqlCeStorageHandler(); storageHandler.UpdateItem(Item, true); MessageBox.Show("Item updated!", "Success", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1); } this.Close(); } catch (Exception exception) { MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); } }
private void menuItem6_Click(object sender, EventArgs e) { SqlCeStorageHandler.CloseConnection(); if (File.Exists(DataStoreHelper.ListDbFileName)) { File.Delete(DataStoreHelper.ListDbFileName); } Close(); }
private void LoadPriorityAndStatus() { var handler = new SqlCeStorageHandler(); comboBoxPriority.DataSource = handler.GetAllPriority(); comboBoxPriority.DisplayMember = "Name"; comboBoxStatus.DataSource = handler.GetAllStatus(); comboBoxStatus.DisplayMember = "Name"; }
private void menuItemDelete_Click(object sender, EventArgs e) { var item = ListBoxItems.SelectedItem as Item; if (null != item) { var storageHandler = new SqlCeStorageHandler(); storageHandler.TombstoneItem(item); } RefreshItems(); }
private void menuItem5_Click(object sender, EventArgs e) { var listItem = ListBoxLists.SelectedItem as List; if (null != listItem) { var storageHandler = new SqlCeStorageHandler(); storageHandler.TombstoneList(listItem); } RefreshLists(); }
private void MenuItemSave_Click(object sender, System.EventArgs e) { if (String.IsNullOrEmpty(this.textBoxName.Text)) { MessageBox.Show("List name cannot be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } try { if (IsNewItem) { var listItem = new List { ID = Guid.NewGuid(), Name = textBoxName.Text, Description = textBoxDescription.Text, CreatedDate = DateTime.Now, UserID = new Guid(Settings.ClientId), ServiceMetadata = new OfflineEntityMetadata() { IsTombstone = false } }; var storageHandler = new SqlCeStorageHandler(); storageHandler.InsertList(listItem, true); MessageBox.Show("List created!", "Success", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1); } else { ListItem.Name = textBoxName.Text.Trim(); ListItem.Description = textBoxDescription.Text.Trim(); var storageHandler = new SqlCeStorageHandler(); storageHandler.UpdateList(ListItem, true); MessageBox.Show("List updated!", "Success", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1); } this.Close(); } catch (Exception exception) { MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); } }
/// <summary> /// Create a database if one does not exist. /// </summary> public static void InitializeDataStore() { _dbConnectionstring = String.Format(CONNECTION_STRING_TEMPLATE, ListDbFileName); if (!File.Exists(ListDbFileName)) { CreateDatabase(); } // We maintain an static open connection to the local SQL CE database // for the time the application runs. In case we switch users, without restarting the application, the connection // needs to be reset so that it does not point to the previous users database. SqlCeStorageHandler.CloseConnection(); }
/// <summary> /// OfflineSyncProvider method implementation called when a change set returned from GetChangeSet has been /// successfully uploaded. /// </summary> /// <param name="state">The unique identifier passed in to the GetChangeSet call.</param> /// <param name="response">ChangeSetResponse that contains an updated server blob and any conflicts or errors that /// happened on the service.</param> public override void OnChangeSetUploaded(Guid state, ChangeSetResponse response) { if (null == response) { throw new ArgumentNullException("response"); } if (null != response.Error) { throw new Exception("Exception during sync!"); } var storageHandler = new SqlCeStorageHandler(); if (null != response.UpdatedItems && 0 != response.UpdatedItems.Count) { foreach (var item in response.UpdatedItems) { var offlineEntity = (SqlCeOfflineEntity)item; storageHandler.ApplyItem(offlineEntity); } } if (null != response.Conflicts && 0 != response.Conflicts.Count) { foreach (var conflict in response.Conflicts) { // We have an conflict so apply the LiveEntity var liveEntity = (SqlCeOfflineEntity)conflict.LiveEntity; // For a SyncError, which resulted from a client insert, the winning item may be a tombstone version // of the client entity. In this case, the ServiceMetadata.Id property of the LiveEntity will be null. // We need to lookup the item using primary keys in order to update it. if (conflict.GetType() == typeof(SyncError)) { var errorEntity = ((SyncError)conflict).ErrorEntity; if (!liveEntity.ServiceMetadata.IsTombstone) { // If the live entity is not a tombstone, then we just need to update the entity. storageHandler.ApplyItem(liveEntity); } else { // At this point, the LiveEntity is a tombstone and does not have primary key info. // If the live entity is a tombstone, then delete the item by looking up the primary key // from the error entity. // The error entity in this case will have both Id and the primary keys. errorEntity.ServiceMetadata.IsTombstone = true; errorEntity.ServiceMetadata.Id = null; storageHandler.ApplyItem((SqlCeOfflineEntity)errorEntity); } } else { storageHandler.ApplyItem(liveEntity); } } } // Clear all the isdirty flags and delete all rows with IsTombstone = true storageHandler.ResetDirtyAndDeleteTombstones(); storageHandler.SaveAnchor(response.ServerBlob); }
private void buttonLogin_Click(object sender, EventArgs e) { try { if (String.IsNullOrEmpty(textBoxUserName.Text.Trim())) { MessageBox.Show("UserName cannot be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } var webRequest = (HttpWebRequest)WebRequest.Create(String.Format(Settings.LoginUrl, textBoxUserName.Text.Trim())); var response = (HttpWebResponse)webRequest.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { MessageBox.Show("Login failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } using (Stream responseStream = response.GetResponseStream()) { // Read the GUID using (var reader = new StreamReader(responseStream)) { var clientId = reader.ReadToEnd(); DataStoreHelper.ListDbFileName = clientId + ".sdf"; Settings.ClientId = clientId; } } if (String.IsNullOrEmpty(Settings.ClientId) || String.IsNullOrEmpty(DataStoreHelper.ListDbFileName)) { MessageBox.Show("Login failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } bool newUser = false; if (!File.Exists(DataStoreHelper.ListDbFileName)) { newUser = true; } DataStoreHelper.InitializeDataStore(); var storageHandler = new SqlCeStorageHandler(); byte[] anchor = storageHandler.GetAnchor(); if (null == anchor || 0 == anchor.Length) { newUser = true; } if (newUser) { // Do an initial sync and open the list view DoInitialSync(); } else { var lists = new Lists(); lists.ShowDialog(); } } catch (Exception exception) { MessageBox.Show(exception.Message, "Unhandled Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); } }