Example #1
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);
            }
        }