Exemple #1
0
        public void SportType_CheckDataBeforeSave()
        {
            //Create a SportType object with data.
            aSportType.LoadData(-1, "Weird Sport", 0, "any kind of weird sport", 85,1);
            int res = aSportType.CheckData(connString);
            Assert.AreEqual(1, res); //Data are ok because the type of sport isn´t in de BD.

            //Save the data into BD, and check again.
            aSportType = aSportType.SaveData(connString);
            Int64 sportTypeIDUsed = aSportType.SportTypeID;
            Int64 userIDUsed = aSportType.UserID;
            res = aSportType.CheckData(connString);
            Assert.AreEqual(-1, res); //Data are not ok, SportTypeName is in de BD.

            //Create a SportType object with data:
            aSportType.LoadData(-1, "Weird Sport2", -10, "any kind of weird sport", 85,1);
            res = aSportType.CheckData(connString);
            Assert.AreEqual(-2, res); //Data are not ok, parentSportID does not exist on SportTypes

            //Create a SportType object with data:
            aSportType.LoadData(-1, "", 0, "any kind of weird sport", 85,1);
            res = aSportType.CheckData(connString);
            Assert.AreEqual(0, res); //Data are not ok, SportTypeName is empty.

            //Create a SportType object with data:
            aSportType.LoadData(-1, "Weird Sport2", 0, "any kind of weird sport", -100,1);
            res = aSportType.CheckData(connString);
            Assert.AreEqual(-3, res); //Data are not ok, userID not in data base.

            //Deleting object saved first.
            aSportType = aSportType.FindSportTypeByID(connString);
            res = aSportType.Delete(connString);
            Assert.AreEqual(1, res);
        }
Exemple #2
0
        private void tsBtnSave_Click(object sender, EventArgs e)
        {
            //Verify all data is correct.
            if (txtParentSportTypeID.Text == "")
                txtParentSportTypeID.Text = "0";
            SportTypes tempData = new SportTypes(sportTypeIDToUpdate,txtSportTypeName.Text,Convert.ToInt64(txtParentSportTypeID.Text),txtMemo.Text,userIDWorking,Convert.ToDouble(txtDistForPace.Text));
            int verifyResult = tempData.CheckData(connString);

            //if update and user find, verify must be ok
            if ((sportTypeIDToUpdate != -1) && (verifyResult == -1))
                verifyResult = 1;

            //if data verification is ok, then we can save.
            if (verifyResult == 1)
            {
                int position = 0;
                if (sportTypeIDToUpdate == -1)
                { //New user
                    //Takes position
                    position = dgvSTypes.RowCount;

                    //Load data into aUSer
                    Global.sportTypeUsed.LoadData(-1, txtSportTypeName.Text, Convert.ToInt64(txtParentSportTypeID.Text), txtMemo.Text, userIDWorking, Convert.ToDouble(txtDistForPace.Text));

                    //save data into BD
                    Global.sportTypeUsed = Global.sportTypeUsed.SaveData(connString);
                }
                else
                {//Update existing user

                    //Take actual position
                    position = dgvSTypes.CurrentRow.Index;

                    //Load data into aUSer
                    Global.sportTypeUsed.LoadData(sportTypeIDToUpdate, txtSportTypeName.Text, Convert.ToInt64(txtParentSportTypeID.Text), txtMemo.Text, userIDWorking, Convert.ToDouble(txtDistForPace.Text));

                    //save data into BD
                    Global.sportTypeUsed = Global.sportTypeUsed.UpdateData(connString);
                    //aSportType = aSportType.up
                }
                //Reload data into grid
                LoadDataInGrid(dgvSTypes,userIDWorking);

                //Goes to the position
                dgvSTypes.CurrentCell = dgvSTypes[0, position];

                Global.sportTypeUsed.SportTypeID = Convert.ToInt32(dgvSTypes[0, dgvSTypes.CurrentRow.Index].Value);
                Global.sportTypeUsed.UserID = userIDWorking;

                //Load data into aUser
                Global.sportTypeUsed = Global.sportTypeUsed.FindSportTypeByID(connString);

                //Loads data into boxes
                LoadDataInBoxes(TrainItLibrary.Global.sportTypeUsed);

                //Enable normal mode.
                setNormalMode();

                //Reload Data into combobox
                this.sportTypesTableAdapter1.FillBy(this.trainITDataSet.SportTypes, userIDWorking);
            }
            else //There is an error in data to save.
            {
                switch (verifyResult)
                {
                    case 0: //SportTypeName not present
                        MessageBox.Show("Falta el nombre del tipo de deporte.", "Atención", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtSportTypeName.Focus();
                        break;
                    case -1: //SportTypeName exist on BD.
                        MessageBox.Show("Ya existe ese tipo de deporte en la base de datos.", "Atención", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtSportTypeName.Focus();
                        break;
                    case -2: //ParentSportID does not exist on BD and is different to 0(root)
                        MessageBox.Show("La familia a la que pertenece el tipo de deporte no existe en la base de datos.", "Atención", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtSportTypeName.Focus();
                        break;

                }
            }
        }