0

I initialized dob and doj in sql as DateTime. But unable to typecast here to string.

        AllEmployee allemp = new AllEmployee();
        allemp.EmpID = Convert.ToInt32(tbempid.Text);
        allemp.Name = tbname.Text;
        allemp.Department = tbdepartment.Text;
        allemp.DOB = Convert.ToDateTime(tbdob.Text); 
        allemp.Experience = tbexperience.Text;
        //allemp.DOB = Convert.ToDateTime(tbdob.Text);
        allemp.DOJ = Convert.ToDateTime(tbdoj.Text);

        //store data in database
        EMSEntities db = new EMSEntities();
        db.AllEmployees.Add(allemp);
        db.SaveChanges();
        lblsuccess.Text = "Data added successfully";
  • Hi @rajus1996 this [post](https://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format) might help. To convert a string to DateTime you can use DateTime.ParseExact(). – Manik Jan 30 '23 at 10:32
  • Do you have an example of the input date string you try to parse? What is the format (mm/dd/yyyy?) and does it match the settings of the machine it is running on? Also, empty strings fail when using `Convert.ToDateTime()`. – Frank Witte Feb 03 '23 at 13:18

0 Answers0