I am having trouble saving dates to my MySQL database (code below)

C#

    var abook = db.egw_addressbook.Local.FirstOrDefault(o => o.contact_id == 16875);
    abook.org_name = mvm.org_name;
    abook.hex_srv_start = mvm.hex_srv_start;

    db.SaveChanges();
    MessageBox.Show("Saved");

XAML

<DatePicker x:Name="Txthex_srv_start" SelectedDate="{Binding hex_srv_start}" HorizontalAlignment="Left" Height="26" Margin="10,55,0,0" VerticalAlignment="Top" Width="111" />

If I make any changes to the date, I get the following error:

Error: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. - -2146232032

Any ideas?

Recommended Answers

All 7 Replies

What does the EntityValidationErrors property contain?

Thanks for helping me out (I'm new to this and still strugling)

Property: hex_srv_start Error: The field hex_srv_start must be a string or array type with a maximum length of '12'.

When you set up EF, did you use Code First or Database First design methods?
It looks like hex_srv_start is the wrong variable type for what you're trying to enter. Is the variable definitely a string type? If so, it may be that you're recording too much data.

Example;
21st Jan 2014 has 13 characters
21/01/2014 00:00:00 has 19 characters

It was using "Database first". I have just opened up the MySQL table and it's of type - VARCHAR(12). When I look at egw_addressbook.cs, it has the syntax as: public string hex_srv_start { get; set; }.

Would setting the format of the DatePicker object do the trick (I am only guessing)?

It looks like I need to format the DatePicker within the XAML, as the field that I am trying to store the value into wants: YYYY-MM-dd and the DatePicker is providing the format as MM/dd/YYYY 12:00:00 AM.

Any ideas as to how I can convert this within the XAML (if indeed that is where I do it).

You can use a Binding Converter to get what you need. It will allow you to do pretty much anything to the data before it is used.

Very useful, very powerful and not too hard to get the hang of :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.