Beispiel #1
0
        // unos promjena u bazu, bilo izmjena na nekom od redu ili dodavanje novog u tabelu
        private void btnPotvrdi_Click(object sender, EventArgs e)
        {
            var properties = property.GetType().GetProperties();

            foreach (var item in flpDetaljno.Controls)
            {
                if (item.GetType() == typeof(TextBoxControl))
                {
                    TextBoxControl input = item as TextBoxControl;

                    if (input.Name == "DuzinaTrajanja" || input.Name == "VrijemePrikazivanja")
                    {
                        TimeSpan pom = new TimeSpan(0, 0, 0);
                        if (TimeSpan.TryParse(input.GetTextBox(), out pom))
                        {
                            TimeSpan     valueT      = TimeSpan.ParseExact(input.GetTextBox(), "c", null);
                            PropertyInfo myPropertyT = properties.Where(x => input.Name == x.Name).FirstOrDefault();
                            myPropertyT.SetValue(property, Convert.ChangeType(valueT, myPropertyT.PropertyType));
                        }
                        else
                        {
                            MessageBox.Show("Vrijeme nije u ispravnom formatu!" + input.GetTextBox());
                            input.BackColor = Color.Red;
                            return;
                        }
                        if (input.Name == "Lozinka" && input.GetTextBox().Length <= 6)
                        {
                            MessageBox.Show("Lozinka mora biti bar 6  karatktera duga!");
                            return;
                        }
                    }


                    else
                    {
                        PropertyInfo myProperty = properties.Where(x => input.Name == x.Name).FirstOrDefault();
                        string       value      = input.GetTextBox();
                        if (value.Trim() == "" && myProperty.GetCustomAttribute <MandatoryDataAttribute>() != null)
                        {
                            MessageBox.Show("Polje " + input.Name + " je obavezno, popunite ga! ");
                            input.BackColor = Color.Red;
                            return;
                        }
                        if (input.Name == "Pol" && (value == "M" || value == "Ž"))
                        {
                            continue;
                        }
                        else if (input.Name == "Pol" && (value != "M" || value != "Ž"))
                        {
                            MessageBox.Show("Polje pol mora biti jedan karakter (M ili Ž)");
                            return;
                        }

                        else
                        {
                            myProperty.SetValue(property, Convert.ChangeType(value, myProperty.PropertyType));
                            input.BackColor = Color.FromArgb(38, 38, 38);
                        }
                    }
                }

                //Marko J. Pokusaji rjesavanja dopune svih polja prilikom unosa i updatea
                else if (item.GetType() == typeof(RichTextBoxControl))
                {
                    RichTextBoxControl input      = item as RichTextBoxControl;
                    string             value      = input.GetVrijednost();
                    PropertyInfo       myProperty = properties.Where(x => input.Name == x.Name).FirstOrDefault();
                    if (myProperty.GetCustomAttribute <MandatoryDataAttribute>() != null && input.GetVrijednost().Trim() == "")
                    {
                        MessageBox.Show("Polje " + input.Name + " je obavezno, popunite ga! ");
                        input.BackColor = Color.Red;
                        return;
                    }
                    else
                    {
                        myProperty.SetValue(property, Convert.ChangeType(value, myProperty.PropertyType));
                    }
                    input.BackColor = Color.FromArgb(38, 38, 38);
                }
                else if (item.GetType() == typeof(DateTimeControl))
                {
                    DateTimeControl input      = item as DateTimeControl;
                    string          value      = input.GetVrijednost();
                    PropertyInfo    myProperty = properties.Where(x => input.Name == x.Name).FirstOrDefault();
                    if (myProperty.GetCustomAttribute <MandatoryDataAttribute>() != null && input.GetVrijednost().Trim() == "")
                    {
                        MessageBox.Show("Polje " + input.Name + " je obavezno, popunite ga! ");
                        input.BackColor = Color.Red;
                        return;
                    }
                    else
                    {
                        myProperty.SetValue(property, Convert.ChangeType(value, myProperty.PropertyType));
                    }
                    input.BackColor = Color.FromArgb(38, 38, 38);
                }
                else if (item.GetType() == typeof(NumericUpDownControl))
                {
                    NumericUpDownControl input = item as NumericUpDownControl;
                    string       value         = input.GetValue().ToString().Trim();
                    PropertyInfo myProperty    = properties.Where(x => input.Name == x.Name).FirstOrDefault();
                    if (myProperty.GetCustomAttribute <MandatoryDataAttribute>() != null && input.GetValue().ToString().Trim() == "")
                    {
                        MessageBox.Show("Polje " + input.Name + " je obavezno, popunite ga! ");
                        input.BackColor = Color.Red;
                        return;
                    }
                    else
                    {
                        myProperty.SetValue(property, Convert.ChangeType(value, myProperty.PropertyType));
                    }
                    input.BackColor = Color.FromArgb(38, 38, 38);
                }
                else if (item.GetType() == typeof(UserLookUpControl))
                {
                    UserLookUpControl input = item as UserLookUpControl;
                    string            value = input.getKey().ToString();
                    int provjera            = 0;
                    if (value.Trim() == "" || int.TryParse(value, out provjera) == false)
                    {
                        MessageBox.Show("Polje " + input.Name + " je obavezno, popunite ga! ");
                        input.BackColor = Color.Red;
                        return;
                    }
                    else
                    {
                        PropertyInfo myProperty = properties.Where(x => input.Name == x.Name).FirstOrDefault();
                        myProperty.SetValue(property, Convert.ChangeType(value, myProperty.PropertyType));
                        input.BackColor = Color.FromArgb(38, 38, 38);
                    }
                }
                else if (item.GetType() == typeof(CheckBoxControl))
                {
                    CheckBoxControl input      = item as CheckBoxControl;
                    bool            value      = input.GetValue();
                    PropertyInfo    myProperty = properties.Where(x => input.Name == x.Name).FirstOrDefault();
                    myProperty.SetValue(property, Convert.ChangeType(value, myProperty.PropertyType));
                }
            }

            if (state == StateEnum.Create)
            {
                if (DialogResult.Yes == (MessageBox.Show("Da li ste sigurni da zelite da dodate novi red?", "Poruka!",
                                                         MessageBoxButtons.YesNo, MessageBoxIcon.Information)))
                {
                    SqlHelper.ExecuteNonQuery(SqlHelper.GetConnectionString(), CommandType.Text, property.GetInsertQuery(), property.GetInsertParameters().ToArray());
                }
                else
                {
                    state = StateEnum.Preview;
                }
            }
            else if (state == StateEnum.Update)
            {
                if (DialogResult.Yes == (MessageBox.Show("Da li ste sigurni da zelite da izmjenite odabrani red?", "Poruka!",
                                                         MessageBoxButtons.YesNo, MessageBoxIcon.Information)))
                {
                    SqlHelper.ExecuteNonQuery(SqlHelper.GetConnectionString(), CommandType.Text, property.GetUpdateQuery(), property.GetUpdateParameters().ToArray());
                }
                else
                {
                    state = StateEnum.Preview;
                }
            }

            UcitajDGV(property);
            state = StateEnum.Preview;
            txtPretraga.Enabled = true;

            panelDugmici.Visible = false;
        }