private void btnSave_Click(object sender, EventArgs e)
        {
            Valid mark = new Valid(Convert.ToInt16(txtKn.Text), Convert.ToInt16(txtApp.Text), Convert.ToInt16(txtTh.Text), Convert.ToInt16(txtCom.Text));//uses enpasulation to figure out if mark is valid or not

            if (mark.Mark == 0)
            {
                MessageBox.Show("Invalid Mark");
                connection.Close();
            }
            else
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "UPDATE tblTest1 SET Knowledge='" + txtKn.Text + "' ,Application='" + txtApp.Text + "' ,Thinking='" + txtCom.Text + "' ,Communication='" + txtTh.Text + "' where StuID=" + lblStu.Text + "";//update the data in the database
                command.CommandText = query;
                command.ExecuteNonQuery();
                connection.Close();
                MessageBox.Show("Data Edited");
                double average = weighting();
                if (average != -1)//this uses the method to check basically what -1 means is that the numbers dont add up to 100
                {
                    try
                    {
                        connection.Open();
                        command.Connection = connection;
                        int editMark = Convert.ToInt16(lblMarkNum.Text);
                        if (editMark == 1)//updating all the marks in the main mark book
                        {
                            command.CommandText = "UPDATE tblMarks SET Mark1=" + average + " where StuID =" + lblStu.Text + "";
                        }
                        else if (editMark == 2)
                        {
                            command.CommandText = "UPDATE tblMarks SET Mark2=" + average + " where StuID =" + lblStu.Text + "";
                        }
                        else if (editMark == 3)
                        {
                            command.CommandText = "UPDATE tblMarks SET Mark3=" + average + " where StuID =" + lblStu.Text + "";
                        }
                        else if (editMark == 4)
                        {
                            command.CommandText = "UPDATE tblMarks SET Mark4=" + average + " where StuID =" + lblStu.Text + "";
                        }
                        else if (editMark == 5)
                        {
                            command.CommandText = "UPDATE tblMarks SET Mark5=" + average + " where StuID =" + lblStu.Text + "";
                        }
                        command.ExecuteNonQuery();
                        connection.Close();

                        this.returnA = average;//passes it back fo it can be displayed instead of opening another connection
                    }
                    catch
                    {
                        MessageBox.Show("Error");
                    }
                }
                else
                {
                    MessageBox.Show("Weightings do not equal 100");
                    connection.Close();
                }
                this.Close();
            }
        }
Exemple #2
0
        private void BtnSave_Click(object sender, EventArgs e)
        {
            int count = 0;
            int x;

            if (string.IsNullOrWhiteSpace(txtFirst.Text) || string.IsNullOrWhiteSpace(txtLast.Text) || string.IsNullOrWhiteSpace(txtUser.Text) || string.IsNullOrWhiteSpace(txtPass.Text) || string.IsNullOrWhiteSpace(txtDay.Text) || string.IsNullOrWhiteSpace(txtMonth.Text) || string.IsNullOrWhiteSpace(txtYear.Text))//check if everything is full
            {
                MessageBox.Show("Not all information has been submitted. Please Try Again.");
            }
            else
            {
                if (int.TryParse(txtDay.Text, out x))//makes sure all of it is the right data type
                {
                    if (int.TryParse(txtMonth.Text, out x))
                    {
                        if (int.TryParse(txtYear.Text, out x))
                        {
                            if (int.TryParse(txtFirst.Text, out x))
                            {
                            }
                            else
                            {
                                if (int.TryParse(txtFirst.Text, out x))
                                {
                                }
                                else
                                {
                                    count = 1;
                                }
                            }
                        }
                    }
                }
                if (count == 0)
                {
                    MessageBox.Show("Error is the data input");
                }
                if (count == 1)//if it is
                {
                    connection.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = connection;
                    int    month = Convert.ToInt16(txtMonth.Text);
                    string DOBM  = "";
                    Valid  date  = new Valid(Convert.ToInt16(txtDay.Text), month);//makes sure day and month is valid eg only 12 months
                    if (date.Day == 0 || date.Month == 0)
                    {
                        MessageBox.Show("Invalid date");
                        connection.Close();
                    }
                    else
                    {
                        count++;
                        if (month == 01)//in data base its writeen with letters so change months to 3 letters
                        {
                            DOBM = "Jan";
                        }
                        else if (month == 02)
                        {
                            DOBM = "Feb";
                        }
                        else if (month == 03)
                        {
                            DOBM = "Mar";
                        }
                        else if (month == 04)
                        {
                            DOBM = "Apr";
                        }
                        else if (month == 05)
                        {
                            DOBM = "May";
                        }
                        else if (month == 06)
                        {
                            DOBM = "Jun";
                        }
                        else if (month == 07)
                        {
                            DOBM = "Jul";
                        }
                        else if (month == 08)
                        {
                            DOBM = "Aug";
                        }
                        else if (month == 09)
                        {
                            DOBM = "Sep";
                        }
                        else if (month == 10)
                        {
                            DOBM = "Oct";
                        }
                        else if (month == 11)
                        {
                            DOBM = "Nov";
                        }
                        else if (month == 12)
                        {
                            DOBM = "Dec";
                        }
                    }
                    if (count == 2)
                    {
                        try
                        {
                            string DOB = txtDay.Text + "-" + DOBM + "-" + txtYear.Text;                                                                                                                                                      //puts the three boxes together
                            command.CommandText = "INSERT into tblStudents (FirstName,LastName,UserName,[Password],DOB) values('" + txtFirst.Text + "','" + txtLast.Text + "','" + txtUser.Text + "','" + txtPass.Text + "','" + DOB + "')"; //inserts new into into database
                            command.ExecuteNonQuery();
                            connection.Close();
                            connection.Open();

                            command.CommandText = "SELECT StuId FROM tblStudents WHERE UserName='******'";//gets the stuid so it can be refrensced later
                            OleDbDataReader reader = command.ExecuteReader();
                            int             stuId  = 0;
                            while (reader.Read())
                            {
                                stuId = Convert.ToInt16(reader["StuID"]);
                            }
                            lblStu.Text = Convert.ToString(stuId);
                            MessageBox.Show("This information has been added to the database.");
                            buttonChange(true);//unlocks the buttons
                        }
                        catch
                        {
                            MessageBox.Show("Error");
                            connection.Close();
                        }
                    }
                }
            }
        }
        private void btnSave2_Click(object sender, EventArgs e)
        {
            Valid mark = new Valid(Convert.ToInt16(txtKn.Text), Convert.ToInt16(txtApp.Text), Convert.ToInt16(txtTh.Text), Convert.ToInt16(txtCom.Text));//same ass above

            if (mark.Mark == 0)
            {
                MessageBox.Show("Invalid Mark");
                connection.Close();
            }
            else
            {
                if (string.IsNullOrWhiteSpace(txtKn.Text) || string.IsNullOrWhiteSpace(txtApp.Text) || string.IsNullOrWhiteSpace(txtCom.Text) || string.IsNullOrWhiteSpace(txtTh.Text))//makes sure all info is filled out
                {
                    MessageBox.Show("Not all information has been submitted. Please Try Again.");
                }
                else
                {
                    int count = 0;
                    int x;
                    if (int.TryParse(txtKn.Text, out x))//try parse all the info to make sure its the right type that is inputed
                    {
                        if (int.TryParse(txtApp.Text, out x))
                        {
                            if (int.TryParse(txtCom.Text, out x))
                            {
                                if (int.TryParse(txtTh.Text, out x))
                                {
                                    count = 1;
                                }
                            }
                        }
                    }
                    if (count == 0)
                    {
                        MessageBox.Show("Error is the data input");
                    }
                    if (count == 1)//if iit is then it will continue the code
                    {
                        try
                        {
                            connection.Close();
                            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Students.accdb; Persist Security Info=False;";
                            connection.Open();
                            OleDbCommand command = new OleDbCommand();
                            command.Connection = connection;
                            int markNum = Convert.ToInt16(lblMarkNum.Text);
                            if (markNum == 1)
                            {
                                command.CommandText = "INSERT into tblTest1 (StuID,Knowledge,Application,Thinking,Communication) values('" + lblStu.Text + "','" + txtKn.Text + "','" + txtApp.Text + "','" + txtCom.Text + "','" + txtTh.Text + "')";//this inserts all the info in the category
                            }
                            else if (markNum == 2)
                            {
                                command.CommandText = "INSERT into tblTest2 (StuID,Knowledge,Application,Thinking,Communication)values('" + lblStu.Text + "','" + txtKn.Text + "','" + txtApp.Text + "','" + txtCom.Text + "','" + txtTh.Text + "')";
                            }
                            else if (markNum == 3)
                            {
                                command.CommandText = "INSERT into tblTest3 (StuID,Knowledge,Application,Thinking,Communication) values('" + lblStu.Text + "','" + txtKn.Text + "','" + txtApp.Text + "','" + txtCom.Text + "','" + txtTh.Text + "')";
                            }
                            else if (markNum == 4)
                            {
                                command.CommandText = "INSERT into tblTest4 (StuID,Knowledge,Application,Thinking,Communication) values('" + lblStu.Text + "','" + txtKn.Text + "','" + txtApp.Text + "','" + txtCom.Text + "','" + txtTh.Text + "')";
                            }
                            else if (markNum == 5)
                            {
                                command.CommandText = "INSERT into tblTest5 (StuID,Knowledge,Application,Thinking,Communication) values('" + lblStu.Text + "','" + txtKn.Text + "','" + txtApp.Text + "','" + txtCom.Text + "','" + txtTh.Text + "')";
                            }
                            command.ExecuteNonQuery();
                            connection.Close();
                            if (weighting() != -1)
                            {
                                double average = weighting();
                                connection.Open();
                                if (markNum == 1)
                                {
                                    command.CommandText = "INSERT into tblMarks(StuID,Mark1) values('" + lblStu.Text + "','" + average + "')";//inserts the data so it can creat the row
                                }
                                if (markNum == 2)
                                {
                                    command.CommandText = "UPDATE tblMarks SET Mark2='" + average + "'where StuID=" + lblStu.Text + "";//then it updates it to avoid zerosing it out
                                }
                                else if (markNum == 3)
                                {
                                    command.CommandText = "UPDATE tblMarks SET Mark3='" + average + "'where StuID=" + lblStu.Text + "";
                                }
                                else if (markNum == 4)
                                {
                                    command.CommandText = "UPDATE tblMarks SET Mark4='" + average + "'where StuID=" + lblStu.Text + "";
                                }
                                else if (markNum == 5)
                                {
                                    command.CommandText = "UPDATE tblMarks SET Mark5='" + average + "'where StuID=" + lblStu.Text + "";
                                }

                                command.ExecuteNonQuery();
                                connection.Close();
                                this.returnA = weighting();
                                this.Close();
                            }
                            else
                            {
                                MessageBox.Show("Weightings do not equal 100");
                            }
                        }
                        catch
                        {
                            MessageBox.Show("Error");
                        }
                    }
                }
            }
        }