[英]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 _AnonymousType01 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.