Advertisement
Guest User

Untitled

a guest
Jan 8th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 25.49 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.             btnExitNew.Visible = false;
  37.             btnExitEdit.Visible = false;
  38.             txtLevel.Enabled = false;
  39.             txtUsername.Enabled = false;
  40.             txtPassword.Enabled = false;
  41.             txtMarks.Enabled = false;
  42.             btnUpdate.Visible = false;
  43.             PopulateCmbName();
  44.             disableUpdate();
  45.             txtStudentId.Enabled = false;
  46.             txtAVG.Enabled = false;
  47.             btnEdit.Enabled = false;
  48.             btnChart.Enabled = false;
  49.             btnClearChart.Enabled = false;
  50.         }
  51.  
  52.         public void enableUpdate()
  53.         {
  54.             txtFirstName.Enabled = true;
  55.             txtLastName.Enabled = true;
  56.             dtpDOB.Enabled = true;
  57.             btnUpdate.Visible = true;
  58.             btnDelete.Visible = true;
  59.         }
  60.         public void disableUpdate()
  61.         {
  62.             txtFirstName.Enabled = false;
  63.             txtLastName.Enabled = false;
  64.             dtpDOB.Enabled = false;
  65.             btnSave.Visible = false;
  66.             btnDelete.Visible = false;
  67.  
  68.         }
  69.         private void PopulateCmbName()
  70.         {
  71.             cmbNames.Items.Clear();
  72.             try
  73.             {
  74.                 connection.Open();
  75.                 MySqlCommand command = new MySqlCommand();
  76.                 command.Connection = connection;
  77.  
  78.                 string query = "SELECT LastName, FirstName FROM tblstudents";
  79.                 command.CommandText = query;
  80.                 MySqlDataReader reader = command.ExecuteReader();
  81.  
  82.                 while (reader.Read())
  83.                 {
  84.                     if (reader[0].ToString() == "min6" && reader[1].ToString() == "ad")
  85.                     {
  86.                         //skip
  87.                     }
  88.                     else
  89.                     {
  90.                         cmbNames.Items.Add(reader[0].ToString() + "," + reader[1].ToString());
  91.                     }
  92.                 }
  93.                 reader.Close();
  94.                 connection.Close();
  95.             }
  96.             catch (Exception e)
  97.             {
  98.                 MessageBox.Show(e.ToString());
  99.                 connection.Close();
  100.             }
  101.         }
  102.         private void btnEdit_Click(object sender, EventArgs e)
  103.         {
  104.             DialogResult dialogResult = MessageBox.Show("You are about to enter edit mode!", "Edit Mode", MessageBoxButtons.YesNo);
  105.             if (dialogResult == DialogResult.Yes)
  106.             {
  107.                 btnExitEdit.Visible = true;
  108.                 cmbNames.Enabled = false;
  109.                 txtMarks.Enabled = true;
  110.                 btnEdit.Visible = false;
  111.                 txtUsername.Enabled = true;
  112.                 txtPassword.Enabled = true;
  113.                 enableUpdate();
  114.                
  115.             }
  116.             else
  117.             {
  118.  
  119.             }
  120.         }
  121.  
  122.         private void cmbNames_SelectedIndexChanged(object sender, EventArgs e)
  123.         {
  124.               try
  125.               {
  126.                   btnChart.Enabled = true;
  127.                   btnClearChart.Enabled = true;
  128.                   txtUsername.Enabled = false;
  129.                   txtPassword.Enabled = false;
  130.                   txtMarks.Enabled = false;
  131.                   txtFirstName.Enabled = false;
  132.                   txtLastName.Enabled = false;
  133.                   dtpDOB.Enabled = false;
  134.                   btnEdit.Enabled = true;
  135.                   txtMarks.Text = "";
  136.                   connection.Open();
  137.                   MySqlCommand command = new MySqlCommand();
  138.                   command.Connection = connection;
  139.                   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 + "' ";
  140.                   command.CommandText = query;
  141.                   MySqlDataReader reader = command.ExecuteReader();
  142.                   if (reader.Read() == true)
  143.                   {
  144.                       for (int i = 0; i < cmbNames.SelectedIndex; i++)
  145.                       {
  146.                           reader.Read();
  147.                       }
  148.                   }
  149.                   int Total = 0;
  150.                   txtFirstName.Text = reader["FirstName"].ToString();
  151.                   txtLastName.Text = reader["LastName"].ToString();
  152.                   txtStudentId.Text = reader["stuID"].ToString();
  153.                   txtUsername.Text = reader["Username"].ToString();
  154.                   txtPassword.Text = reader["Password"].ToString();
  155.                  
  156.                   dtpDOB.Text = Convert.ToDateTime(reader["DOB"].ToString()).ToString("yyyy/MM/dd");
  157.                   for (int i = 0; i < 5; i++)
  158.                   {
  159.                       txtMarks.Text += (reader[i + 3].ToString()) + Environment.NewLine;
  160.                       Total += int.Parse(reader[i + 3].ToString());
  161.                   }
  162.  
  163.                   txtAVG.Text = (Total / 5).ToString();
  164.                   string mark = "";
  165.                   if (int.Parse(txtAVG.Text) >= 90)
  166.                   {
  167.                       mark = "4+";
  168.                   }
  169.                   else if (int.Parse(txtAVG.Text) >= 85 && int.Parse(txtAVG.Text) < 90)
  170.                   {
  171.                       mark = "4";
  172.                   }
  173.                   else if (int.Parse(txtAVG.Text) >= 80 && int.Parse(txtAVG.Text) < 85)
  174.                   {
  175.                       mark = "4-";
  176.                   }
  177.                   else if (int.Parse(txtAVG.Text) < 80 && int.Parse(txtAVG.Text) >= 75)
  178.                   {
  179.                       mark = "3+";
  180.                   }
  181.                   else if (int.Parse(txtAVG.Text) < 75 && int.Parse(txtAVG.Text) >= 70)
  182.                   {
  183.                       mark = "3";
  184.                   }
  185.                   else if (int.Parse(txtAVG.Text) < 70 && int.Parse(txtAVG.Text) >= 65)
  186.                   {
  187.                       mark = "3-";
  188.                   }
  189.                   else if (int.Parse(txtAVG.Text) < 65 && int.Parse(txtAVG.Text) >= 60)
  190.                   {
  191.                       mark = "2+";
  192.                   }
  193.                   else if (int.Parse(txtAVG.Text) < 60 && int.Parse(txtAVG.Text) >= 55)
  194.                   {
  195.                       mark = "2";
  196.                   }
  197.                   else if (int.Parse(txtAVG.Text) < 55 && int.Parse(txtAVG.Text) >= 50)
  198.                   {
  199.                       mark = "2-";
  200.                   }
  201.                   else if (int.Parse(txtAVG.Text) < 50)
  202.                   {
  203.                       mark = "F";
  204.                   }
  205.                   txtLevel.Text = mark;
  206.                   reader.Close();
  207.                   connection.Close();
  208.               }
  209.               catch (Exception ex)
  210.               {
  211.                   MessageBox.Show(ex.ToString());
  212.                   connection.Close();
  213.               }
  214.           }
  215.         private void btnNew_Click(object sender, EventArgs e)
  216.         {
  217.             DialogResult dialogResult = MessageBox.Show("Enter new data?", "Entering Data", MessageBoxButtons.YesNo);
  218.             if (dialogResult == DialogResult.Yes)
  219.             {
  220.                 txtLevel.Enabled = false;
  221.                 txtUsername.Enabled = true;
  222.                 txtPassword.Enabled = true;
  223.                 btnSave.Enabled = true;
  224.                 btnSave.Visible = true;
  225.                 btnExitNew.Visible = true;
  226.                 enableNew();
  227.                 clearText();
  228.             }
  229.             else if (dialogResult == DialogResult.No)
  230.             {
  231.                 btnSave.Visible = false;
  232.                 txtFirstName.Enabled = false;
  233.                 txtLastName.Enabled = false;
  234.                 dtpDOB.Enabled = false;
  235.                 txtMarks.Enabled = false;
  236.             }
  237.             enableText();
  238.         }
  239.         private void btnSave_Click(object sender, EventArgs e)
  240.         {
  241.             try
  242.             {
  243.                 if (countLines() == 5)
  244.                 {
  245.                     if (checkInputMarks() == true)
  246.                     {
  247.                         int NewID = assignStudentID();
  248.                         connection.Open();
  249.                         MySqlCommand command = new MySqlCommand();
  250.                         command.Connection = connection;
  251.  
  252.                         for (int i = 0; i < 5; i++)
  253.                         {
  254.                             Marks[i] = int.Parse(txtMarks.Lines[i]);
  255.                         }
  256.                         command.CommandText = "INSERT INTO tblStudents (StuID, FirstName, LastName, Username, Password, DOB) VALUES ('" + NewID + "','" + txtFirstName.Text + "','" + txtLastName.Text + "','" + txtUsername.Text + "','" + txtPassword.Text + "','" + dtpDOB.Text + "');";
  257.                         command.ExecuteNonQuery();
  258.                         command.CommandText = "SELECT StuID FROM tblstudents WHERE FirstName='" + txtFirstName.Text + "' ";
  259.                         MySqlDataReader reader = command.ExecuteReader();
  260.                         reader.Read();
  261.                         reader.Close();
  262.                         command.CommandText = "INSERT INTO tblmarks (MarkID, StuID, Mark1, Mark2, Mark3, Mark4, Mark5) VALUES ('" + NewID + "','" + NewID + "', '" + Marks[0] + "', '" + Marks[1] + "', '" + Marks[2] + "', '" + Marks[3] + "', '" + Marks[4] + "')";
  263.                         command.ExecuteNonQuery();
  264.  
  265.                         MessageBox.Show("Data Saved Successfully");
  266.                         connection.Close();
  267.  
  268.                         clearText();
  269.                         cmbNames.Enabled = true;
  270.                         cmbNames.Items.Clear();
  271.                         PopulateCmbName();
  272.                         txtFirstName.Enabled = false;
  273.                         txtLastName.Enabled = false;
  274.                         dtpDOB.Enabled = false;
  275.                         txtUsername.Enabled = false;
  276.                         txtPassword.Enabled = false;
  277.                         btnSave.Visible = false;
  278.                         btnExitNew.Visible = false;
  279.                     }
  280.                 }
  281.                 else
  282.                 {
  283.                     MessageBox.Show("You currently have " + countLines() + " marks. Please make sure you only have 5 marks");
  284.                 }
  285.             }
  286.             catch (Exception ex)
  287.             {
  288.                 MessageBox.Show(ex.ToString());
  289.                 connection.Close();
  290.             }
  291.  
  292.         }
  293.         private void enableNew()
  294.         {
  295.             btnNew.Enabled = true;
  296.             btnSave.Enabled = true;
  297.             btnEdit.Enabled = true;
  298.             btnDelete.Enabled = true;
  299.             btnExit.Enabled = true;
  300.             btnChart.Enabled = true;
  301.             btnClearChart.Enabled = true;
  302.         }
  303.         private void clearText()
  304.         {
  305.             txtStudentId.Text = "";
  306.             txtFirstName.Text = "";
  307.             txtLastName.Text = "";
  308.             dtpDOB.Text = "";
  309.             txtAVG.Text = "";
  310.             txtMarks.Text = "";
  311.         }
  312.         private void enableText()
  313.         {
  314.             txtStudentId.Enabled = false;
  315.             txtFirstName.Enabled = true;
  316.             txtLastName.Enabled = true;
  317.             dtpDOB.Enabled = true;
  318.             txtAVG.Enabled = false;
  319.             txtMarks.Enabled = true;
  320.         }
  321.  
  322.         private void btnDelete_Click(object sender, EventArgs e)
  323.         {
  324.             try
  325.             {
  326.                 connection.Open();
  327.                 MySqlCommand command = new MySqlCommand();
  328.  
  329.                 command.Connection = connection;
  330.                 string Query = "DELETE FROM tblmarks WHERE StuID=" + txtStudentId.Text + "";
  331.                 //string Query = "DELETE from tblstudents WHERE StudID=" + txtStudentID.Text + "";
  332.                 command.CommandText = Query;
  333.                 command.ExecuteNonQuery();
  334.  
  335.                 //Query = "DELETE from tblmarks WHERE StudID=" + txtStudentID.Text + "";
  336.                 Query = "DELETE FROM tblstudents WHERE StuID=" + txtStudentId.Text + "";
  337.                 command.CommandText = Query;
  338.                 command.ExecuteNonQuery();
  339.  
  340.                 MessageBox.Show("Data Deleted");
  341.                 connection.Close();
  342.                 clearText();
  343.                 cmbNames.Items.Clear();
  344.                 btnDelete.Visible = false;
  345.                 btnUpdate.Visible= false;
  346.                 btnEdit.Visible = true;
  347.                 txtFirstName.Enabled = false;
  348.                 txtLastName.Enabled = false;
  349.                 dtpDOB.Enabled = false;
  350.                 txtUsername.Enabled = false;
  351.                 txtPassword.Enabled = false;
  352.                 cmbNames.Enabled = true;
  353.                 txtMarks.Enabled = false;
  354.                 PopulateCmbName();
  355.             }
  356.             catch (Exception ex)
  357.             {
  358.                 MessageBox.Show("Error" + ex);
  359.                 connection.Close();
  360.             }
  361.         }
  362.  
  363.         private void btnChart_Click(object sender, EventArgs e)
  364.         {
  365.             try
  366.             {
  367.                 connection.Open();
  368.                 MySqlCommand command = new MySqlCommand();
  369.                 command.Connection = connection;
  370.  
  371.                 string query = "SELECT Mark1, Mark2, Mark3, Mark4, Mark5 FROM tblmarks WHERE tblmarks.StuID=" + txtStudentId.Text;
  372.  
  373.                 command.CommandText = query;
  374.                 MySqlDataReader reader = command.ExecuteReader();
  375.                 chartMarks.Series["Marks"].YValuesPerPoint = 5;
  376.  
  377.                 while (reader.Read())
  378.                 {
  379.                     //clear the chart not the series
  380.                     foreach (var series in chartMarks.Series)
  381.                     {
  382.                         series.Points.Clear();
  383.                     }
  384.  
  385.                     for (int x = 0; x < 5; x++)
  386.                     {
  387.                         this.chartMarks.Series["Marks"].Points.AddXY("Mark" + Convert.ToString(x + 1), reader[x].ToString());
  388.                     }
  389.                 }
  390.                 connection.Close();
  391.  
  392.             }
  393.             catch (Exception err)
  394.             {
  395.                 MessageBox.Show("ERROR" + err);
  396.                 connection.Close();
  397.             }
  398.         }
  399.         //snippet//works
  400.         private void btnClearChart_Click(object sender, EventArgs e)
  401.         {
  402.             foreach (var series in chartMarks.Series)
  403.             {
  404.                 series.Points.Clear();
  405.             }
  406.         }
  407.         private bool CheckMarks(bool NoErrors = false, string Counter = "")
  408.         {
  409.             for (int i = 0; i < 5; i++)
  410.             {
  411.                 if ((int.Parse(txtMarks.Lines[i]) > 100) || (int.Parse(txtMarks.Lines[i]) < 0))
  412.                 {
  413.                     Counter += i + ",";
  414.                 }
  415.  
  416.             }
  417.             if (Counter == "")
  418.             {
  419.                 NoErrors = true;
  420.             }
  421.             else
  422.             {
  423.                 NoErrors = false;
  424.                 MessageBox.Show("Lines " + Counter + "have invalid values");
  425.             }
  426.             return NoErrors;
  427.  
  428.         }
  429.         public int countLines()
  430.         {
  431.             int counter = 0;
  432.             //Counting the amount of lines
  433.             for (int i = 0; i < txtMarks.Lines.Length; i++)
  434.             {
  435.  
  436.                 if ((txtMarks.Lines[i] != null) && (txtMarks.Lines[i] != ""))
  437.                 {
  438.                     counter += 1;
  439.  
  440.                 }
  441.             }
  442.             return (counter);
  443.         }      //Used to count the lines in txtMarks
  444.         private void txtUpdate_Click(object sender, EventArgs e)
  445.         {
  446.             if (countLines() == 5)
  447.             {
  448.                 if (CheckMarks() == true)
  449.                 {
  450.                     try
  451.                     {
  452.                         if (checkInputMarks() == true)
  453.                         {
  454.                             txtUsername.Enabled = false;
  455.                             txtPassword.Enabled = false;
  456.                             connection.Open();
  457.                             string dateOfBirth = dtpDOB.Value.ToString("yyyy/MM/dd");
  458.                             MySqlCommand command = new MySqlCommand();
  459.                             command.Connection = connection;
  460.                             string Query = "Update tblStudents SET FirstName ='" + txtFirstName.Text + "',LastName ='" + txtLastName.Text + "',Username ='" + txtUsername.Text + "',Password ='" + txtPassword.Text + "',DOB ='" + dtpDOB.Text + "'Where StuID =" + txtStudentId.Text;
  461.                             command.CommandText = Query;
  462.                             command.ExecuteNonQuery();
  463.  
  464.                             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;
  465.                             command.CommandText = Query;
  466.                             command.ExecuteNonQuery();
  467.                             connection.Close();
  468.                             MessageBox.Show("Data Updated!");
  469.                             txtFirstName.Enabled = false;
  470.                             txtLastName.Enabled = false;
  471.                             dtpDOB.Enabled = false;
  472.                             cmbNames.Enabled = true;
  473.                             btnUpdate.Visible = false;
  474.                             btnDelete.Visible = false;
  475.                             txtMarks.Enabled = false;
  476.                             btnEdit.Visible = true;
  477.                             btnExitEdit.Visible = false;
  478.                         }
  479.                     }
  480.                     catch (Exception error)
  481.                     {
  482.                         cmbNames.Enabled = false;
  483.                         MessageBox.Show("Error" + error);
  484.                     }
  485.                 }
  486.             }
  487.             else
  488.             {
  489.                 MessageBox.Show("You currently have " + countLines() + " marks. Please make sure you only have 5 marks");
  490.             }
  491.         }
  492.  
  493.         private void txtFirstName_KeyPress(object sender, KeyPressEventArgs e)
  494.         {
  495.             e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
  496.         }
  497.  
  498.         private void txtLastName_KeyPress(object sender, KeyPressEventArgs e)
  499.         {
  500.             e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
  501.         }
  502.  
  503.         private void txtMarks_KeyPress(object sender, KeyPressEventArgs e)
  504.         {
  505.             if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
  506.             {
  507.                 e.Handled = true;
  508.             }
  509.         }
  510.         public bool checkInputMarks()    //Remember to add an error check for the date
  511.         {
  512.             bool Errors = false, FormatErrors = true;
  513.             int Counter = countLines(), Check = 0, Holder = 0;
  514.             string LineERRORS = "";
  515.  
  516.             if (Counter == 5)
  517.             {
  518.                 //Checks if the marks input are intergers
  519.                 for (int i = 0; i < Counter; i++)  //i<txtMarks.Lines.Length;
  520.                 {
  521.                     if (!int.TryParse(txtMarks.Lines[i], out Holder))
  522.                     {
  523.                         Check++;
  524.                     }
  525.                 }
  526.  
  527.                 if (Check == 0)
  528.                 {
  529.                     for (int i = 0; i < Counter; i++)
  530.                     {
  531.                         if (int.Parse(txtMarks.Lines[i]) > 100)
  532.                         {
  533.                             LineERRORS += i + ",";
  534.                         }
  535.                         else if (int.Parse(txtMarks.Lines[i]) < 0)
  536.                         {
  537.                             LineERRORS += (i + 1) + ",";
  538.                         }
  539.                     }
  540.  
  541.                     if (Check == 0)
  542.                     {
  543.                         if (checkFormat() == true)
  544.                         {
  545.                             FormatErrors = false;
  546.                         }
  547.                         else
  548.                         {
  549.                             FormatErrors = true;
  550.                         }
  551.                     }
  552.  
  553.                 }
  554.  
  555.             }
  556.  
  557.  
  558.             else if ((Counter == 5) && (Check == 0) && (LineERRORS != "") && (FormatErrors == true))
  559.             {
  560.                 Errors = false;
  561.                 MessageBox.Show("One or more of your marks is invalid");
  562.             }
  563.             else if ((Counter == 5) && (Check == 0) && (LineERRORS == "") && (FormatErrors == false))
  564.             {
  565.                 Errors = false;
  566.             }
  567.             return (Errors);
  568.         }
  569.  
  570.         public bool checkFormat(bool Correct = true)
  571.         {
  572.             //learned to parse dates from https://www.dotnetperls.com/datetime-tryparse
  573.             DateTime Temp;
  574.             if (DateTime.TryParse("dd,MMM,yy", out Temp))
  575.             {
  576.                 Correct = true;
  577.             }
  578.             else
  579.             {
  580.                 Correct = false;
  581.                 MessageBox.Show("The Date entered was in the wrong format Please follow the format\nyyyy,mm,dd  ex.1999,09,14");
  582.             }
  583.             return (Correct);
  584.         }
  585.         public int assignStudentID()
  586.         {
  587.             int NEWID = 0;
  588.             try
  589.             {
  590.                 connection.Open();
  591.                 MySqlCommand command = new MySqlCommand();
  592.  
  593.                 command.Connection = connection;
  594.  
  595.                 command.CommandText = "SELECT StuID FROM tblStudents Order by StuID";
  596.                 MySqlDataReader reader = command.ExecuteReader();
  597.                 //reader.Read();
  598.  
  599.                 string Check = "";
  600.                 int counter = 0;
  601.                 if (reader.HasRows)
  602.                 {
  603.                     while (reader.Read())
  604.                     {
  605.                         counter++;
  606.                         Check += reader[0].ToString() + ",";
  607.                     }
  608.                 }
  609.  
  610.                 reader.Close();
  611.  
  612.                 int[] StudentID = new int[counter];
  613.  
  614.  
  615.                 for (int i = 0; i < StudentID.Length; i++)
  616.                 {
  617.                     StudentID[i] = int.Parse(Check.Split(',')[i]);
  618.                 }
  619.  
  620.                 //Find the largest ID or largest number in the array
  621.                 int LargestID = 0;
  622.                 for (int i = 0; i < StudentID.Length; i++)
  623.                 {
  624.                     if (LargestID < StudentID[i])
  625.                     {
  626.                         LargestID = StudentID[i];
  627.                     }
  628.                 }
  629.  
  630.  
  631.                 for (int y = 1; y <= LargestID; y++)
  632.                 {
  633.                     for (int i = y - 1; i < StudentID.Length; i++)
  634.                     {
  635.                         if (StudentID[i] == y)
  636.                         {
  637.                             break;
  638.                         }
  639.                         else
  640.                         {
  641.                             NEWID = y;
  642.                         }
  643.                     }
  644.                     if (NEWID != 0)
  645.                     {
  646.                         break;
  647.                     }
  648.                 }
  649.  
  650.                 if (NEWID == 0)
  651.                 {
  652.                     NEWID = LargestID + 1;
  653.                 }
  654.  
  655.                 connection.Close();
  656.                 return (NEWID);
  657.  
  658.             }
  659.             catch (Exception ex)
  660.             {
  661.  
  662.                 MessageBox.Show("ERROR" + ex);
  663.                 connection.Close();
  664.                 return (NEWID);
  665.             }
  666.         }
  667.  
  668.         private void btnExitEdit_Click(object sender, EventArgs e)
  669.         {
  670.             int Selected = cmbNames.SelectedIndex;
  671.             cmbNames.Enabled = true;
  672.             txtFirstName.Enabled = false;
  673.             txtLastName.Enabled = false;
  674.             dtpDOB.Enabled = false;
  675.             txtUsername.Enabled = false;
  676.             txtPassword.Enabled = false;
  677.             cmbNames.SelectedIndex = 1;
  678.             cmbNames.SelectedIndex = Selected;
  679.             txtMarks.Enabled = false;
  680.             btnExitEdit.Visible = false;
  681.             btnUpdate.Visible = false;
  682.             btnDelete.Visible = false;
  683.             btnEdit.Visible = true;
  684.         }
  685.  
  686.         private void btnExitNew_Click(object sender, EventArgs e)
  687.         {
  688.             cmbNames.Enabled = true;
  689.             txtFirstName.Enabled = false;
  690.             txtLastName.Enabled = false;
  691.             dtpDOB.Enabled = false;
  692.             txtUsername.Enabled = false;
  693.             txtPassword.Enabled = false;
  694.             cmbNames.SelectedIndex = 0;
  695.             txtMarks.Enabled = false;
  696.             btnExitNew.Visible = false;
  697.             btnUpdate.Visible = false;
  698.             btnDelete.Visible = false;
  699.             btnEdit.Visible = true;
  700.         }
  701.     }
  702. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement