简体   繁体   中英

Error: String was not recognized as a valid DateTime.

Error: String was not recognized as a valid DateTime. below is the stack trace-

" at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)\\r\\n at System.Convert.ToDateTime(String value)\\r\\n at ConsoleApplication10.Program.b_ 1(<>f _AnonymousType0 1 a) in C:\\\\Documents and Settings\\\\xxxxdev\\\\My Documents\\\\Visual Studio 2008\\\\Projects\\\\ConsoleApplication10\\\\ConsoleApplication10\\\\Program.cs:line 23\\r\\n at System.Linq.Enumerable.WhereSelectListIterator 2.MoveNext()\\r\\n at ConsoleApplication10.Program.Main(String[] args) in C:\\Documents and Settings\\hj81dev\\My Documents\\Visual Studio 2008\\Projects\\ConsoleApplication10\\ConsoleApplication10\\Program.cs:line 28\\r\\n at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\\r\\n at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\\r\\n at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\\r\\n at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\\r\\n at System.Threading.ExecutionContext.Run(ExecutionContext exe cutionContext, ContextCallback callback, Object state)\\r\\n at System.Threading.ThreadHelper.ThreadStart()"

this is my code. in the sql server database my dateofbirth field is varbinary type

class Program
    {
        static void Main(string[] args)
        {
            var customerProfileGuid = new Guid("35D02589-C5FA-437D-B661-000215C68584");
            using (CustomerProfileEntities context = new CustomerProfileEntities())
            {
                var test = from x in context.CustomerProfile
                           where x.CustomerProfileId == customerProfileGuid
                           select new { x };

                var customerData = test.ToList();
                var customerResult = (from a in customerData
                                      select new Profile
                                      {
                                          DateOfBirth =Convert.ToDateTime(Encoding.UTF8.GetString(a.x.DateOfBirth)) //getting error here
                                      });

                foreach (var profile in customerResult)
                {
                    var profileData = profile;
                }
            }

        }
    }
    public class Profile
    {
        private DateTime dateOfBirthField;

        [System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
        public DateTime DateOfBirth
        {
            get
            {
                return this.dateOfBirthField;
            }
            set
            {
                this.dateOfBirthField = value;
            }
        }
    }

Please do the needful

set cultureinfo to be sure, which format is used

var cultureInfo = new CultureInfo(yourCulture);
Thread.CurrentThread.CurrentCulture = cultureInfo;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM