Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to get the date of birth of employees from data row of the datatable but getting the exception " String was not recognized as a valid DateTime".
Please help me to get the value of type date time from data row.


The following is the my code.


List<employeebo> employeeList = new List<employeebo>();
foreach (DataRow dr in dt.Rows)
{
DateTime t = DateTime.Now;
employeeObject.EmployeeID = Convert.ToInt64(dr["empId"]);
employeeObject.EmployeeFirstName = Convert.ToString(dr["empFirstName"]);
employeeObject.EmployeeMiddleName = Convert.ToString(dr["empMiddleName"]);
employeeObject.EmployeeLastName = Convert.ToString(dr["emptLastName"]);
employeeObject.EmployeeGenderStr = Convert.ToString(dr["empGender"]);
employeeObject.EmployeeDateOfBirth = Convert.ToDateTime(dr["empDOB"]);
//employeeObject.EmployeeDateOfBirth = DateTime.ParseExact(dr["empDOB"].ToString().Replace(";", " "), "m/d/yyyy hh:mm:ss", CultureInfo.InvariantCulture);// DateTime.Parse(dr["empDOB"].ToString());
// employeeObject.EmployeeDateOfBirth = Convert.ToDateTime(dr["empDOB"].ToString().Replace(";", " "), System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat); ;
employeeObject.EmployeeContactno = Convert.ToDouble(dr["empContactNo"]);
employeeObject.EmployeeEmailId = Convert.ToString(dr["empEmailId"]);
employeeObject.EmployeeAddress = Convert.ToString(dr["empAddress"]);
employeeObject.EmployeeDesignation = Convert.ToString(dr["empDesgnation"]);
employeeList.Add(employeeObject);
Posted

Use DateTime.TryParse instead of DateTime.Parse or DateTime.ParseExact; This won't give you an exception when it fails.
 
Share this answer
 
Comments
santoshkumar413 25-Jul-14 6:32am    
Hi,

I have tried even DateTime.Parse, But not solving the issue

1. Actually what i am doing is, i am getting the records from commaseperatedvalue file(.csv) and binding it to datatable(Converting the datareader values into datatable and datatable is becoming into read only values mode), and converting datatable into list of entities and sending the entities to storeproc for insert or update.
2. The DateTime While i am binding to the csv file is converting from 1/1/1900 00:00:00 To 1/1/1900;12:00:00;AM and same is binding to the Datatable.
legendhunter64 25-Jul-14 7:00am    
If the issue was the exception "String is not a valid DateTime", then use DateTime.TryParse. If the issue was the actual format, try splitting the string by ";" and getting the first token.
santoshkumar413 25-Jul-14 7:16am    
Hi, Thanks for the reply, I have added value into the column in csv file of format 1/1/1990 11:20:12 Am with out any ';' and tried to access employeeObject.EmployeeDateOfBirth = Convert.ToDateTime(dr["empDOB"]); but same exception exists
legendhunter64 25-Jul-14 8:16am    
DateTime.TryParse(dr["empDOB"].ToString(), out employeeObject.EmployeeDateOfBirth);
C#
 dr["empDOB"]="1/1/1900 12:34:56"
Convert.ToDateTime(dr["empDOB"].ToString())

This should work just fine. The only reason the exception is thrown is due to the input. So find out what is getting stored in dr["empDOB"].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900