简体   繁体   English

错误:字符串未被识别为有效的DateTime。

[英]Error: String was not recognized as a valid DateTime.

Error: String was not recognized as a valid DateTime. 错误:字符串未被识别为有效的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 “位于System.DateTimeParse.Parse(String s,DateTimeFormatInfo dtfi,DateTimeStyles样式)\\ r \\ n位于System.Convert.ToDateTime(字符串值)\\ r \\ n位于ConsoleApplication10.Program.b_1 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 C:\\ Documents and Settings \\ hj81dev \\ My Documents \\ Visual Studio 2008 \\ Projects \\ ConsoleApplication10 \\ ConsoleApplication10 \\ Program.cs:line 28 \\中的ConsoleApplication10.Program.Main(String [] args)处的.MoveNext()\\ r \\ n r \\ n在System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)\\ r \\ n在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)\\ r \\ n在Microsoft.VisualStudio.HostingProcess .HostProc.RunUsersAssembly()\\ r \\ n在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)\\ r \\ n在System.Threading.ExecutionContext.Run(ExecutionContext exe cutionContext, ContextCallback callback, Object state)\\r\\n at System.Threading.ThreadHelper.ThreadStart()" cutionContext,ContextCallback回调,对象状态)\\ r \\ n位于System.Threading.ThreadHelper.ThreadStart()“

this is my code. 这是我的代码。 in the sql server database my dateofbirth field is varbinary type 在SQL Server数据库中,我的dateofbirth字段是varbinary类型

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 设置cultureinfo以确保使用哪种格式

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误消息为“字符串未被识别为有效的DateTime。” - Error Message as “String was not recognized as a valid DateTime.” 获取错误“字符串未被识别为有效的DateTime”。 - Getting error “String was not recognized as a valid DateTime.” 迄今为止的字符串解析错误“字符串未被识别为有效的DateTime。” - String to date parsing error “String was not recognized as a valid DateTime.” “字符串未被识别为有效的DateTime。”Window 7计算机环境中出现错误 - “String was not recognized as a valid DateTime.” Error occur in Window 7 computer environment 无法将字符串识别为有效的DateTime。 :仅服务器错误 - String was not recognized as a valid DateTime. : Error only in Server 对于某些确切的日期,发生了“字符串未被识别为有效的DateTime。”错误 - "String was not recognized as a valid DateTime.” error occurs for some exact dates 错误“字符串未被识别为有效的日期时间。” 在 c# - error “String was not recognized as a valid DateTime.” in c# 字符串日期未被识别为有效的日期时间。 - String date was not recognized as a valid DateTime.' 无法将字符串识别为有效的DateTime。 引发异常 - String was not recognized as a valid DateTime. Throws an Exception 字符串未被识别为有效的日期时间。 将字符串转换为日期时间 - String was not recognized as a valid DateTime. Converting string to DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM