I'm trying to get the date of birth of employees from the DataRow
s of the DataTable
, but I'm getting the exception:
String was not recognized as a valid DateTime.
Please help me to get the value of type DateTime
from the DataRow
. The following is the my code.
List employeeList = new List();
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);
}
employeeObject.EmployeeDateOfBirth= Convert.ToDateTime(dr["empDOB"]);
Try with this and replace "yyyy-MM-dd" with your desired format.
DateTime.ParseExact(dr["empDOB"].toString(), "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With