0

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_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.WhereSelectListIterator2.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()"

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

user995099
  • 129
  • 1
  • 3
  • 12

1 Answers1

0

set cultureinfo to be sure, which format is used

var cultureInfo = new CultureInfo(yourCulture);
Thread.CurrentThread.CurrentCulture = cultureInfo;
maxlego
  • 4,864
  • 3
  • 31
  • 38