2011-11-28 45 views
0

錯誤:字符串未被識別爲有效的DateTime。下面 是堆棧trace-錯誤:字符串未被識別爲有效的DateTime。

" 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 executionContext, ContextCallback callback, Object state)\r\n at System.Threading.ThreadHelper.ThreadStart()"

這是我的代碼。在SQL Server數據庫中我的出生日期字段是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; 
      } 
     } 
    } 

請做要緊

+0

你想傳遞什麼樣的價值約會? – Oded

+1

你是否檢查過Encoding.UTF8.GetString(a.x.DateOfBirth)實際返回的內容? –

+0

斷點行並檢查你正試圖通過的值 – Purplegoldfish

回答

0

設置的CultureInfo可以肯定,使用哪種格式

var cultureInfo = new CultureInfo(yourCulture); 
Thread.CurrentThread.CurrentCulture = cultureInfo; 
相關問題