Skip to content

Commit

Permalink
New User page
Browse files Browse the repository at this point in the history
  • Loading branch information
SKrishna791 committed Mar 19, 2023
1 parent 881ff27 commit f8be93e
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 90 deletions.
7 changes: 7 additions & 0 deletions IoT1/IoT1.csproj
Expand Up @@ -134,6 +134,9 @@
<Compile Include="Splash.xaml.cs">
<DependentUpon>Splash.xaml</DependentUpon>
</Compile>
<Compile Include="View\AddUserPage.xaml.cs">
<DependentUpon>AddUserPage.xaml</DependentUpon>
</Compile>
<Compile Include="View\Location.xaml.cs">
<DependentUpon>Location.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -169,6 +172,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="View\AddUserPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="View\HomePage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
4 changes: 3 additions & 1 deletion IoT1/Model/User.cs
Expand Up @@ -4,8 +4,10 @@ public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int AgentID { get; set; }
public int AgentId { get; set; }
public int StationNumber { get; set; }

public bool IsActiveInMission { get; set; }
public string FullName
{
get { return FirstName + " " + LastName; }
Expand Down
39 changes: 39 additions & 0 deletions IoT1/View/AddUserPage.xaml
@@ -0,0 +1,39 @@
<Page x:Class="IoT1.View.AddUserPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:IoT1.View"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="AddUserPage">

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<Label Grid.Row="0" Content="First Name:" Margin="10"/>
<TextBox Grid.Row="0" Name="FirstNameTextBox" Margin="10" Width="200"/>

<Label Grid.Row="1" Content="Last Name:" Margin="10"/>
<TextBox Grid.Row="1" Name="LastNameTextBox" Margin="10" Width="200"/>

<Label Grid.Row="2" Content="Agent ID:" Margin="10"/>
<TextBox Grid.Row="2" Name="AgentIdTextBox" Margin="10" Width="200"/>

<Label Grid.Row="3" Content="Station Number:" Margin="10"/>
<TextBox Grid.Row="3" Name="StationNumberTextBox" Margin="10" Width="200"/>

<Label Grid.Row="4" Content="Active in Mission:" Margin="10"/>
<CheckBox Grid.Row="4" Name="ActiveInMissionCheckBox" Margin="300,10,10,10" Background="Red" Checked="ActiveInMissionCheckBox_Checked" Unchecked="ActiveInMissionCheckBox_Unchecked"/>

<Button Grid.Row="5" Name="SaveButton" Content="Save" Margin="10" Width="100" Click="SaveButton_Click"/>
</Grid>
</Page>
75 changes: 75 additions & 0 deletions IoT1/View/AddUserPage.xaml.cs
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IoT1.View
{
/// <summary>
/// Interaction logic for AddUserPage.xaml
/// </summary>
public partial class AddUserPage : Page
{
public AddUserPage()
{
InitializeComponent();
}

private void SaveButton_Click(object sender, RoutedEventArgs e)
{
string firstName = FirstNameTextBox.Text;
string lastName = LastNameTextBox.Text;
int agentId = int.Parse(AgentIdTextBox.Text);
int stationNumber = int.Parse(StationNumberTextBox.Text);
bool isActiveInMission = ActiveInMissionCheckBox.IsChecked ?? false;

User newUser = new User
{
FirstName = firstName,
LastName = lastName,
AgentId = agentId,
StationNumber = stationNumber,
IsActiveInMission = isActiveInMission
};

using (SqlConnection connection = new SqlConnection(@"Data Source=(localdb)\Local; Database = LoginDB;Integrated Security = True;"))
{
SqlCommand command = new SqlCommand("INSERT INTO Userspage (FirstName, LastName, AgentId, StationNumber, IsActive) " +
"VALUES (@FirstName, @LastName, @AgentId, @StationNumber, @IsActiveInMission)", connection);
command.Parameters.AddWithValue("@FirstName", newUser.FirstName);
command.Parameters.AddWithValue("@LastName", newUser.LastName);
command.Parameters.AddWithValue("@AgentId", newUser.AgentId);
command.Parameters.AddWithValue("@StationNumber", newUser.StationNumber);
command.Parameters.AddWithValue("@IsActiveInMission", newUser.IsActiveInMission);

connection.Open();
int result = command.ExecuteNonQuery();
if (result == 1)
{
MessageBox.Show("User added successfully!");
}
}

}
private void ActiveInMissionCheckBox_Checked(object sender, RoutedEventArgs e)
{
ActiveInMissionCheckBox.Background = Brushes.Green;
}

private void ActiveInMissionCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
ActiveInMissionCheckBox.Background = Brushes.Red;
}
}
}
49 changes: 25 additions & 24 deletions IoT1/View/UserInputWindow.xaml
@@ -1,33 +1,34 @@
<Window x:Class="IoT1.UserInputWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:iot1="clr-namespace:IoT1"
Title="Add User" Height="250" Width="350">
Title="Add User" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="First Name:" />
<TextBox Grid.Row="0" Grid.Column="1" x:Name="txtFirstName" />
<Label Grid.Row="1" Grid.Column="0" Content="Last Name:" />
<TextBox Grid.Row="1" Grid.Column="1" x:Name="txtLastName" />
<Label Grid.Row="2" Grid.Column="0" Content="Agent ID:" />
<TextBox Grid.Row="2" Grid.Column="1" x:Name="txtAgentID" />
<Label Grid.Row="3" Grid.Column="0" Content="Station Number:" />
<TextBox Grid.Row="3" Grid.Column="1" x:Name="txtStationNumber" />
<Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" x:Name="btnSave" Content="Save" Click="BtnSave_Click" />
<Button Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" x:Name="btnCancel" Content="Cancel" Click="BtnCancel_Click" />

<Label Grid.Row="0" Content="First Name:" Margin="10"/>
<TextBox Grid.Row="0" Name="FirstNameTextBox" Margin="10" Width="200"/>

<Label Grid.Row="1" Content="Last Name:" Margin="10"/>
<TextBox Grid.Row="1" Name="LastNameTextBox" Margin="10" Width="200"/>

<Label Grid.Row="2" Content="Agent ID:" Margin="10"/>
<TextBox Grid.Row="2" Name="AgentIdTextBox" Margin="10" Width="200"/>

<Label Grid.Row="3" Content="Station Number:" Margin="10"/>
<TextBox Grid.Row="3" Name="StationNumberTextBox" Margin="10" Width="200"/>

<Label Grid.Row="4" Content="Active in Mission:" Margin="10"/>
<CheckBox Grid.Row="4" Name="ActiveInMissionCheckBox" Margin="300,10,10,10" Background="Red" Checked="ActiveInMissionCheckBox_Checked" Unchecked="ActiveInMissionCheckBox_Unchecked"/>

<Button Grid.Row="5" Name="SaveButton" Content="Save" Margin="10" Width="100" Click="SaveButton_Click"/>
</Grid>

</Window>
85 changes: 43 additions & 42 deletions IoT1/View/UserInputWindow.xaml.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -20,65 +21,65 @@ namespace IoT1
/// </summary>
public partial class UserInputWindow : Window
{
private readonly ObservableCollection<User> _users;
private readonly UserPage _userPage;

public UserInputWindow(ObservableCollection<User> users, UserPage userPage)
{
InitializeComponent();
_users = users;
_userPage = userPage;
this.Owner = Application.Current.MainWindow;

}



private void BtnSave_Click(object sender, RoutedEventArgs e)
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
// Validate the input fields
if (string.IsNullOrEmpty(txtFirstName.Text))
{
MessageBox.Show("Please enter a first name.");
return;
}
if (string.IsNullOrEmpty(txtLastName.Text))
{
MessageBox.Show("Please enter a last name.");
return;
}
int agentID;
if (!int.TryParse(txtAgentID.Text, out agentID))
{
MessageBox.Show("Please enter a valid agent ID.");
return;
}
int stationNumber;
if (!int.TryParse(txtStationNumber.Text, out stationNumber))
{
MessageBox.Show("Please enter a valid station number.");
return;
}
string firstName = FirstNameTextBox.Text;
string lastName = LastNameTextBox.Text;
int agentId = int.Parse(AgentIdTextBox.Text);
int stationNumber = int.Parse(StationNumberTextBox.Text);
bool isActiveInMission = ActiveInMissionCheckBox.IsChecked ?? false;

// Create a new instance of the User class
var user = new User
User newUser = new User
{
FirstName = txtFirstName.Text,
LastName = txtLastName.Text,
AgentID = agentID,
StationNumber = stationNumber
FirstName = firstName,
LastName = lastName,
AgentId = agentId,
StationNumber = stationNumber,
IsActiveInMission = isActiveInMission
};

_userPage.AddUser(user);

// Close the dialog box
Close();
}
using (SqlConnection connection = new SqlConnection(@"Data Source=(localdb)\Local; Database = LoginDB;Integrated Security = True;"))
{
SqlCommand command = new SqlCommand("INSERT INTO Userspage (FirstName, LastName, AgentId, StationNumber, IsActive) " +
"VALUES (@FirstName, @LastName, @AgentId, @StationNumber, @IsActiveInMission)", connection);
command.Parameters.AddWithValue("@FirstName", newUser.FirstName);
command.Parameters.AddWithValue("@LastName", newUser.LastName);
command.Parameters.AddWithValue("@AgentId", newUser.AgentId);
command.Parameters.AddWithValue("@StationNumber", newUser.StationNumber);
command.Parameters.AddWithValue("@IsActiveInMission", newUser.IsActiveInMission);

connection.Open();
int result = command.ExecuteNonQuery();
if (result == 1)
{
MessageBox.Show("User added successfully!");
}
}

}
private void ActiveInMissionCheckBox_Checked(object sender, RoutedEventArgs e)
{
ActiveInMissionCheckBox.Background = Brushes.Green;
}

