Click here to Skip to main content
15,559,491 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

i want to write birthday for users in mysql database.
i create entity framework to my mysql server.
However,
when i try to add birthday i got

An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in TarifinizBizde.dll but was not handled in user code

Additional information: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

this exception.

and i think the mysql datetime format is:"yyyy-mm-dd"
and entity framework date time format is:"dd-mm-yyyy"

how can i change mysql or entity framework datetime?

C#
usr.DogumTarihi = Convert.ToDateTime(txtDogum.Text);


What I have tried:

using globalization tostring("yyyy-mm-dd")
but they are not worked
Posted
Updated 1-Jun-16 21:24pm
Comments
Richard Deeming 1-Jun-16 11:26am    
And what does the EntityValidationErrors property tell you?
Dave Kreskowiak 1-Jun-16 12:49pm    
There's no such thing as a "format" for a DateTime. A format is only applicable in the UI, not in the code or database.

Hi,

Can you try it like below:

C#
bool parsed = false;
DateTime outDate;
parsed = DateTime.TryParseExact(inputDate, "dd-MM-yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out outDate);

if(parsed)
{
    usr.DogumTarihi = outDate.ToString("yyyy-MM-dd");
}
else
{
    // Invalid date has been passed
}


But remember you will need to use System.Globalization namespace to use this.

Please let me know.

Thanks.
 
Share this answer
 
First, find out what are the errors you are getting. Then come back with the result. This piece of code might help you finding out the errors:

C#
try
{
    // put your code here for saving or updating record.
}
catch (DbEntityValidationException ex)
{
StringBuilder sbError = new StringBuilder();
    foreach (var objError in ex.EntityValidationErrors)
    {
sbError.AppendFormat("Entity Type \"{0}\" in state \"{1}\" has the following errors:", objError.Entry.Entity.GetType().Name, objError.Entry.State);
        foreach (var validationError in objError.ValidationErrors)
        {
sbError.AppendFormat("Property: \"{0}\", Error: \"{1}\"",
                validationError.PropertyName, validationError.ErrorMessage);           
        }
    }
string validationErrors = sbError.ToString();
    //put you debugger here and see what is the value of "validationErrors"
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900