Here i want to cast ToDateTime() into ToShortDateString().
During casting required column value is null. Then i got problem of DBNull as below:
System.InvalidCastException: Object cannot be cast from DBNull to other types.
To solve this problem i follow the below code for casting the Date into ToShortDate.
Here dtEmp refers to Datatable.
Hence problem is solved.
During casting required column value is null. Then i got problem of DBNull as below:
System.InvalidCastException: Object cannot be cast from DBNull to other types.
To solve this problem i follow the below code for casting the Date into ToShortDate.
Here dtEmp refers to Datatable.
Code is as follows:
if (dtEmp.Rows[0]["Anniversary_Date"] != DBNull.Value) { txtAnniversaryDate.Text = Convert.ToDateTime(dtEmp.Rows[0]["Anniversary_Date"]).ToShortDateString(); }OR
txtAnniversaryDate.Text = dtEmp.Rows[0]["Anniversary_Date"] != DBNull.Value ? Convert.ToDateTime(dtEmp.Rows[0]["Anniversary_Date"]).ToShortDateString() : "";
Hence problem is solved.
Conversion Conversion Emoticon Emoticon