Kyaria

Login/Registration Page V1.1

Mar 29th, 2017
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 21.09 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;
  10. using System.Windows.Forms;
  11. using System.Media;
  12. using System.Data.SqlClient;
  13.  
  14. namespace AlexandriasBibliothek
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         private bool check_name = false;
  19.         private bool check_nachname = false;
  20.         private bool check_user_id = false;
  21.         private bool check_password = false;
  22.         private bool check_birthday = false;
  23.         private bool check_pure_intentions = false;
  24.  
  25.         public Form1()
  26.         {
  27.             InitializeComponent();
  28.  
  29.             SoundPlayer StartUp = new SoundPlayer(@"C:\Alexandria\Sound\320664_pizzaiolo_lovelyboot1.wav");
  30.             StartUp.Play();
  31.  
  32.             textBox2.Enabled = false;
  33.  
  34.             textBox3.ForeColor = System.Drawing.Color.Gray;
  35.             textBox4.ForeColor = System.Drawing.Color.Gray;
  36.             textBox5.ForeColor = System.Drawing.Color.Gray;
  37.             textBox6.ForeColor = System.Drawing.Color.Gray;
  38.             textBox7.ForeColor = System.Drawing.Color.Gray;
  39.             textBox8.ForeColor = System.Drawing.Color.Gray;
  40.             textBox9.ForeColor = System.Drawing.Color.Gray;
  41.             textBox10.ForeColor = System.Drawing.Color.Gray;
  42.  
  43.             dateTimePicker1.CalendarForeColor = System.Drawing.Color.Gray;
  44.  
  45.             textBox2.UseSystemPasswordChar = true;
  46.             textBox11.UseSystemPasswordChar = true;
  47.  
  48.             checkBox1.Enabled = false;
  49.             checkBox2.Enabled = false;
  50.             checkBox3.Enabled = false;
  51.             checkBox4.Enabled = false;
  52.             checkBox5.Enabled = false;
  53.             checkBox6.Enabled = false;
  54.             checkBox7.Enabled = false;
  55.             checkBox8.Enabled = false;
  56.             checkBox9.Enabled = false;
  57.  
  58.             radioButton1.Checked = true;
  59.         }
  60.  
  61.         private void Form1_Load(object sender, EventArgs e)
  62.         {
  63.  
  64.         }
  65.  
  66.         private void textBox1_TextChanged(object sender, EventArgs e)
  67.         {
  68.             if (textBox1.Text != "") { textBox2.Enabled = true; checkBox5.Enabled = true; }
  69.             else { textBox2.Enabled = false; checkBox5.Enabled = false; }
  70.         }
  71.  
  72.         private void button1_Click(object sender, EventArgs e)
  73.         {
  74.             try
  75.             {
  76.                 if (textBox1.Text != "" && textBox2.Text != "")
  77.                 {
  78.                     using (SqlConnection conn = new SqlConnection())
  79.                     {
  80.                         conn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Alexandria\Database\Database1.mdf;Integrated Security=True;";
  81.                         conn.Open();            //Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Alexandria\Database\Database1.mdf;Integrated Security=True
  82.  
  83.                         try
  84.                         {
  85.  
  86.                             SqlCommand SelectCommand = new SqlCommand("SELECT count(user_id) FROM USERS WHERE @0 = user_id and @1 = user_password;", conn);
  87.  
  88.                             SelectCommand.Parameters.Add(new SqlParameter("@0", textBox1.Text));
  89.                             SelectCommand.Parameters.Add(new SqlParameter("@1", textBox2.Text));
  90.  
  91.                             using (SqlDataReader reader = SelectCommand.ExecuteReader())
  92.                             {
  93.                                 reader.Read();
  94.                                 if (Convert.ToString(reader[0]) == "1")
  95.                                 {
  96.                                     MessageBox.Show("Welcome, " + textBox1.Text);
  97.  
  98.                                     Form2 scd_screen = new Form2();
  99.                                     scd_screen.Location = this.Location;
  100.                                     scd_screen.Size = this.Size;
  101.                                     scd_screen.Show();
  102.                                     this.Hide();
  103.                                 }
  104.                                 else { MessageBox.Show("Wrong UserID or password!"); }
  105.                             }
  106.  
  107.                         }
  108.                         catch (SqlException)
  109.                         {
  110.                             MessageBox.Show("Wrong UserID or password!2");
  111.                         }
  112.                     }
  113.                 }
  114.                 else { MessageBox.Show("Wrong UserID or password!"); }
  115.             }
  116.             catch { MessageBox.Show("Wrong UserID or password!"); }
  117.         }
  118.  
  119.         private void button2_Click(object sender, EventArgs e)
  120.         {
  121.             if (check_name == true && check_nachname == true && check_user_id == true && check_password == true && check_birthday)
  122.             {
  123.                 int plz = 0;
  124.                 int.TryParse(textBox6.Text, out plz);
  125.  
  126.                 using (SqlConnection conn = new SqlConnection())
  127.                 {
  128.                     try
  129.                     {
  130.                         conn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Alexandria\Database\Database1.mdf;Integrated Security=True;";
  131.                         conn.Open();            //Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Alexandria\Database\Database1.mdf;Integrated Security=True
  132.  
  133.                         SqlCommand insertCommand = new SqlCommand("Insert into USERS values(@a,@b,@c,@d,@e,@f,@g,@h,@i);", conn);
  134.  
  135.                         insertCommand.Parameters.Add(new SqlParameter("@a", textBox5.Text));
  136.                         insertCommand.Parameters.Add(new SqlParameter("@b", textBox4.Text));
  137.                         insertCommand.Parameters.Add(new SqlParameter("@c", textBox3.Text));
  138.                         insertCommand.Parameters.Add(new SqlParameter("@d", dateTimePicker1.Text));
  139.                         insertCommand.Parameters.Add(new SqlParameter("@e", textBox10.Text));
  140.                         insertCommand.Parameters.Add(new SqlParameter("@f", textBox7.Text));
  141.                         insertCommand.Parameters.Add(new SqlParameter("@g", plz));
  142.                         insertCommand.Parameters.Add(new SqlParameter("@h", textBox9.Text));
  143.                         insertCommand.Parameters.Add(new SqlParameter("@i", textBox8.Text));
  144.  
  145.                         if (insertCommand.ExecuteNonQuery() == 1)
  146.                         {
  147.                             MessageBox.Show("Willkommen " + textBox3.Text + " " + textBox4.Text + Environment.NewLine + "Du kannst dich nun einloggen und alles Spaßige tun, was man mit einer Datenbank so tun kann!");
  148.  
  149.                             textBox3.Text = "Name";
  150.                             textBox3.ForeColor = System.Drawing.Color.Gray;
  151.  
  152.                             textBox4.Text = "Surname";
  153.                             textBox4.ForeColor = System.Drawing.Color.Gray;
  154.  
  155.                             textBox5.Text = "UserID";
  156.                             textBox5.ForeColor = System.Drawing.Color.Gray;
  157.  
  158.                             textBox6.Text = "ZIP";
  159.                             textBox6.ForeColor = System.Drawing.Color.Gray;
  160.  
  161.                             textBox7.Text = "Town";
  162.                             textBox7.ForeColor = System.Drawing.Color.Gray;
  163.  
  164.                             textBox8.Text = "Street";
  165.                             textBox8.ForeColor = System.Drawing.Color.Gray;
  166.  
  167.                             textBox9.Text = "Housenumber";
  168.                             textBox9.ForeColor = System.Drawing.Color.Gray;
  169.  
  170.                             textBox10.Text = "Password";
  171.                             textBox10.ForeColor = System.Drawing.Color.Gray;
  172.  
  173.                             dateTimePicker1.Text = Convert.ToString(System.DateTime.Now);
  174.                         }
  175.                     }
  176.                     catch
  177.                     {
  178.                         MessageBox.Show(textBox5.Text + " has alredy been taken. Sorry");
  179.  
  180.                         textBox5.ForeColor = System.Drawing.Color.Gray;
  181.                         textBox5.Text = "UserID";
  182.                     }
  183.                 }
  184.             }
  185.             else
  186.             {
  187.                 if (check_name != true) MessageBox.Show("Please enter a valid name");
  188.                 if (check_nachname != true) MessageBox.Show("Please enter a valid surname");
  189.                 if (check_user_id != true) MessageBox.Show("Please enter a valid UserID");
  190.                 if (check_password != true) MessageBox.Show("Please enter a valid password");
  191.                 if (check_birthday != true) MessageBox.Show("Please enter a valid birthday");
  192.             }
  193.         }
  194.  
  195.         private void checkBox5_CheckedChanged(object sender, EventArgs e)
  196.         {
  197.             if (checkBox5.Checked == true)
  198.             {
  199.                 textBox2.UseSystemPasswordChar = false;
  200.             }
  201.             else textBox2.UseSystemPasswordChar = true;
  202.         }
  203.  
  204.         private void checkBox6_CheckedChanged(object sender, EventArgs e)
  205.         {
  206.             if (textBox10.ForeColor == System.Drawing.Color.Black && checkBox6.Checked == false)
  207.             {
  208.                 textBox10.UseSystemPasswordChar = true;
  209.             }
  210.             else if (textBox10.ForeColor == System.Drawing.Color.Gray || checkBox6.Checked == true)
  211.             {
  212.                 textBox10.UseSystemPasswordChar = false;
  213.             }
  214.         }
  215.  
  216.         private void textBox3_Enter(object sender, EventArgs e)
  217.         {
  218.             if(textBox3.ForeColor == System.Drawing.Color.Gray)
  219.             {
  220.                 textBox3.ForeColor = System.Drawing.Color.Black;
  221.                 textBox3.Text = "";
  222.             }
  223.         }
  224.  
  225.         private void textBox3_Leave(object sender, EventArgs e)
  226.         {
  227.             if (textBox3.Text == "")
  228.             {
  229.                 textBox3.ForeColor = System.Drawing.Color.Gray;
  230.                 textBox3.Text = "Name";
  231.             }
  232.             else check_name = true;
  233.         }
  234.  
  235.         private void textBox4_Enter(object sender, EventArgs e)
  236.         {
  237.             if (textBox4.ForeColor == System.Drawing.Color.Gray)
  238.             {
  239.                 textBox4.ForeColor = System.Drawing.Color.Black;
  240.                 textBox4.Text = "";
  241.             }
  242.         }
  243.  
  244.         private void textBox4_Leave(object sender, EventArgs e)
  245.         {
  246.             if (textBox4.Text == "")
  247.             {
  248.                 textBox4.ForeColor = System.Drawing.Color.Gray;
  249.                 textBox4.Text = "Surname";
  250.             }
  251.             else check_nachname = true;
  252.         }
  253.  
  254.         private void textBox5_Enter(object sender, EventArgs e)
  255.         {
  256.             if (textBox5.ForeColor == System.Drawing.Color.Gray)
  257.             {
  258.                 textBox5.ForeColor = System.Drawing.Color.Black;
  259.                 textBox5.Text = "";
  260.             }
  261.         }
  262.  
  263.         private void textBox5_Leave(object sender, EventArgs e)
  264.         {
  265.             if (textBox5.Text == "")
  266.             {
  267.                 textBox5.ForeColor = System.Drawing.Color.Gray;
  268.                 textBox5.Text = "UserID";
  269.             }
  270.         }
  271.  
  272.         private void textBox6_Enter(object sender, EventArgs e)
  273.         {
  274.             if (textBox6.ForeColor == System.Drawing.Color.Gray)
  275.             {
  276.                 textBox6.ForeColor = System.Drawing.Color.Black;
  277.                 textBox6.Text = "";
  278.             }
  279.         }
  280.  
  281.         private void textBox6_Leave(object sender, EventArgs e)
  282.         {
  283.             if (textBox6.Text == "")
  284.             {
  285.                 textBox6.ForeColor = System.Drawing.Color.Gray;
  286.                 textBox6.Text = "ZIP";
  287.             }
  288.         }
  289.  
  290.         private void textBox7_Enter(object sender, EventArgs e)
  291.         {
  292.             if (textBox7.ForeColor == System.Drawing.Color.Gray)
  293.             {
  294.                 textBox7.ForeColor = System.Drawing.Color.Black;
  295.                 textBox7.Text = "";
  296.             }
  297.         }
  298.  
  299.         private void textBox7_Leave(object sender, EventArgs e)
  300.         {
  301.             if (textBox7.Text == "")
  302.             {
  303.                 textBox7.ForeColor = System.Drawing.Color.Gray;
  304.                 textBox7.Text = "Town";
  305.             }
  306.         }
  307.  
  308.         private void textBox8_Enter(object sender, EventArgs e)
  309.         {
  310.             if (textBox8.ForeColor == System.Drawing.Color.Gray)
  311.             {
  312.                 textBox8.ForeColor = System.Drawing.Color.Black;
  313.                 textBox8.Text = "";
  314.             }
  315.         }
  316.  
  317.         private void textBox8_Leave(object sender, EventArgs e)
  318.         {
  319.             if (textBox8.Text == "")
  320.             {
  321.                 textBox8.ForeColor = System.Drawing.Color.Gray;
  322.                 textBox8.Text = "Street";
  323.             }
  324.         }
  325.  
  326.         private void textBox9_Enter(object sender, EventArgs e)
  327.         {
  328.             if (textBox9.ForeColor == System.Drawing.Color.Gray)
  329.             {
  330.                 textBox9.ForeColor = System.Drawing.Color.Black;
  331.                 textBox9.Text = "";
  332.             }
  333.         }
  334.  
  335.         private void textBox9_Leave(object sender, EventArgs e)
  336.         {
  337.             if (textBox9.Text == "")
  338.             {
  339.                 textBox9.ForeColor = System.Drawing.Color.Gray;
  340.                 textBox9.Text = "Housenumber";
  341.             }
  342.         }
  343.  
  344.         private void textBox10_Enter(object sender, EventArgs e)
  345.         {
  346.             if (textBox10.ForeColor == System.Drawing.Color.Gray)
  347.             {
  348.                 textBox10.ForeColor = System.Drawing.Color.Black;
  349.                 textBox10.Text = "";
  350.             }
  351.         }
  352.  
  353.         private void textBox10_Leave(object sender, EventArgs e)
  354.         {
  355.             if (textBox10.Text == "")
  356.             {
  357.                 textBox10.ForeColor = System.Drawing.Color.Gray;
  358.                 textBox10.Text = "Password";
  359.             }
  360.         }
  361.  
  362.         private void dateTimePicker1_Enter(object sender, EventArgs e)
  363.         {
  364.  
  365.         }
  366.  
  367.         private void dateTimePicker1_Leave(object sender, EventArgs e)
  368.         {
  369.  
  370.         }
  371.  
  372.         private void textBox10_TextChanged(object sender, EventArgs e)
  373.         {
  374.             if (textBox10.Text == "")
  375.             {
  376.                 checkBox1.Checked = false;
  377.                 checkBox2.Checked = false;
  378.                 checkBox3.Checked = false;
  379.                 checkBox4.Checked = false;
  380.                 checkBox7.Checked = false;
  381.             }
  382.  
  383.             if (textBox10.ForeColor == System.Drawing.Color.Black && checkBox6.Checked == false)
  384.             {
  385.                 textBox10.UseSystemPasswordChar = true;
  386.             }
  387.             else if (textBox10.ForeColor == System.Drawing.Color.Gray || checkBox6.Checked == true)
  388.             {
  389.                 textBox10.UseSystemPasswordChar = false;
  390.             }
  391.  
  392.             if (textBox10.Text != "" && textBox10.Text != "Password")
  393.             {
  394.                 checkBox6.Enabled = true;
  395.  
  396.                 int pass_length = textBox10.Text.Length;
  397.  
  398.                 int a = 0, b = 0, c = 0, d = 0, f = 0;
  399.  
  400.                 for (int i = 0; i < pass_length; i++)
  401.                 {
  402.                     if (pass_length >= 8)
  403.                     {
  404.                         checkBox7.Checked = true;
  405.                     }
  406.                     else if (pass_length < 8) checkBox7.Checked = false;
  407.  
  408.                     if (Char.IsUpper(textBox10.Text[i]))
  409.                     {
  410.                         checkBox1.Checked = true;
  411.                         b = i;
  412.                     }
  413.                     else if(Char.IsUpper(textBox10.Text[b]) != true) checkBox1.Checked = false;
  414.  
  415.                     if (Char.IsLower(textBox10.Text[i]))
  416.                     {
  417.                         checkBox2.Checked = true;
  418.                         c = i;
  419.                     }
  420.                     else if (Char.IsLower(textBox10.Text[c]) != true) checkBox2.Checked = false;
  421.  
  422.                     if (int.TryParse(Convert.ToString(textBox10.Text[i]), out a))
  423.                     {
  424.                         checkBox3.Checked = true;
  425.                         d = i;
  426.                     }
  427.                     else if (int.TryParse(Convert.ToString(textBox10.Text[d]), out a) != true) checkBox3.Checked = false;
  428.  
  429.                     if ((textBox10.Text[i]>32) && (textBox10.Text[i]<48))
  430.                     {
  431.                         checkBox4.Checked = true;
  432.                         f = i;
  433.                     }
  434.                     else if ((textBox10.Text[f] > 32) && (textBox10.Text[f] < 48) != true) checkBox4.Checked = false;
  435.  
  436.                     if (checkBox1.Checked == true && checkBox2.Checked == true && checkBox3.Checked == true && checkBox4.Checked == true && checkBox7.Checked == true)
  437.                     {
  438.                         check_password = true;
  439.                     }
  440.                     else { check_password = false; }
  441.                 }
  442.             }
  443.             else { checkBox6.Enabled = false; }
  444.         }
  445.  
  446.         private void textBox5_TextChanged(object sender, EventArgs e)
  447.         {
  448.             int check_length = textBox5.Text.Length;
  449.  
  450.             if (check_length >= 6 && check_length <= 20 && textBox5.ForeColor == System.Drawing.Color.Black)
  451.             {
  452.                 checkBox8.Checked = true;
  453.                 check_user_id = true;
  454.             }
  455.             else { checkBox8.Checked = false; check_user_id = false; }
  456.         }
  457.  
  458.         private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
  459.         {
  460.             DateTime current_time = DateTime.Now;
  461.             DateTime picked_time = Convert.ToDateTime(dateTimePicker1.Text);
  462.  
  463.             int value = DateTime.Compare(current_time, picked_time);
  464.  
  465.             if (value > 0) { checkBox9.Checked = true; check_birthday = true; }
  466.             else { checkBox9.Checked = false; check_birthday = false; }
  467.         }
  468.  
  469.         private void groupBox2_Enter(object sender, EventArgs e)
  470.         {
  471.  
  472.         }
  473.  
  474.         private void button3_Click(object sender, EventArgs e)
  475.         {
  476.             using (SqlConnection conn = new SqlConnection())
  477.             {
  478.                 if (textBox11.Text != "" && textBox12.Text != "")
  479.                 {
  480.                     DialogResult returned_result = MessageBox.Show("Ya' sure, that ya' really wanna' do that?", "Sure?", MessageBoxButtons.YesNo);
  481.  
  482.                     if (returned_result == DialogResult.Yes)
  483.                         check_pure_intentions = true;
  484.  
  485.                         if (check_pure_intentions)
  486.                         {
  487.                             conn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Alexandria\Database\Database1.mdf;Integrated Security=True;";
  488.                             conn.Open();            //Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Alexandria\Database\Database1.mdf;Integrated Security=True
  489.  
  490.                             try
  491.                             {
  492.                                 SqlCommand DeleteCommand = new SqlCommand("DELETE from USERS where @0 = user_id and @1 = user_password;", conn);
  493.  
  494.                                 DeleteCommand.Parameters.Add(new SqlParameter("@0", textBox12.Text));
  495.                                 DeleteCommand.Parameters.Add(new SqlParameter("@1", textBox11.Text));
  496.  
  497.                                 if (DeleteCommand.ExecuteNonQuery() == 1)
  498.                                 {
  499.                                     MessageBox.Show("You deleted, " + textBox12.Text);
  500.  
  501.                                     textBox11.Text = "";
  502.                                     textBox12.Text = "";
  503.                                 }
  504.                                 else
  505.                                 {
  506.                                     MessageBox.Show("Sorry, but we need valid informations");
  507.                                 }
  508.                             }
  509.                             catch (SqlException)
  510.                             {
  511.                                 MessageBox.Show("Wrong UserID or password!");
  512.                             }
  513.                         }
  514.                         else
  515.                         {
  516.                             textBox11.Text = "";
  517.                             textBox12.Text = "";
  518.                         }
  519.                 }
  520.                 else
  521.                 {
  522.                     MessageBox.Show("Damn son, just enter some valid values!");
  523.                 }
  524.             }
  525.         }
  526.  
  527.         private void checkBox10_CheckedChanged(object sender, EventArgs e)
  528.         {
  529.             if (checkBox10.Checked == false)
  530.             {
  531.                 textBox11.UseSystemPasswordChar = true;
  532.             }
  533.             else if (checkBox10.Checked == true)
  534.             {
  535.                 textBox11.UseSystemPasswordChar = false;
  536.             }
  537.         }
  538.  
  539.         private void radioButton1_CheckedChanged(object sender, EventArgs e)
  540.         {
  541.             if(radioButton1.Checked == true)
  542.             {
  543.                 groupBox1.Visible = true;
  544.                 groupBox2.Visible = true;
  545.                 groupBox3.Visible = false;
  546.             }
  547.             else if(radioButton2.Checked == true)
  548.             {
  549.                 groupBox1.Visible = false;
  550.                 groupBox2.Visible = false;
  551.                 groupBox3.Visible = true;
  552.             }
  553.         }
  554.     }
  555. }
Advertisement
Add Comment
Please, Sign In to add comment