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