Advertisement
Guest User

Login/Registration Page

a guest
Mar 26th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Media;
  11. using System.Data.SqlClient;
  12.  
  13. namespace AlexandriasBibliothek
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         private bool check_name = false;
  18.         private bool check_nachname = false;
  19.         private bool check_user_id = false;
  20.         private bool check_password = false;
  21.         private bool check_birthday = false;
  22.  
  23.         public Form1()
  24.         {
  25.             InitializeComponent();
  26.  
  27.             SoundPlayer StartUp = new SoundPlayer(@"C:\Alexandria\Sound\320664_pizzaiolo_lovelyboot1.wav");
  28.             StartUp.Play();
  29.  
  30.             textBox2.Enabled = false;
  31.  
  32.             textBox3.ForeColor = System.Drawing.Color.Gray;
  33.             textBox4.ForeColor = System.Drawing.Color.Gray;
  34.             textBox5.ForeColor = System.Drawing.Color.Gray;
  35.             textBox6.ForeColor = System.Drawing.Color.Gray;
  36.             textBox7.ForeColor = System.Drawing.Color.Gray;
  37.             textBox8.ForeColor = System.Drawing.Color.Gray;
  38.             textBox9.ForeColor = System.Drawing.Color.Gray;
  39.             textBox10.ForeColor = System.Drawing.Color.Gray;
  40.  
  41.             dateTimePicker1.CalendarForeColor = System.Drawing.Color.Gray;
  42.  
  43.             textBox2.UseSystemPasswordChar = true;
  44.  
  45.             checkBox1.Enabled = false;
  46.             checkBox2.Enabled = false;
  47.             checkBox3.Enabled = false;
  48.             checkBox4.Enabled = false;
  49.             checkBox5.Enabled = false;
  50.             checkBox6.Enabled = false;
  51.             checkBox7.Enabled = false;
  52.             checkBox8.Enabled = false;
  53.             checkBox9.Enabled = false;
  54.         }
  55.  
  56.         private void Form1_Load(object sender, EventArgs e)
  57.         {
  58.  
  59.         }
  60.  
  61.         private void textBox1_TextChanged(object sender, EventArgs e)
  62.         {
  63.             if (textBox1.Text != "") { textBox2.Enabled = true; checkBox5.Enabled = true; }
  64.             else { textBox2.Enabled = false; checkBox5.Enabled = false; }
  65.         }
  66.  
  67.         private void button1_Click(object sender, EventArgs e)
  68.         {
  69.             try
  70.             {
  71.                 if (textBox1.Text != "" && textBox2.Text != "")
  72.                 {
  73.                     using (SqlConnection conn = new SqlConnection())
  74.                     {
  75.                         conn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Alexandria\Database\Database1.mdf;Integrated Security=True;";
  76.                         conn.Open();            //Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Alexandria\Database\Database1.mdf;Integrated Security=True
  77.  
  78.                         try
  79.                         {
  80.  
  81.                             SqlCommand SelectCommand = new SqlCommand("SELECT count(user_id) FROM USERS WHERE @0 = user_id and @1 = user_password;", conn);
  82.  
  83.                             SelectCommand.Parameters.Add(new SqlParameter("@0", textBox1.Text));
  84.                             SelectCommand.Parameters.Add(new SqlParameter("@1", textBox2.Text));
  85.  
  86.                             using (SqlDataReader reader = SelectCommand.ExecuteReader())
  87.                             {
  88.                                 reader.Read();
  89.                                 if (Convert.ToString(reader[0]) == "1")
  90.                                 {
  91.                                     MessageBox.Show("Welcome, " + textBox1.Text);
  92.  
  93.                                     /*Form2 scd_screen = new Form2();
  94.                                     scd_screen.Location = this.Location;
  95.                                     scd_screen.Size = this.Size;
  96.                                     scd_screen.Show();
  97.                                     this.Hide();*/
  98.                                 }
  99.                                 else { MessageBox.Show("Wrong UserID or password!"); }
  100.                             }
  101.  
  102.                         }
  103.                         catch (SqlException)
  104.                         {
  105.                             MessageBox.Show("Wrong UserID or password!2");
  106.                         }
  107.                     }
  108.                 }
  109.                 else { MessageBox.Show("Wrong UserID or password!"); }
  110.             }
  111.             catch { MessageBox.Show("Wrong UserID or password!"); }
  112.         }
  113.  
  114.         private void button2_Click(object sender, EventArgs e)
  115.         {
  116.             if (check_name == true && check_nachname == true && check_user_id == true && check_password == true && check_birthday)
  117.             {
  118.                 int plz = 0;
  119.                 int.TryParse(textBox6.Text, out plz);
  120.  
  121.                 using (SqlConnection conn = new SqlConnection())
  122.                 {
  123.                     try
  124.                     {
  125.                         conn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Alexandria\Database\Database1.mdf;Integrated Security=True;";
  126.                         conn.Open();            //Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Alexandria\Database\Database1.mdf;Integrated Security=True
  127.  
  128.                         SqlCommand insertCommand = new SqlCommand("Insert into USERS values(@a,@b,@c,@d,@e,@f,@g,@h,@i);", conn);
  129.  
  130.                         insertCommand.Parameters.Add(new SqlParameter("@a", textBox5.Text));
  131.                         insertCommand.Parameters.Add(new SqlParameter("@b", textBox4.Text));
  132.                         insertCommand.Parameters.Add(new SqlParameter("@c", textBox3.Text));
  133.                         insertCommand.Parameters.Add(new SqlParameter("@d", dateTimePicker1.Text));
  134.                         insertCommand.Parameters.Add(new SqlParameter("@e", textBox10.Text));
  135.                         insertCommand.Parameters.Add(new SqlParameter("@f", textBox7.Text));
  136.                         insertCommand.Parameters.Add(new SqlParameter("@g", plz));
  137.                         insertCommand.Parameters.Add(new SqlParameter("@h", textBox9.Text));
  138.                         insertCommand.Parameters.Add(new SqlParameter("@i", textBox8.Text));
  139.  
  140.                         if (insertCommand.ExecuteNonQuery() == 1)
  141.                         {
  142.                             MessageBox.Show("Welcome, " + textBox3.Text + " " + textBox4.Text);
  143.  
  144.                             /*Form2 scd_screen = new Form2();
  145.                             scd_screen.Location = this.Location;
  146.                             scd_screen.Size = this.Size;
  147.                             scd_screen.Show();
  148.                             this.Hide();*/
  149.                         }
  150.                     }
  151.                     catch
  152.                     {
  153.                         MessageBox.Show(textBox5.Text + " has alredy been taken. Sorry");
  154.  
  155.                         textBox5.ForeColor = System.Drawing.Color.Gray;
  156.                         textBox5.Text = "UserID";
  157.                     }
  158.                 }
  159.             }
  160.             else
  161.             {
  162.                 if (check_name != true) MessageBox.Show("Please enter a valid name");
  163.                 if (check_nachname != true) MessageBox.Show("Please enter a valid surname");
  164.                 if (check_user_id != true) MessageBox.Show("Please enter a valid UserID");
  165.                 if (check_password != true) MessageBox.Show("Please enter a valid password");
  166.                 if (check_birthday != true) MessageBox.Show("Please enter a valid birthday");
  167.             }
  168.         }
  169.  
  170.         private void checkBox5_CheckedChanged(object sender, EventArgs e)
  171.         {
  172.             if (checkBox5.Checked == true)
  173.             {
  174.                 textBox2.UseSystemPasswordChar = false;
  175.             }
  176.             else textBox2.UseSystemPasswordChar = true;
  177.         }
  178.  
  179.         private void checkBox6_CheckedChanged(object sender, EventArgs e)
  180.         {
  181.             if (textBox10.ForeColor == System.Drawing.Color.Black && checkBox6.Checked == false)
  182.             {
  183.                 textBox10.UseSystemPasswordChar = true;
  184.             }
  185.             else if (textBox10.ForeColor == System.Drawing.Color.Gray || checkBox6.Checked == true)
  186.             {
  187.                 textBox10.UseSystemPasswordChar = false;
  188.             }
  189.         }
  190.  
  191.         private void textBox3_Enter(object sender, EventArgs e)
  192.         {
  193.             if(textBox3.ForeColor == System.Drawing.Color.Gray)
  194.             {
  195.                 textBox3.ForeColor = System.Drawing.Color.Black;
  196.                 textBox3.Text = "";
  197.             }
  198.         }
  199.  
  200.         private void textBox3_Leave(object sender, EventArgs e)
  201.         {
  202.             if (textBox3.Text == "")
  203.             {
  204.                 textBox3.ForeColor = System.Drawing.Color.Gray;
  205.                 textBox3.Text = "Name";
  206.             }
  207.             else check_name = true;
  208.         }
  209.  
  210.         private void textBox4_Enter(object sender, EventArgs e)
  211.         {
  212.             if (textBox4.ForeColor == System.Drawing.Color.Gray)
  213.             {
  214.                 textBox4.ForeColor = System.Drawing.Color.Black;
  215.                 textBox4.Text = "";
  216.             }
  217.         }
  218.  
  219.         private void textBox4_Leave(object sender, EventArgs e)
  220.         {
  221.             if (textBox4.Text == "")
  222.             {
  223.                 textBox4.ForeColor = System.Drawing.Color.Gray;
  224.                 textBox4.Text = "Surname";
  225.             }
  226.             else check_nachname = true;
  227.         }
  228.  
  229.         private void textBox5_Enter(object sender, EventArgs e)
  230.         {
  231.             if (textBox5.ForeColor == System.Drawing.Color.Gray)
  232.             {
  233.                 textBox5.ForeColor = System.Drawing.Color.Black;
  234.                 textBox5.Text = "";
  235.             }
  236.         }
  237.  
  238.         private void textBox5_Leave(object sender, EventArgs e)
  239.         {
  240.             if (textBox5.Text == "")
  241.             {
  242.                 textBox5.ForeColor = System.Drawing.Color.Gray;
  243.                 textBox5.Text = "UserID";
  244.             }
  245.         }
  246.  
  247.         private void textBox6_Enter(object sender, EventArgs e)
  248.         {
  249.             if (textBox6.ForeColor == System.Drawing.Color.Gray)
  250.             {
  251.                 textBox6.ForeColor = System.Drawing.Color.Black;
  252.                 textBox6.Text = "";
  253.             }
  254.         }
  255.  
  256.         private void textBox6_Leave(object sender, EventArgs e)
  257.         {
  258.             if (textBox6.Text == "")
  259.             {
  260.                 textBox6.ForeColor = System.Drawing.Color.Gray;
  261.                 textBox6.Text = "ZIP";
  262.             }
  263.         }
  264.  
  265.         private void textBox7_Enter(object sender, EventArgs e)
  266.         {
  267.             if (textBox7.ForeColor == System.Drawing.Color.Gray)
  268.             {
  269.                 textBox7.ForeColor = System.Drawing.Color.Black;
  270.                 textBox7.Text = "";
  271.             }
  272.         }
  273.  
  274.         private void textBox7_Leave(object sender, EventArgs e)
  275.         {
  276.             if (textBox7.Text == "")
  277.             {
  278.                 textBox7.ForeColor = System.Drawing.Color.Gray;
  279.                 textBox7.Text = "Town";
  280.             }
  281.         }
  282.  
  283.         private void textBox8_Enter(object sender, EventArgs e)
  284.         {
  285.             if (textBox8.ForeColor == System.Drawing.Color.Gray)
  286.             {
  287.                 textBox8.ForeColor = System.Drawing.Color.Black;
  288.                 textBox8.Text = "";
  289.             }
  290.         }
  291.  
  292.         private void textBox8_Leave(object sender, EventArgs e)
  293.         {
  294.             if (textBox8.Text == "")
  295.             {
  296.                 textBox8.ForeColor = System.Drawing.Color.Gray;
  297.                 textBox8.Text = "Street";
  298.             }
  299.         }
  300.  
  301.         private void textBox9_Enter(object sender, EventArgs e)
  302.         {
  303.             if (textBox9.ForeColor == System.Drawing.Color.Gray)
  304.             {
  305.                 textBox9.ForeColor = System.Drawing.Color.Black;
  306.                 textBox9.Text = "";
  307.             }
  308.         }
  309.  
  310.         private void textBox9_Leave(object sender, EventArgs e)
  311.         {
  312.             if (textBox9.Text == "")
  313.             {
  314.                 textBox9.ForeColor = System.Drawing.Color.Gray;
  315.                 textBox9.Text = "Housenumber";
  316.             }
  317.         }
  318.  
  319.         private void textBox10_Enter(object sender, EventArgs e)
  320.         {
  321.             if (textBox10.ForeColor == System.Drawing.Color.Gray)
  322.             {
  323.                 textBox10.ForeColor = System.Drawing.Color.Black;
  324.                 textBox10.Text = "";
  325.             }
  326.         }
  327.  
  328.         private void textBox10_Leave(object sender, EventArgs e)
  329.         {
  330.             if (textBox10.Text == "")
  331.             {
  332.                 textBox10.ForeColor = System.Drawing.Color.Gray;
  333.                 textBox10.Text = "Password";
  334.             }
  335.         }
  336.  
  337.         private void dateTimePicker1_Enter(object sender, EventArgs e)
  338.         {
  339.  
  340.         }
  341.  
  342.         private void dateTimePicker1_Leave(object sender, EventArgs e)
  343.         {
  344.  
  345.         }
  346.  
  347.         private void textBox10_TextChanged(object sender, EventArgs e)
  348.         {
  349.             if (textBox10.Text == "")
  350.             {
  351.                 checkBox1.Checked = false;
  352.                 checkBox2.Checked = false;
  353.                 checkBox3.Checked = false;
  354.                 checkBox4.Checked = false;
  355.                 checkBox7.Checked = false;
  356.             }
  357.  
  358.             if (textBox10.ForeColor == System.Drawing.Color.Black && checkBox6.Checked == false)
  359.             {
  360.                 textBox10.UseSystemPasswordChar = true;
  361.             }
  362.             else if (textBox10.ForeColor == System.Drawing.Color.Gray || checkBox6.Checked == true)
  363.             {
  364.                 textBox10.UseSystemPasswordChar = false;
  365.             }
  366.  
  367.             if (textBox10.Text != "" && textBox10.Text != "Password")
  368.             {
  369.                 checkBox6.Enabled = true;
  370.  
  371.                 int pass_length = textBox10.Text.Length;
  372.  
  373.                 int a = 0, b = 0, c = 0, d = 0, f = 0;
  374.  
  375.                 for (int i = 0; i < pass_length; i++)
  376.                 {
  377.                     if (pass_length >= 8)
  378.                     {
  379.                         checkBox7.Checked = true;
  380.                     }
  381.                     else if (pass_length < 8) checkBox7.Checked = false;
  382.  
  383.                     if (Char.IsUpper(textBox10.Text[i]))
  384.                     {
  385.                         checkBox1.Checked = true;
  386.                         b = i;
  387.                     }
  388.                     else if(Char.IsUpper(textBox10.Text[b]) != true) checkBox1.Checked = false;
  389.  
  390.                     if (Char.IsLower(textBox10.Text[i]))
  391.                     {
  392.                         checkBox2.Checked = true;
  393.                         c = i;
  394.                     }
  395.                     else if (Char.IsLower(textBox10.Text[c]) != true) checkBox2.Checked = false;
  396.  
  397.                     if (int.TryParse(Convert.ToString(textBox10.Text[i]), out a))
  398.                     {
  399.                         checkBox3.Checked = true;
  400.                         d = i;
  401.                     }
  402.                     else if (int.TryParse(Convert.ToString(textBox10.Text[d]), out a) != true) checkBox3.Checked = false;
  403.  
  404.                     if ((textBox10.Text[i]>32) && (textBox10.Text[i]<48))
  405.                     {
  406.                         checkBox4.Checked = true;
  407.                         f = i;
  408.                     }
  409.                     else if ((textBox10.Text[f] > 32) && (textBox10.Text[f] < 48) != true) checkBox4.Checked = false;
  410.  
  411.                     if (checkBox1.Checked == true && checkBox2.Checked == true && checkBox3.Checked == true && checkBox4.Checked == true && checkBox7.Checked == true)
  412.                     {
  413.                         check_password = true;
  414.                     }
  415.                     else { check_password = false; }
  416.                 }
  417.             }
  418.             else { checkBox6.Enabled = false; }
  419.         }
  420.  
  421.         private void textBox5_TextChanged(object sender, EventArgs e)
  422.         {
  423.             int check_length = textBox5.Text.Length;
  424.  
  425.             if (check_length >= 6 && check_length <= 20)
  426.             {
  427.                 checkBox8.Checked = true;
  428.                 check_user_id = true;
  429.             }
  430.             else { checkBox8.Checked = false; check_user_id = false; }
  431.         }
  432.  
  433.         private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
  434.         {
  435.             DateTime current_time = DateTime.Now;
  436.             DateTime picked_time = Convert.ToDateTime(dateTimePicker1.Text);
  437.  
  438.             int value = DateTime.Compare(current_time, picked_time);
  439.  
  440.             if (value > 0) { checkBox9.Checked = true; check_birthday = true; }
  441.             else { checkBox9.Checked = false; check_birthday = false; }
  442.         }
  443.  
  444.         private void groupBox2_Enter(object sender, EventArgs e)
  445.         {
  446.  
  447.         }
  448.     }
  449. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement