Guest User

Untitled

a guest
Jan 8th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.82 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.Windows.Forms;
  9. using MySql.Data.MySqlClient;
  10. using System.Globalization;
  11. using MySql.Data;
  12. //Craig D'Souza
  13. //January 8 2017
  14. //StudentInformationSystem
  15. //Create a student info program using MySQL
  16. namespace StudentInformation
  17. {
  18.     public partial class frmAdmin : Form
  19.     {
  20.         private MySqlConnection connection = new MySqlConnection();
  21.         int[] Marks = new int[5];
  22.         string toPass;
  23.         string toPass2;
  24.  
  25.         //admin form
  26.         public frmAdmin(string recieved, string recieved2)
  27.         {
  28.             InitializeComponent();
  29.             connection.ConnectionString = @"Server=cra1g.tk; Database=craigstudentinfo; Uid=craig; Pwd=password123;";
  30.             toPass = recieved;
  31.             toPass2 = recieved2;
  32.         }
  33.  
  34.         private void frmAdmin_Load(object sender, EventArgs e)
  35.         {
  36.             txtLevel.Enabled = false;
  37.             txtUsername.Enabled = false;
  38.             txtPassword.Enabled = false;
  39.             txtMarks.Enabled = false;
  40.             btnUpdate.Visible = false;
  41.             PopulateCmbName();
  42.             disableUpdate();
  43.             txtStudentId.Enabled = false;
  44.             txtAVG.Enabled = false;
  45.             btnEdit.Enabled = false;
  46.             btnChart.Enabled = false;
  47.             btnClearChart.Enabled = false;
  48.         }
  49.  
  50.         public void enableUpdate()
  51.         {
  52.             txtFirstName.Enabled = true;
  53.             txtLastName.Enabled = true;
  54.             dtpDOB.Enabled = true;
  55.             btnUpdate.Visible = true;
  56.             btnDelete.Visible = true;
  57.         }
  58.         public void disableUpdate()
  59.         {
  60.             txtFirstName.Enabled = false;
  61.             txtLastName.Enabled = false;
  62.             dtpDOB.Enabled = false;
  63.             btnSave.Visible = false;
  64.             btnDelete.Visible = false;
  65.  
  66.         }
  67.         private void PopulateCmbName()
  68.         {
  69.             cmbNames.Items.Clear();
  70.             try
  71.             {
  72.                 connection.Open();
  73.                 MySqlCommand command = new MySqlCommand();
  74.                 command.Connection = connection;
  75.  
  76.                 string query = "SELECT LastName, FirstName FROM tblstudents";
  77.                 command.CommandText = query;
  78.                 MySqlDataReader reader = command.ExecuteReader();
  79.  
  80.                 while (reader.Read())
  81.                 {
  82.                     if (reader[0].ToString() == "min6" && reader[1].ToString() == "ad")
  83.                     {
  84.                         //skip
  85.                     }
  86.                     else
  87.                     {
  88.                         cmbNames.Items.Add(reader[0].ToString() + "," + reader[1].ToString());
  89.                     }
  90.                 }
  91.                 reader.Close();
  92.                 connection.Close();
  93.             }
  94.             catch (Exception e)
  95.             {
  96.                 MessageBox.Show(e.ToString());
  97.                 connection.Close();
  98.             }
  99.         }
  100.         private void btnEdit_Click(object sender, EventArgs e)
  101.         {
  102.             DialogResult dialogResult = MessageBox.Show("You are about to enter edit mode!", "Edit Mode", MessageBoxButtons.YesNo);
  103.             if (dialogResult == DialogResult.Yes)
  104.             {
  105.                 cmbNames.Enabled = false;
  106.                 txtMarks.Enabled = true;
  107.                 btnEdit.Visible = false;
  108.                 txtUsername.Enabled = true;
  109.                 txtPassword.Enabled = true;
  110.                 enableUpdate();
  111.                
  112.             }
  113.             else
  114.             {
  115.  
  116.             }
  117.         }
  118.  
  119.         private void cmbNames_SelectedIndexChanged(object sender, EventArgs e)
  120.         {
  121.               try
  122.               {
  123.                   btnChart.Enabled = true;
  124.                   btnClearChart.Enabled = true;
  125.                   txtUsername.Enabled = false;
  126.                   txtPassword.Enabled = false;
  127.                   txtMarks.Enabled = false;
  128.                   txtFirstName.Enabled = false;
  129.                   txtLastName.Enabled = false;
  130.                   dtpDOB.Enabled = false;
  131.                   btnEdit.Enabled = true;
  132.                   txtMarks.Text = "";
  133.                   connection.Open();
  134.                   MySqlCommand command = new MySqlCommand();
  135.                   command.Connection = connection;
  136.                   string query = "SELECT FirstName, LastName, DOB, Mark1, Mark2, Mark3, Mark4, Mark5, tblMarks.StuID, Username, Password FROM tblMarks LEFT JOIN tblStudents ON tblMarks.StuID=tblStudents.StuID WHERE LastName + ',' +FirstName='" + cmbNames.Text + "' ";
  137.                   command.CommandText = query;
  138.                   MySqlDataReader reader = command.ExecuteReader();
  139.                   if (reader.Read() == true)
  140.                   {
  141.                       for (int i = 0; i < cmbNames.SelectedIndex; i++)
  142.                       {
  143.                           reader.Read();
  144.                       }
  145.                   }
  146.                   int Total = 0;
  147.                   txtFirstName.Text = reader["FirstName"].ToString();
  148.                   txtLastName.Text = reader["LastName"].ToString();
  149.                   txtStudentId.Text = reader["stuID"].ToString();
  150.                   txtUsername.Text = reader["Username"].ToString();
  151.                   txtPassword.Text = reader["Password"].ToString();
  152.                  
  153.                   dtpDOB.Text = Convert.ToDateTime(reader["DOB"].ToString()).ToString("yyyy/MM/dd");
  154.                   for (int i = 0; i < 5; i++)
  155.                   {
  156.                       txtMarks.Text += (reader[i + 3].ToString()) + Environment.NewLine;
  157.                       Total += int.Parse(reader[i + 3].ToString());
  158.                   }
  159.                   txtAVG.Text = (Total / 5).ToString();
  160.                   reader.Close();
  161.                   connection.Close();
  162.               }
  163.               catch (Exception ex)
  164.               {
  165.                   MessageBox.Show(ex.ToString());
  166.                   connection.Close();
  167.               }
  168.           }
  169.         private void btnNew_Click(object sender, EventArgs e)
  170.         {
  171.             txtLevel.Enabled = false;
  172.             txtUsername.Enabled = true;
  173.             txtPassword.Enabled = true;
  174.             btnSave.Enabled = true;
  175.             btnSave.Visible = true;
  176.             DialogResult dialogResult = MessageBox.Show("Enter new data?", "Entering Data", MessageBoxButtons.YesNo);
  177.             if (dialogResult == DialogResult.Yes)
  178.             {
  179.                 enableNew();
  180.                 clearText();
  181.             }
  182.             else if (dialogResult == DialogResult.No)
  183.             {
  184.                 btnSave.Visible = false;
  185.                 txtFirstName.Enabled = false;
  186.                 txtLastName.Enabled = false;
  187.                 dtpDOB.Enabled = false;
  188.                 txtMarks.Enabled = false;
  189.             }
  190.             enableText();
  191.         }
  192.         private void btnSave_Click(object sender, EventArgs e)
  193.         {
  194.             try
  195.             {
  196.                 int NewID = assignStudentID();
  197.                 connection.Open();
  198.                 MySqlCommand command = new MySqlCommand();
  199.                 command.Connection = connection;
  200.  
  201.                 for (int i = 0; i < 5; i++)
  202.                 {
  203.                     Marks[i] = int.Parse(txtMarks.Lines[i]);
  204.                 }
  205.                 command.CommandText = "INSERT INTO tblstudents (FirstName, LastName, DOB) VALUES ('" + txtFirstName.Text + "','" + txtLastName.Text + "','" + dtpDOB.Text + "')";
  206.                 command.ExecuteNonQuery();
  207.  
  208.                 command.CommandText = "SELECT StuID FROM tblstudents WHERE FirstName='" + txtFirstName.Text + "' ";
  209.  
  210.                 MySqlDataReader reader = command.ExecuteReader();
  211.  
  212.                 reader.Read();
  213.                 txtStudentId.Text = NewID.ToString();
  214.                 reader.Close();
  215.  
  216.                 command.CommandText = "INSERT INTO tblmarks (StuID, Mark1, Mark2, Mark3, Mark4, Mark5) VALUES ('" + txtStudentId.Text + "', '" + Marks[0] + "', '" + Marks[1] + "', '" + Marks[2] + "', '" + Marks[3] + "', '" + Marks[4] + "')";
  217.  
  218.                 command.ExecuteNonQuery();
  219.  
  220.                 MessageBox.Show("Data Saved Successfully");
  221.                 connection.Close();
  222.  
  223.                 clearText();
  224.                 cmbNames.Enabled = true;
  225.                 cmbNames.Items.Clear();
  226.                 PopulateCmbName();
  227.             }
  228.             catch (Exception ex)
  229.             {
  230.                 MessageBox.Show(ex.ToString());
  231.                 connection.Close();
  232.             }
  233.  
  234.         }
  235.         private void enableNew()
  236.         {
  237.             btnNew.Enabled = true;
  238.             btnSave.Enabled = true;
  239.             btnEdit.Enabled = true;
  240.             btnDelete.Enabled = true;
  241.             btnExit.Enabled = true;
  242.             btnChart.Enabled = true;
  243.             btnClearChart.Enabled = true;
  244.         }
  245.         private void clearText()
  246.         {
  247.             txtStudentId.Text = "";
  248.             txtFirstName.Text = "";
  249.             txtLastName.Text = "";
  250.             dtpDOB.Text = "";
  251.             txtAVG.Text = "";
  252.             txtMarks.Text = "";
  253.         }
  254.         private void enableText()
  255.         {
  256.             txtStudentId.Enabled = false;
  257.             txtFirstName.Enabled = true;
  258.             txtLastName.Enabled = true;
  259.             dtpDOB.Enabled = true;
  260.             txtAVG.Enabled = false;
  261.             txtMarks.Enabled = true;
  262.         }
  263.  
  264.         private void btnDelete_Click(object sender, EventArgs e)
  265.         {
  266.             try
  267.             {
  268.                 connection.Open();
  269.                 MySqlCommand command = new MySqlCommand();
  270.  
  271.                 command.Connection = connection;
  272.                 string Query = "DELETE FROM tblmarks WHERE StuID=" + txtStudentId.Text + "";
  273.                 //string Query = "DELETE from tblstudents WHERE StudID=" + txtStudentID.Text + "";
  274.                 command.CommandText = Query;
  275.                 command.ExecuteNonQuery();
  276.  
  277.                 //Query = "DELETE from tblmarks WHERE StudID=" + txtStudentID.Text + "";
  278.                 Query = "DELETE FROM tblstudents WHERE StuID=" + txtStudentId.Text + "";
  279.                 command.CommandText = Query;
  280.                 command.ExecuteNonQuery();
  281.  
  282.                 MessageBox.Show("Data Deleted");
  283.                 connection.Close();
  284.                 clearText();
  285.                 cmbNames.Items.Clear();
  286.                 btnDelete.Visible = false;
  287.                 btnUpdate.Visible= false;
  288.                 btnEdit.Visible = true;
  289.                 txtFirstName.Enabled = false;
  290.                 txtLastName.Enabled = false;
  291.                 dtpDOB.Enabled = false;
  292.                 txtUsername.Enabled = false;
  293.                 txtPassword.Enabled = false;
  294.                 cmbNames.Enabled = true;
  295.                 txtMarks.Enabled = false;
  296.                 PopulateCmbName();
  297.             }
  298.             catch (Exception ex)
  299.             {
  300.                 MessageBox.Show("Error" + ex);
  301.                 connection.Close();
  302.             }
  303.         }
  304.  
  305.         private void btnChart_Click(object sender, EventArgs e)
  306.         {
  307.             try
  308.             {
  309.                 connection.Open();
  310.                 MySqlCommand command = new MySqlCommand();
  311.                 command.Connection = connection;
  312.  
  313.                 string query = "SELECT Mark1, Mark2, Mark3, Mark4, Mark5 FROM tblmarks WHERE tblmarks.StuID=" + txtStudentId.Text;
  314.  
  315.                 command.CommandText = query;
  316.                 MySqlDataReader reader = command.ExecuteReader();
  317.                 chartMarks.Series["Marks"].YValuesPerPoint = 5;
  318.  
  319.                 while (reader.Read())
  320.                 {
  321.                     //clear the chart not the series
  322.                     foreach (var series in chartMarks.Series)
  323.                     {
  324.                         series.Points.Clear();
  325.                     }
  326.  
  327.                     for (int x = 0; x < 5; x++)
  328.                     {
  329.                         this.chartMarks.Series["Marks"].Points.AddXY("Mark" + Convert.ToString(x + 1), reader[x].ToString());
  330.                     }
  331.                 }
  332.                 connection.Close();
  333.  
  334.             }
  335.             catch (Exception err)
  336.             {
  337.                 MessageBox.Show("ERROR" + err);
  338.                 connection.Close();
  339.             }
  340.         }
  341.         //snippet//works
  342.         private void btnClearChart_Click(object sender, EventArgs e)
  343.         {
  344.             foreach (var series in chartMarks.Series)
  345.             {
  346.                 series.Points.Clear();
  347.             }
  348.         }
  349.         private bool CheckMarks(bool NoErrors = false, string Counter = "")
  350.         {
  351.             for (int i = 0; i < 5; i++)
  352.             {
  353.                 if ((int.Parse(txtMarks.Lines[i]) > 100) || (int.Parse(txtMarks.Lines[i]) < 0))
  354.                 {
  355.                     Counter += i + ",";
  356.                 }
  357.  
  358.             }
  359.             if (Counter == "")
  360.             {
  361.                 NoErrors = true;
  362.             }
  363.             else
  364.             {
  365.                 NoErrors = false;
  366.                 MessageBox.Show("Lines " + Counter + "have invalid values");
  367.             }
  368.             return NoErrors;
  369.  
  370.         }
  371.         public int countLines()
  372.         {
  373.             int counter = 0;
  374.             //Counting the amount of lines
  375.             for (int i = 0; i < txtMarks.Lines.Length; i++)
  376.             {
  377.  
  378.                 if ((txtMarks.Lines[i] != null) && (txtMarks.Lines[i] != ""))
  379.                 {
  380.                     counter += 1;
  381.  
  382.                 }
  383.             }
  384.             return (counter);
  385.         }      //Used to count the lines in txtMarks
  386.         private void txtUpdate_Click(object sender, EventArgs e)
  387.         {
  388.             if (countLines() == 5)
  389.             {
  390.                 if (CheckMarks() == true)
  391.                 {
  392.                     try
  393.                     {
  394.                         txtUsername.Enabled = false;
  395.                         txtPassword.Enabled = false;
  396.                         connection.Open();
  397.                         string dateOfBirth = dtpDOB.Value.ToString("yyyy/MM/dd");
  398.                         MySqlCommand command = new MySqlCommand();
  399.                         command.Connection = connection;
  400.                         string Query = "Update tblStudents SET FirstName ='" + txtFirstName.Text + "',LastName ='" + txtLastName.Text + "',Username ='" + txtUsername.Text + "',Password ='" + txtPassword.Text + "',DOB ='" + dtpDOB.Text + "'Where StuID =" + txtStudentId.Text;
  401.                         command.CommandText = Query;
  402.                         command.ExecuteNonQuery();
  403.  
  404.                         Query = "Update tblMarks SET Mark1 ='" + txtMarks.Lines[0] + "',Mark2 ='" + txtMarks.Lines[1] + "',Mark3 ='" + txtMarks.Lines[2] + "',Mark4 ='" + txtMarks.Lines[3] + "',Mark5 ='" + txtMarks.Lines[4] + "'Where StuID =" + txtStudentId.Text;
  405.                         command.CommandText = Query;
  406.                         command.ExecuteNonQuery();
  407.                         connection.Close();
  408.                         MessageBox.Show("Data Updated!");
  409.                         txtFirstName.Enabled = false;
  410.                         txtLastName.Enabled = false;
  411.                         dtpDOB.Enabled = false;
  412.                         cmbNames.Enabled = true;
  413.                         btnUpdate.Visible = false;
  414.                         btnDelete.Visible = false;
  415.                         txtMarks.Enabled = false;
  416.                         btnEdit.Visible = true;
  417.                     }
  418.                     catch (Exception error)
  419.                     {
  420.                         cmbNames.Enabled = false;
  421.                         MessageBox.Show("Error" + error);
  422.                     }
  423.                 }
  424.             }
  425.             else
  426.             {
  427.                 MessageBox.Show("You currently have " + countLines() + " marks. Please make sure you only have 5 marks");
  428.             }
  429.         }
  430.  
  431.         private void txtFirstName_KeyPress(object sender, KeyPressEventArgs e)
  432.         {
  433.             e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
  434.         }
  435.  
  436.         private void txtLastName_KeyPress(object sender, KeyPressEventArgs e)
  437.         {
  438.             e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
  439.         }
  440.  
  441.         private void txtMarks_KeyPress(object sender, KeyPressEventArgs e)
  442.         {
  443.             if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
  444.             {
  445.                 e.Handled = true;
  446.             }
  447.         }
  448.         public int assignStudentID()
  449.         {
  450.             int NEWID = 0;
  451.             try
  452.             {
  453.                 connection.Open();
  454.                 MySqlCommand command = new MySqlCommand();
  455.  
  456.                 command.Connection = connection;
  457.  
  458.                 command.CommandText = "SELECT StuID FROM tblStudents Order by StuID";
  459.                 MySqlDataReader reader = command.ExecuteReader();
  460.                 //reader.Read();
  461.  
  462.                 string Check = "";
  463.                 int counter = 0;
  464.                 if (reader.HasRows)
  465.                 {
  466.                     while (reader.Read())
  467.                     {
  468.                         counter++;
  469.                         Check += reader[0].ToString() + ",";
  470.                     }
  471.                 }
  472.  
  473.                 reader.Close();
  474.  
  475.                 int[] StudentID = new int[counter];
  476.  
  477.  
  478.                 for (int i = 0; i < StudentID.Length; i++)
  479.                 {
  480.                     StudentID[i] = int.Parse(Check.Split(',')[i]);
  481.                 }
  482.  
  483.                 //Find the largest ID or largest number in the array
  484.                 int LargestID = 0;
  485.                 for (int i = 0; i < StudentID.Length; i++)
  486.                 {
  487.                     if (LargestID < StudentID[i])
  488.                     {
  489.                         LargestID = StudentID[i];
  490.                     }
  491.                 }
  492.  
  493.  
  494.                 for (int y = 1; y <= LargestID; y++)
  495.                 {
  496.                     for (int i = y - 1; i < StudentID.Length; i++)
  497.                     {
  498.                         if (StudentID[i] == y)
  499.                         {
  500.                             break;
  501.                         }
  502.                         else
  503.                         {
  504.                             NEWID = y;
  505.                         }
  506.                     }
  507.                     if (NEWID != 0)
  508.                     {
  509.                         break;
  510.                     }
  511.                 }
  512.  
  513.                 if (NEWID == 0)
  514.                 {
  515.                     NEWID = LargestID + 1;
  516.                 }
  517.  
  518.                 connection.Close();
  519.                 return (NEWID);
  520.  
  521.             }
  522.             catch (Exception ex)
  523.             {
  524.  
  525.                 MessageBox.Show("ERROR" + ex);
  526.                 connection.Close();
  527.                 return (NEWID);
  528.             }
  529.         }  //Method used to determine the id when a student is added
  530.     }
  531. }
Advertisement
Add Comment
Please, Sign In to add comment