private void buttonSave_Click(object sender, EventArgs e)
        {
            realEsate = db.RealEstate.Single(x => x.Id == realEsate.Id);

            if (String.IsNullOrWhiteSpace(textBoxStreet.Text) ||
                String.IsNullOrWhiteSpace(textBoxPrice.Text) ||
                String.IsNullOrWhiteSpace(textBoxDescription.Text))
            {
                MessageBox.Show("Fields can not be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                realEsate.Street      = textBoxStreet.Text;
                realEsate.Description = textBoxDescription.Text;
                realEsate.Rooms       = Convert.ToInt32(numericUpDownRooms.Value);
                realEsate.Floor       = Convert.ToInt32(numericUpDownFloor.Value);
            }

            Cities city;

            Enum.TryParse(comboBoxCity.SelectedValue.ToString(), out city);
            realEsate.City = city;

            if (FormRealEstateCreator.CheckPrice(textBoxPrice.Text))
            {
                realEsate.Price = Convert.ToInt32(textBoxPrice.Text);
            }
            else
            {
                MessageBox.Show("Invalid price value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            bool hasPhoto = false;
            int  photoIdx = 0;

            foreach (var arr in photoSlider)
            {
                if (arr != null)
                {
                    if (photoSlider[photoIdx++].GetUpperBound(0) > 1)
                    {
                        hasPhoto = true;
                    }
                }
            }
            if (!hasPhoto)
            {
                MessageBox.Show("Pick at least one photo", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            realEsate.PhotoSlider = ImageManip.PhotoSliderToByteArray(photoSlider);

            db.SaveChanges();

            textBoxStreet.Text       = String.Empty;
            textBoxDescription.Text  = String.Empty;
            textBoxPrice.Text        = "0";
            numericUpDownRooms.Value = 1;
            numericUpDownFloor.Value = 1;
            pictureBoxSlider.Image   = null;
            photoNumber           = 0;
            labelPhotoNumber.Text = "1/5";
        }
Ejemplo n.º 2
0
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(textBoxStreet.Text) ||
                String.IsNullOrWhiteSpace(textBoxPrice.Text) ||
                String.IsNullOrWhiteSpace(textBoxDescription.Text))
            {
                MessageBox.Show("Fields can not be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                realEstate.Street      = textBoxStreet.Text;
                realEstate.Description = textBoxDescription.Text;
                realEstate.Rooms       = Convert.ToInt32(numericUpDownRooms.Value);
                realEstate.Floor       = Convert.ToInt32(numericUpDownFloor.Value);
            }

            Cities city;

            Enum.TryParse(comboBoxCity.SelectedValue.ToString(), out city);
            realEstate.City = city;

            if (CheckPrice(textBoxPrice.Text))
            {
                realEstate.Price = Convert.ToInt32(textBoxPrice.Text);
            }
            else
            {
                MessageBox.Show("Invalid price value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            bool hasPhoto = false;

            for (int i = 0; i < photoSlider.Length; ++i)
            {
                if (photoSlider[i] != null)
                {
                    if (photoSlider[i].GetUpperBound(0) > 1)
                    {
                        hasPhoto = true;
                    }
                }
            }
            if (!hasPhoto)
            {
                MessageBox.Show("Pick at least one photo", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            realEstate.PhotoSlider = ImageManip.PhotoSliderToByteArray(photoSlider);

            realEstate.Client = db.Clients.FirstOrDefault();

            db.RealEstate.Add(realEstate);
            db.SaveChanges();

            textBoxStreet.Text       = String.Empty;
            textBoxDescription.Text  = String.Empty;
            textBoxPrice.Text        = "0";
            numericUpDownRooms.Value = 1;
            numericUpDownFloor.Value = 1;
            pictureBoxSlider.Image   = null;
            photoByteArr             = null;
            photoSlider           = new byte[5][];
            photoNumber           = 0;
            labelPhotoNumber.Text = "1/5";
        }