0

In View I have Combobox:

<ComboBox Name="Typ" Grid.Column="5" Grid.Row="0"  Margin="3 5 3 5" 
    SelectedItem="{Binding Path=typ, UpdateSourceTrigger=PropertyChanged}">

    <ComboBoxItem Content="ATMA"/>
    <ComboBoxItem Content="NTP" />
    <ComboBoxItem Content="ATMA" />

</ComboBox>

I want get Selected ComboBoxItem in ViewModel and save it to database

public string typ
{
    get
    {
        return item.typ;
    }
    set
    {
        if (value != item.typ)
        {
            item.typ = value;
            OnPropertyChanged(() => typ);
        }
    }
}

When I want save item.typ to database, I got error that I have wrong type of typ. In database type typ column is string.

Code that save to database

 public override void save()
        {

            DialogResult dialogResult = MessageBox.Show("Czy napewno chcesz dodać obiekt?", "Powiadomienie", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                item.stan = "1";


                komunikat.dodaj();
                ATMAEntites.Przedstawiciele.Add(item);


                ATMAEntites.SaveChanges();
                load();
            }
            else if (dialogResult == DialogResult.No)
            {

            }

        }

I think, that data type that i get in typ from ComboBoxItem is different that string, but when i try Convert to string, error is this same

public string typ
{
    get
    {
        return item.typ.ToString();
    }
    set
    {
        if (value != item.typ)
        {
            item.typ = value;
            OnPropertyChanged(() => typ);
        }
    }
}
Adam
  • 125
  • 11
  • Can you share the exact error message? Also, please show the code you're using to save to the database. – haldo Feb 04 '20 at 18:52
  • This post is not a wpf XAML issue, for XAML/wpf is doing its job. You may want to delete this post and post a database question instead. – ΩmegaMan Feb 04 '20 at 18:53
  • System.Data.Entity.Validation.DbEntityValidationException: „Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.” – Adam Feb 04 '20 at 18:58

1 Answers1

1

For the error that you are saying on the comments below, it is probably the field you are trying to save on the database. Or, maybe the field, has a type of constraint on database. Try to look at the exception that you are having, and see exactly what it is.

It is difficult to say exactly what it is, if you don`t post the real exception for us.

I am posting a question on stackoveflow saying about that error that you have. See if that could help you too.

That error that you are having, its like that one here Validation failed for one or more entities. See 'EntityValidationErrors' property for more details

Community
  • 1
  • 1