Esempio n. 1
0
        /******************************/
        /*      Other Functions       */
        /******************************/
        #region Other Functions

        /// <summary>
        /// LoadTable
        /// SELECT
        /// </summary>
        private void LoadTable()
        {
            using (var EntitiesContext = new TestDBEntities())
            {
                try
                {
                    NameList = new ObservableCollection <Name>();
                    var Names = EntitiesContext.Names;
                    foreach (var name in Names)
                    {
                        NameList.Add(name);
                    }
                    DataGrid_TableNames.ItemsSource = NameList;

                    Message = "Load Data from SQL";
                }
                catch (Exception)
                {
                    Message = "ERROR can not load data from SQL-Server!";
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// DataGrid_TableNames_PreviewKeyDown
        /// Here we perform the Delete
        /// DELETE on Del-KeyDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGrid_TableNames_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var Grid = (DataGrid)sender;

            if (Key.Delete == e.Key)
            {
                using (var EntitiesContext = new TestDBEntities())
                {
                    try
                    {
                        long Id         = ((MyMSEntityFrameWorkApp.Name)Grid.SelectedItem).Id;
                        Name EntityItem = EntitiesContext.Names.First(n => n.Id == Id);
                        EntitiesContext.Names.Remove(EntityItem);
                        EntitiesContext.SaveChanges();
                        Message = "Save Data to SQL";
                    }
                    catch (Exception)
                    {
                        Message = "ERROR on Delete";
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// DataGrid_TableNames_RowEditEnding
        /// INSERT or UPDATE depents on IsAddingANewItem
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGrid_TableNames_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            var Grid = (DataGrid)sender;

            Grid.RowEditEnding -= DataGrid_TableNames_RowEditEnding; // CommitEdit and Items.Refresh is not possible when event is subscribed
            Grid.CommitEdit();
            Grid.Items.Refresh();
            Grid.RowEditEnding += DataGrid_TableNames_RowEditEnding;
            using (var EntitiesContext = new TestDBEntities())
            {
                // INSERT
                if (IsAddingANewItem)
                {
                    if (SelectedItem != null)
                    {
                        Name EntityItem = new Name();
                        EntityItem.FirstName  = SelectedItem.FirstName;
                        EntityItem.LastName   = SelectedItem.LastName;
                        EntityItem.Age        = SelectedItem.Age;
                        EntityItem.InsertDate = DateTime.Now;
                        EntitiesContext.Names.Add(EntityItem);
                        IsAddingANewItem = false;
                    }
                    else
                    {
                        Message = String.Format("ERROR on Insert");
                    }
                }
                else // UPDATE
                {
                    Name UpdateItem = Grid.SelectedItem as MyMSEntityFrameWorkApp.Name;
                    if (UpdateItem != null)
                    {
                        Name EntityItem = EntitiesContext.Names.Where(o => o.Id == UpdateItem.Id).FirstOrDefault();
                        if (EntityItem != null)
                        {
                            EntityItem.FirstName  = UpdateItem.FirstName;
                            EntityItem.LastName   = UpdateItem.LastName;
                            EntityItem.Age        = UpdateItem.Age;
                            EntityItem.InsertDate = UpdateItem.InsertDate;
                        }
                        else
                        {
                            Message = String.Format("ERROR on Update");
                        }
                    }
                    else
                    {
                        Message = String.Format("ERROR on Update");
                    }
                }

                // Save the changes to SQL DB either a row is added or just updated
                try
                {
                    EntitiesContext.SaveChanges();
                    Message = "Save Data to SQL";
                }
                catch (Exception)
                {
                    Message = "ERROR on Save Data to SQL";
                }
            }
        }