private void BtnCancel_Click(object sender, RoutedEventArgs e)
private void ActiveInMissionCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
Close();
ActiveInMissionCheckBox.Background = Brushes.Red;
}





}


Expand Down
60 changes: 47 additions & 13 deletions IoT1/View/UserPage.xaml
Expand Up @@ -8,22 +8,56 @@
d:DesignHeight="450" d:DesignWidth="800"
Title="UserPage">

<Page.Resources>
<Style TargetType="Button" x:Key="AddUserButtonStyle">
<Setter Property="Background" Value="#2196F3"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="Margin" Value="0,10,0,0"/>
<Setter Property="Padding" Value="10,5"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Width" Value="150"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
CornerRadius="5"
Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" x:Name="btnAddUser" Content="Add User" Click="btnAddUser_Click"/>

<ListView Grid.Row="1" ItemsSource="{Binding Users}" Background="Transparent">
<ListView.View>
<GridView>
<GridViewColumn Header="Agent ID" DisplayMemberBinding="{Binding AgentID}" Width="100"/>
<GridViewColumn Header="Full Name" DisplayMemberBinding="{Binding FullName}" Width="200"/>
<GridViewColumn Header="Station #" DisplayMemberBinding="{Binding StationNumber}" Width="100"/>
</GridView>
</ListView.View>
</ListView>
<DataGrid x:Name="UserGrid" Grid.Row="1" ItemsSource="{Binding Users}" AutoGenerateColumns="False" Margin="26,10,10,74" SelectionChanged="UserGrid_SelectionChanged"
BorderBrush="#B9B9B9" BorderThickness="1" AlternatingRowBackground="#F6F6F6" RowBackground="#FFFFFF" RowHeight="35"
HorizontalGridLinesBrush="#E6E6E6" VerticalGridLinesBrush="#E6E6E6" CanUserSortColumns="True" CanUserResizeColumns="True" CanUserResizeRows="False"
IsReadOnly="True" FontSize="14">
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" Width="*" />
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" Width="*" />
<DataGridTextColumn Header="Agent ID" Binding="{Binding AgentId}" Width="*" />
<DataGridTextColumn Header="Station Number" Binding="{Binding StationNumber}" Width="*" />
<DataGridCheckBoxColumn Header="Active in Mission" Binding="{Binding IsActiveInMission}" Width="*" />
</DataGrid.Columns>
</DataGrid>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Bottom">
<Button Content="Add User" Click="Button_Click" Style="{StaticResource AddUserButtonStyle}" />
</StackPanel>
</Grid>

</Page>

0 comments on commit f8be93e

Please sign in to comment.