Exemple #1
0
 private void Cancel_Click(object sender, RoutedEventArgs e)
 {
     Accounting.IsHitTestVisible = true;
     try
     {
         Accounting.ChangeLocationView();
     }
     catch (Exception exception)
     {
         MessageBox.Show(exception.Message, exception.GetType().Name, MessageBoxButton.OK,
                         MessageBoxImage.Error);
     }
     Close();
 }
Exemple #2
0
        private void SaveChanges_Click(object sender, RoutedEventArgs e)
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                try
                {
                    connection.Open();
                    if (AudienceTableID == 0)
                    {
                        var command = new SqlCommand("AddAudienceTable", connection)
                        {
                            CommandType = CommandType.StoredProcedure
                        };
                        command.Parameters.Add(new SqlParameter("@AudienceID", AudienceID));
                        command.Parameters.Add(new SqlParameter("@Name", CurrentPlace.Name));
                        command.Parameters.Add(new SqlParameter("@Description", CurrentPlace.Description));
                        command.Parameters.Add(new SqlParameter
                        {
                            ParameterName = "@TableID",
                            Direction     = ParameterDirection.Output,
                            DbType        = DbType.Int32
                        });
                        command.ExecuteNonQuery();
                        AudienceTableID = Convert.ToInt32(command.Parameters["@TableID"].Value);
                        CurrentPlace.ID = AudienceTableID;
                    }

                    //new SqlCommand($"Update AudienceTable Set Name=N'{CurrentPlace.Name}', Description=N'{CurrentPlace.Description}' where ID={CurrentPlace.ID}",
                    //    connection).ExecuteNonQuery();
                    var count = CurrentPlace.TypeDeviceRemovedCollection.Count;
                    for (var i = 0; i < count; i++)
                    {
                        var temp = CurrentPlace.TypeDeviceRemovedCollection[i];
                        if (temp.Row != null)
                        {
                            var obj = new SqlCommand(
                                $"Select TOP(1) PlaceID from {temp.Row["TableName"]} Where ID={temp.Row["ID"]}",
                                connection)
                                      .ExecuteScalar();
                            var id = Convert.ToInt32(!(obj is DBNull) ? obj : 0);
                            if (id == temp.PlaceID)
                            {
                                new SqlCommand($"Update {temp.Row["TableName"]} set PlaceID=null Where ID={temp.Row["ID"]}",
                                               connection).ExecuteNonQuery();
                            }
                        }

                        if (temp.PlaceID != 0)
                        {
                            if (temp.IsRemoved)
                            {
                                new SqlCommand($"Delete from Place Where ID={temp.PlaceID}", connection).ExecuteNonQuery();
                            }
                        }
                    }

                    count = CurrentPlace.TypeDeviceCollection.Count;
                    for (var i = 0; i < count; i++)
                    {
                        var temp = CurrentPlace.TypeDeviceCollection[i];
                        if (temp.PlaceID != 0)
                        {
                            new SqlCommand(
                                $"Update Place set TypeDeviceID=dbo.GetTypeDeviceID(N'{temp.TypeDevice.Type}') Where ID={temp.PlaceID}",
                                connection).ExecuteNonQuery();
                            if (temp.Row != null) // устройство
                            {
                                new SqlCommand(
                                    $"Update {temp.Row["TableName"]} set PlaceID={temp.PlaceID} Where ID={temp.Row["ID"]}",
                                    connection).ExecuteNonQuery();
                            }
                        }
                        else if (!(temp.TypeDevice is null))
                        {
                            var command = new SqlCommand("AddLocation", connection)
                            {
                                CommandType = CommandType.StoredProcedure
                            };
                            command.Parameters.Add(new SqlParameter("@AudienceTableID", AudienceTableID));
                            command.Parameters.Add(new SqlParameter("@TypeDeviceID",
                                                                    Convert.ToInt32(new SqlCommand($"Select dbo.GetTypeDeviceID(N'{temp.TypeDevice.Type}')",
                                                                                                   connection).ExecuteScalar())));
                            var parameter = new SqlParameter
                            {
                                ParameterName = "@PlaceID",
                                Direction     = ParameterDirection.Output,
                                DbType        = DbType.Int32
                            };
                            command.Parameters.Add(parameter);
                            command.ExecuteNonQuery();
                            var PID = Convert.ToInt32(command.Parameters["@PlaceID"].Value);
                            if (temp.Row != null) // устройство
                            {
                                new SqlCommand(
                                    $"Update {temp.Row["TableName"]} set PlaceID={PID} Where ID={temp.Row["ID"]}",
                                    connection).ExecuteNonQuery();
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, exception.GetType().Name, MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                }
            }

            Accounting.IsHitTestVisible = true;
            Accounting.ChangeLocationView();
            Close();
        }