Convert.ToInt16(row["INT"] as Int16?); returns 0 and (Int16)row["INT"]; throws an exception Specified cast is not valid.
private DataTable GetTableWithValue()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("INT", typeof(Int32)),
new DataColumn("STRING", typeof(string )),
new DataColumn("DATETIME", typeof(DateTime )),
new DataColumn("BOOLEAN", typeof(bool)),
});
dt.Rows.Add(dt.NewRow());
dt.Rows[0][0] = 10;
dt.Rows[0][1] = "Babu";
dt.Rows[0][2] = DateTime.Now;
dt.Rows[0][3] = true;
return dt;
}
dt = GetTableWithValue();
row = dt.Rows[0];
int? INT = (row["INT"] as int?); //10
Int16? INT16 = Convert.ToInt16(row["INT"] as Int16?); //0
Int16 = (Int16)row["INT"]; //Specified cast is not valid.