I wrote a class CustomDataTable
that inherits DataTable
but adds the function ToHtml()
CustomDataTable
public static CustomDataTable : DataTable {
public Table ToHtml() {
Table tbl;
/*
logic for converting each row in the DataTable to HTML TableRow
*/
return tbl;
}
}
Usage:
public static CustomDataTable getTable(String sql) {
SqlConnection con = new SqlConnection(connection_string);
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return (CustomDataTable)ds.Tables[0]; // this line triggers the exception
// Unable to cast object of type 'System.Data.DataTable' to type 'MyProject.CustomDataTable'
}
Why did the cast fail?