I used the following extension method for my DataRow types:
public static string ColumnIsNull(this DataRow row, string colName, string defaultValue = "") { string val = defaultValue; if (row.Table.Columns.Contains(colName)) { if (row[colName] != DBNull.Value) { val = row[colName]?.ToString(); } } return val; }
using:
MyControl.Text = MyDataTable.Rows[0].ColumnIsNull("MyColumn"); MyOtherControl.Text = MyDataTable.Rows[0].ColumnIsNull("AnotherCol", "Doh! I'm null");
First, I check for the existence of a column, because if none of the query results has a nonzero value for this column, the DataTable will not even create this column.
Denis M. Kitchen Dec 20 '18 at 19:11 2018-12-20 19:11
source share