Example #1
0
        private void btnA_Edit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //let's check first if the textboxes are empty since we cannot allow a blank data
                if (txtCcode.Text.Trim() == null || txtCdesc.Text.Trim() == null)
                {
                    MessageBox.Show("Course Data cannot be empty");
                    return;
                }

                SqlConnect con = new SqlConnect();
                con.conOpen();
                if (con != null)
                {
                    //let's check first if the data trying to change is already exist.
                    string     check    = "SELECT TOP 1 * FROM tblCourse WHERE coursecode = @course and course_id <> @id";
                    SqlCommand cmdcheck = new SqlCommand(check, con.Con);
                    cmdcheck.Parameters.AddWithValue("@course", txtCcode.Text.Trim());
                    cmdcheck.Parameters.AddWithValue("@id", txtCourseiD.Text.Trim());
                    SqlDataReader sdr = cmdcheck.ExecuteReader();

                    if (sdr.Read()) //if theres a record, return, meaning we cannot allow it
                    {
                        MessageBox.Show("Cannot update record, the data you are trying to change already exist.");
                        sdr.Close();
                        return;
                    }
                    else //we can change or update it
                    {
                        sdr.Close();
                        CrudConfiguration           update    = new CrudConfiguration();
                        string                      tablename = "tblCourse";                                                 //tablename
                        string[]                    col       = new string[] { "coursecode", "coursedesc" };                 //tblcolumns to update
                        string[]                    values    = new string[] { txtCcode.Text.Trim(), txtCdesc.Text.Trim() }; //values to pass
                        Dictionary <string, string> conds     = new Dictionary <string, string>
                        {
                            { "course_id", txtCourseiD.Text.Trim() }
                        }; //the conditions to make

                        if (update.updateTrans(tablename, col, values, conds))
                        {
                            CourseLoad("");
                            enabledNewTransbutton();
                            clearControls();
                            MessageBox.Show("Record updated successfully");
                        }
                    }
                }
                else
                {
                    return;
                }
                con.conclose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #2
0
        private void btnA_Save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //let's check first if textboxes are empty since we cannot insert blank data
                if (txtCcode.Text.Trim() == null || txtCdesc.Text.Trim() == null)
                {
                    MessageBox.Show("Course data cannot be empty.");
                    return;
                }
                SqlConnect con = new SqlConnect();
                con.conOpen();
                if (con != null)
                {
                    //check first if the record trying to add is already exist
                    string     check    = "SELECT TOP 1 * FROM tblCourse WHERE coursecode = @ccode and coursedesc = @cdesc";
                    SqlCommand cmdcheck = new SqlCommand(check, con.Con);
                    cmdcheck.Parameters.AddWithValue("@ccode", txtCcode.Text.Trim());
                    cmdcheck.Parameters.AddWithValue("@cdesc", txtCdesc.Text.Trim());
                    SqlDataReader sdr = cmdcheck.ExecuteReader();

                    if (sdr.Read()) //if theres a record
                    {
                        MessageBox.Show("Cannot Insert. The record you are trying to add is already exist. ");
                        sdr.Close();
                        return;
                    }
                    else //let's insert and add the data
                    {
                        sdr.Close();
                        CrudConfiguration insert    = new CrudConfiguration();
                        string[]          col       = new string[] { "coursecode", "coursedesc" };
                        string[]          values    = new string[] { txtCcode.Text.Trim(), txtCdesc.Text.Trim() };
                        string            tablename = "tblCourse";
                        if (insert.saveTrans(tablename, col, values))
                        {
                            CourseLoad("");
                            enabledNewTransbutton();
                            clearControls();
                            MessageBox.Show("Record saved successfully");
                        }
                    }
                }
                else
                {
                    return;
                }
                con.conclose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #3
0
        private void btnA_Delete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SqlConnect con = new SqlConnect();
                con.conOpen();
                if (con != null)
                {
                    //let's check first if the data trying to delete is being used by the borrowers list.
                    string     check    = "SELECT TOP 1 * FROM tblBarrower WHERE course = @id";
                    SqlCommand cmdcheck = new SqlCommand(check, con.Con);
                    cmdcheck.Parameters.AddWithValue("@id", txtCourseiD.Text.Trim());
                    SqlDataReader sdr = cmdcheck.ExecuteReader();

                    if (sdr.Read()) //if theres a record, return, meaning we cannot allow it
                    {
                        MessageBox.Show("Cannot delete record, the data you are trying to delete is being used by the barrowers");
                        sdr.Close();
                        return;
                    }
                    else //we can delete it
                    {
                        sdr.Close();
                        CrudConfiguration           delete    = new CrudConfiguration();
                        string                      tablename = "tblCourse"; //tablename
                        Dictionary <string, string> conds     = new Dictionary <string, string>
                        {
                            { "course_id", txtCourseiD.Text.Trim() }
                        }; //the conditions to make

                        if (delete.deleteTrans(tablename, conds))
                        {
                            CourseLoad("");
                            enabledNewTransbutton();
                            clearControls();
                            MessageBox.Show("Record deleted successfully");
                        }
                    }
                }
                else
                {
                    return;
                }
                con.conclose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }