Search code examples
c#.netwpfmvvm

Data transfer WPF


When you select a room, the RoomId is passed to another window that displays tasks based on the RoomId. I select a room, goes to another window, but no tasks are displayed + I tried to add a task, but couldn't because RoomId = 0. I checked via MessageBox, RoomId is passed when loading tasks, when initializing RoomView, but on the button RoomId = 0.

private void ExecuteShowRoomViewCommand(object obj)
{
    RoomWindow roomWindow = new RoomWindow();
    if (roomWindow.ShowDialog() == true || roomWindow.IsRoomCreated)
    {

        RoomView roomView = new RoomView(roomWindow.RoomId);
        CurrentChildView = new RoomViewModel();
        Caption = roomWindow.RoomName;
        Icon = PackIconKind.Class;

    }
}
namespace TaskManagement
{
    /// <summary>
    /// Interaction logic for RoomWindow.xaml
    /// </summary>
    public partial class RoomWindow : Window
    {
        string connectionString = "Data Source=DESKTOP-FTLJ7LQ\\MSSQLS;Initial Catalog=TaskManagement;Integrated Security=True;";
        public bool IsRoomCreated { get; private set; } = false;
        public string RoomName { get; private set; }
        public string AccessCode { get; private set; }
        public int RoomId { get; private set; }

        public RoomWindow()
        {
            InitializeComponent();
            LoadUserRooms();
        }

        private void LoadUserRooms()
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    string query = @"SELECT Room.RoomId, Room.RoomName 
                                     FROM Room
                                     INNER JOIN RoomUser ON Room.RoomId = RoomUser.RoomId
                                     WHERE RoomUser.UserId = @UserId";
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        command.Parameters.AddWithValue("@UserId", MainWindow.CurrentUserId);
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            List<Room> rooms = new List<Room>();
                            while (reader.Read())
                            {
                                rooms.Add(new Room
                                {
                                    RoomId = reader.GetInt32(0),
                                    RoomName = reader.GetString(1)
                                });
                            }
                            RoomListBox.ItemsSource = rooms;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred while loading rooms: {ex.Message}");
            }
        }

        private void RoomListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (RoomListBox.SelectedItem is Room selectedRoom)
            {
                RoomId = selectedRoom.RoomId;
                RoomName = selectedRoom.RoomName;
                MessageBox.Show($"Room selected: {RoomName}");
                MessageBox.Show($"Room selected: {RoomId}");
                MessageBox.Show($"Room selected: {MainWindow.CurrentUserId}");
                IsRoomCreated = true;
                this.Close();
            }
        }

        private void JoinRoomButton_Click(object sender, RoutedEventArgs e)
        {
            string accessCode = AccessCodeTextBox.Text.Trim();
            if (string.IsNullOrWhiteSpace(accessCode))
            {
                MessageBox.Show("Please enter the access code.");
                return;
            }

            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    string query = @"SELECT Room.RoomId, Room.RoomName 
                                     FROM Room 
                                     INNER JOIN AccessCode ON Room.AccessCodeId = AccessCode.AccessCodeId 
                                     WHERE AccessCode.AccessCode = @AccessCode AND AccessCode.ExpirationDate > GETDATE()";
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        command.Parameters.AddWithValue("@AccessCode", accessCode);
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                RoomId = reader.GetInt32(0);
                                RoomName = reader.GetString(1);
                                MessageBox.Show($"You have joined the room: {RoomName}");
                                IsRoomCreated = true;
                                // Adding record to RoomUser table
                                AddUserToRoom(RoomId, MainWindow.CurrentUserId);
                                this.Close();
                            }
                            else
                            {
                                MessageBox.Show("Invalid access code or the code has expired.");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred while joining the room: {ex.Message}");
            }
        }

        private void CreateRoomButton_Click(object sender, RoutedEventArgs e)
        {
            RoomName = RoomNameTextBox.Text.Trim(); // Ensure no extra spaces
            if (!string.IsNullOrWhiteSpace(RoomName))
            {
                AccessCode = GenerateAccessCode();

                try
                {
                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        connection.Open();

                        // Insert AccessCode into AccessCode table
                        string insertAccessCodeQuery = @"INSERT INTO AccessCode (AccessCode, DateGenerated, ExpirationDate) 
                                                         OUTPUT INSERTED.AccessCodeId
                                                         VALUES (@AccessCode, @DateGenerated, @ExpirationDate)";
                        using (SqlCommand command = new SqlCommand(insertAccessCodeQuery, connection))
                        {
                            command.Parameters.AddWithValue("@AccessCode", AccessCode);
                            command.Parameters.AddWithValue("@DateGenerated", DateTime.Now);
                            command.Parameters.AddWithValue("@ExpirationDate", DateTime.Now.AddDays(30)); // For example, the code is valid for 30 days

                            int accessCodeId = (int)command.ExecuteScalar();

                            // Insert Room into Room table
                            string insertRoomQuery = "INSERT INTO Room (RoomName, AccessCodeId) VALUES (@RoomName, @AccessCodeId)";
                            using (SqlCommand roomCommand = new SqlCommand(insertRoomQuery, connection))
                            {
                                roomCommand.Parameters.AddWithValue("@RoomName", RoomName);
                                roomCommand.Parameters.AddWithValue("@AccessCodeId", accessCodeId);
                                roomCommand.ExecuteNonQuery();
                            }
                        }
                    }
                    IsRoomCreated = true;
                    MessageBox.Show($"Room '{RoomName}' created with access code: {AccessCode}");
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"An error occurred while creating the room: {ex.Message}");
                }
            }
            else
            {
                MessageBox.Show("Please enter the room name.");
            }
        }

        private string GenerateAccessCode()
        {
            // Generate access code
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var random = new Random();
            var result = new StringBuilder(8); // Access code length is 8 characters
            for (int i = 0; i < 8; i++)
            {
                result.Append(chars[random.Next(chars.Length)]);
            }
            return result.ToString();
        }

        private void AddUserToRoom(int roomId, int userId)
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    string insertRoomUserQuery = "INSERT INTO RoomUser (RoomId, UserId, RoleId) VALUES (@RoomId, @UserId, @RoleId)";
                    using (SqlCommand command = new SqlCommand(insertRoomUserQuery, connection))
                    {
                        command.Parameters.AddWithValue("@RoomId", roomId);
                        command.Parameters.AddWithValue("@UserId", userId);
                        command.Parameters.AddWithValue("@RoleId", 1); // Role "user" (assuming user role ID is 1)
                        command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred while adding the user to the room: {ex.Message}");
            }
        }
    }
}

namespace TaskManagement
{
    /// <summary>
    /// Interaction logic for RoomView.xaml
    /// </summary>
    public partial class RoomView : UserControl
    {
        private string connectionString = "Data Source=DESKTOP-FTLJ7LQ\\MSSQLS;Initial Catalog=TaskManagement;Integrated Security=True;";

        public int RoomId { get;  set; }

        public RoomView()
        {
            InitializeComponent();
            DataContext = this;
        }

        public RoomView(int roomId) : this()
        {
            RoomId = roomId;
            List<RoomTask> taskItems = LoadData(roomId);
            dataGridTask1.ItemsSource = taskItems;
        }

        private List<RoomTask> LoadData(int roomId)
        {
            List<RoomTask> taskList = new List<RoomTask>();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string sqlQuery = @"SELECT RoomTask.TaskId, RoomTask.TaskName, RoomTask.TaskDateCreation, RoomTask.TaskDateCompletion, RoomTask.TaskPriority, RoomTask.TaskStatus, 
                                    Project.ProjectName 
                                    FROM RoomTask 
                                    INNER JOIN Project ON RoomTask.ProjectId = Project.ProjectId
                                    WHERE RoomTask.RoomId = @RoomId";
                using (SqlCommand command = new SqlCommand(sqlQuery, connection))
                {
                    command.Parameters.AddWithValue("@RoomId", roomId);
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            RoomTask taskItem = new RoomTask
                            {
                                TaskId = Convert.ToInt32(reader["TaskId"]),
                                TaskName = reader["TaskName"].ToString(),
                                TaskDateCreation = Convert.ToDateTime(reader["TaskDateCreation"]),
                                TaskDateCompletion = reader["TaskDateCompletion"] != DBNull.Value ? Convert.ToDateTime(reader["TaskDateCompletion"]) : (DateTime?)null,
                                TaskPriority = reader["TaskPriority"].ToString(),
                                TaskStatus = reader["TaskStatus"].ToString(),
                                Project = new ProjectItem { ProjectName = reader["ProjectName"].ToString() }
                            };
                            taskList.Add(taskItem);
                        }
                    }
                }
            }
            return taskList;
        }

        private void AddTaskButton_Click(object sender, RoutedEventArgs e)
        {
            if (RoomId == 0)
            {
                MessageBox.Show("Invalid room ID.");
                return;
            }
            string taskName = textBoxTaskName.Text.Trim();
            if (string.IsNullOrWhiteSpace(taskName))
            {
                MessageBox.Show("Please enter the task name.");
                return;
            }

            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    string query = @"INSERT INTO RoomTask (TaskName, TaskDateCreation, TaskPriority, TaskStatus, ProjectId, RoomId) 
                                     VALUES (@TaskName, @TaskDateCreation, @TaskPriority, @TaskStatus, @ProjectId, @RoomId)";
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        command.Parameters.AddWithValue("@TaskName", taskName);
                        command.Parameters.AddWithValue("@TaskDateCreation", DateTime.Now);
                        command.Parameters.AddWithValue("@TaskPriority", "Medium");
                        command.Parameters.AddWithValue("@TaskStatus", "In Progress");
                        command.Parameters.AddWithValue("@ProjectId", GetSelectedProjectId());
                        command.Parameters.AddWithValue("@RoomId", RoomId);

                        command.ExecuteNonQuery();
                    }
                }
                List<RoomTask> taskItems = LoadData(RoomId);
                dataGridTask1.ItemsSource = taskItems;
                textBoxTaskName.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred while adding the task: {ex.Message}");
            }
        }

        private int GetSelectedProjectId()
        {
            // Returns the ID of the selected project, here you need to implement the logic to get ProjectId
            return 1; // Example
        }

        private void textBoxTaskName_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                AddTaskButton_Click(sender, e);
            }
        }
    }
}


Solution

  • In your code

    if (roomWindow.ShowDialog() == true || roomWindow.IsRoomCreated)
    {
        // what is the case for this when you never use it?
        RoomView roomView = new RoomView(roomWindow.RoomId);
    
        CurrentChildView = new RoomViewModel();
        Caption = roomWindow.RoomName;
        Icon = PackIconKind.Class;
    }
    

    you create a new RoomView instance where you pass the RoomId and assign it to a local variable. But that instance will never be used. So you assign that RoomId technical to nothing that will be presented on the screen.


    Wild guess because you did not provide all of the relevant code:

    This CurrentChildView = new RoomViewModel(); will also create a new RoomView instance because of a DataTemplate for RoomViewModel of course calling the empty constructor.

    Set a breakpoint in the constructor of RoomView and you should notice that you deal with two created instances here.