Advertisement
Guest User

frmUsers

a guest
Jun 19th, 2023
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.25 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using CarDealership.GroupFinal266DataSetTableAdapters;
  11. using CarLibrary;
  12.  
  13. namespace CarDealership
  14. {
  15.     public partial class frmUsers : Form, IUser, IUtility
  16.     {
  17.         User user = new User() { userID = -1 };
  18.         UserDB userDB = new UserDB();
  19.  
  20.         int startRecord = 0;
  21.  
  22.         public frmUsers()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void buyersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
  28.         {
  29.             this.Validate();
  30.             this.buyersBindingSource.EndEdit();
  31.             this.tableAdapterManager.UpdateAll(this.groupFinal266DataSet);
  32.         }
  33.  
  34.         private void Users_Load(object sender, EventArgs e)
  35.         {
  36.             PaginationSetUp(Program.sqlConnection);
  37.  
  38.             // TODO: This line of code loads data into the 'groupFinal266DataSet.Buyers' table. You can move, or remove it, as needed.
  39.             this.buyersTableAdapter.Fill(this.groupFinal266DataSet.Buyers);
  40.  
  41.             LoadSampleUsers();
  42.         }
  43.  
  44.         private void LoadSampleUsers()
  45.         {
  46.             txtRegisterEmail.Text = "zzm4h94sr1a@icznn.com";
  47.             txtRegisterPassword.Text = "u3AeOX ^ 686 & h";
  48.             txtRegisterFirstName.Text = "Nanna";
  49.             txtRegisterLastName.Text = "Maric";
  50.  
  51.             txtSellerEmailLogin.Text = "zzm4h94sr1a@icznn.com";
  52.             txtLoginPassword.Text = "u3AeOX ^ 686 & h";
  53.         }
  54.  
  55.         private bool PaginationSetUp(SqlConnection sqlConnection)
  56.         {
  57.             try
  58.             {
  59.                 sellersTableAdapter.Adapter.SelectCommand = new SqlCommand("SELECT * FROM Sellers", sqlConnection);
  60.                 sqlConnection.Open();
  61.  
  62.                 sellersTableAdapter.Adapter.Fill(this.groupFinal266DataSet, startRecord, 5, "PaginatedSellersDataTable");
  63.  
  64.                 sellersDataGridView.DataSource = this.groupFinal266DataSet;
  65.                 sellersDataGridView.DataMember = "PaginatedSellersDataTable";
  66.             }
  67.             catch (Exception ex)
  68.             {
  69.                 MessageBox.Show(ex.Message, ex.GetType().ToString());
  70.                 return false;
  71.             }
  72.             finally
  73.             {
  74.                 sqlConnection.Close();
  75.             }
  76.             return true;
  77.         }
  78.  
  79.         private void btnPreviousPage_Click(object sender, EventArgs e)
  80.         {
  81.             startRecord -= 5;
  82.  
  83.             if (startRecord <= 0)
  84.                 startRecord = 0;
  85.  
  86.             this.groupFinal266DataSet.Clear();
  87.             sellersTableAdapter.Adapter.Fill(this.groupFinal266DataSet, startRecord, 5, "PaginatedSellersDataTable");
  88.         }
  89.  
  90.         private void btnNext_Click(object sender, EventArgs e)
  91.         {
  92.             int count = 0;
  93.             SqlConnection sqlConnection = Program.sqlConnection;
  94.  
  95.             SqlCommand cmdCount = new SqlCommand("SELECT COUNT(*) FROM [GroupFinal266].[dbo].[Sellers]", sqlConnection);
  96.             sqlConnection.Open();
  97.             count = Convert.ToInt32(cmdCount.ExecuteScalar());
  98.  
  99.             sqlConnection.Close();
  100.  
  101.             startRecord += 5;
  102.            
  103.             if (startRecord > count) {
  104.                 startRecord -= 5;
  105.             }
  106.  
  107.             this.groupFinal266DataSet.Clear();
  108.             sellersTableAdapter.Adapter.Fill(this.groupFinal266DataSet, startRecord, 5, "PaginatedSellersDataTable");
  109.         }
  110.  
  111.         public void AssignBusinessObjectDataToUpload()
  112.         {
  113.             user.email = txtRegisterEmail.Text;
  114.             user.password = txtRegisterPassword.Text;
  115.             user.firstName = txtRegisterFirstName.Text;
  116.             user.lastName = txtRegisterLastName.Text;
  117.         }
  118.  
  119.         public bool ValidateBusinessObjectData()
  120.         {
  121.             try
  122.             {
  123.                 // Loop through all the properties to make sure none of them are empty.
  124.                 //https://www.w3schools.blog/loop-over-object-properties-c
  125.                 // If one is empty then throw an error and return false.
  126.                 foreach (PropertyInfo property in user.GetType().GetProperties())
  127.                     if (property.GetValue(user) == null || string.IsNullOrEmpty(property.GetValue(user).ToString()))
  128.                         throw new ArgumentNullException(property.Name, char.ToUpper(property.Name[0]) + property.Name.Substring(1) + " not found");
  129.  
  130.                 return true;
  131.             }
  132.             catch (Exception ex)
  133.             {
  134.                 MessageBox.Show(ex.Message, ex.GetType().ToString());
  135.             }
  136.  
  137.             return false;
  138.         }
  139.  
  140.         private void btnRegister_Click(object sender, EventArgs e)
  141.         {
  142.             AssignBusinessObjectDataToUpload();
  143.  
  144.             if (!ValidateBusinessObjectData())
  145.                 return;
  146.  
  147.             if (!userDB.Upload(user, Program.sqlConnection)) {
  148.                 MessageBox.Show(userDB.MsgText, userDB.MsgCaption);
  149.                 return;
  150.             }
  151.  
  152.             foreach (TextBox textBox in registerGroupBox.Controls.OfType<TextBox>())
  153.                 textBox.Clear();
  154.  
  155.             sellersDataGridView.Update();
  156.             sellersDataGridView.Refresh();
  157.         }
  158.  
  159.         private void btnLogin_Click(object sender, EventArgs e)
  160.         {
  161.             UserAuthentication();
  162.         }
  163.  
  164.         public void UserAuthentication()
  165.         {
  166.             // Put in validation here to see if it exists in the SQL database
  167.             try
  168.             {
  169.                 if (string.IsNullOrEmpty(txtSellerEmailLogin.Text))
  170.                     throw new ArgumentException("Please input a username", "Username not found");
  171.  
  172.                 if (string.IsNullOrEmpty(txtLoginPassword.Text))
  173.                     throw new ArgumentException("Please input a password", "Password not found");
  174.  
  175.                 user.email = txtSellerEmailLogin.Text;
  176.                 user.password = txtLoginPassword.Text;
  177.  
  178.                 // Because hashes are deterministic, two same passwords will always share the same hash
  179.                 if (!userDB.VerifyLoginUser(user, Program.sqlConnection))
  180.                     MessageBox.Show(userDB.MsgText, userDB.MsgCaption);
  181.  
  182.                 EnterFormCarsForSale();
  183.             }
  184.             catch (Exception ex)
  185.             {
  186.                 MessageBox.Show(ex.Message, ex.GetType().ToString());
  187.             }
  188.         }
  189.  
  190.         public void EnableControls(bool enable = true)
  191.         {
  192.             // Why is it attempting to convert here?
  193.             foreach (GroupBox groupBox in Controls.OfType<GroupBox>())
  194.                 foreach (Control c in groupBox.Controls)
  195.                 {
  196.                     if (c is Button)
  197.                         c.Enabled = enable;
  198.  
  199.                     if (c is TextBox)
  200.                         c.Enabled = enable;
  201.                 }
  202.  
  203.             this.sellersDataGridView.Columns["DeleteUserAccount"].Visible = enable;
  204.         }
  205.  
  206.         private void EnterFormCarsForSale()
  207.         {
  208.             // Check if int is null or empty
  209.             if (string.IsNullOrEmpty(user.userID.ToString()) || user.userID <= 0)
  210.                 return;
  211.  
  212.             frmCarsForSale carsForSale = new frmCarsForSale(user.userID, this);
  213.             carsForSale.Show();
  214.  
  215.             EnableControls(false);
  216.         }
  217.  
  218.         private void btnShowPassword_Click(object sender, EventArgs e)
  219.         {
  220.             if (txtRegisterPassword.UseSystemPasswordChar || txtLoginPassword.UseSystemPasswordChar)
  221.             {
  222.                 txtRegisterPassword.UseSystemPasswordChar = false;
  223.                 txtLoginPassword.UseSystemPasswordChar = false;
  224.             }
  225.         }
  226.  
  227.         private void btnHidePassword_Click(object sender, EventArgs e)
  228.         {
  229.             if (!txtRegisterPassword.UseSystemPasswordChar || !txtLoginPassword.UseSystemPasswordChar)
  230.             {
  231.                 txtRegisterPassword.UseSystemPasswordChar = true;
  232.                 txtLoginPassword.UseSystemPasswordChar = true;
  233.             }
  234.         }
  235.  
  236.         public void AssignBusinessObjectDataToDelete(int rowIndex)
  237.         {
  238.             user.userID = Convert.ToInt32(sellersDataGridView.Rows[rowIndex].Cells[0].Value);
  239.             user.firstName = Convert.ToString(sellersDataGridView.Rows[rowIndex].Cells[1].Value);
  240.             user.lastName = Convert.ToString(sellersDataGridView.Rows[rowIndex].Cells[2].Value);
  241.             user.email = Convert.ToString(sellersDataGridView.Rows[rowIndex].Cells[3].Value);
  242.             user.password = Convert.ToString(sellersDataGridView.Rows[rowIndex].Cells[4].Value);
  243.         }
  244.  
  245.         private void sellersDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
  246.         {
  247.             if (e.ColumnIndex == 7)
  248.             {
  249.                 AssignBusinessObjectDataToDelete(e.RowIndex);
  250.  
  251.                 if (!ValidateBusinessObjectData())
  252.                     return;
  253.  
  254.                 if (!userDB.Delete(user, Program.sqlConnection)) {
  255.                     MessageBox.Show(userDB.MsgText, userDB.MsgCaption);
  256.                     return;
  257.                 }
  258.  
  259.                 sellersDataGridView.Rows.RemoveAt(e.RowIndex);
  260.             }
  261.         }
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement