Could you please look on the code below and say why this code doesn’t throw an exception when the column value is null?
DataTable table = new DataTable();
table.Columns.Add("PreviewHtml");
table.Rows.Add(new object[] { "aksdhaskldh" });
table.Rows.Add(new object[] { "129836 128o tagjk 1782 3" });
table.Rows.Add(new object[] { null });
table.Rows.Add(new object[] { "1278o36 " });
foreach (DataRow r in table.Rows)
{
Console.WriteLine(r["PreviewHtml"].ToString());
}
It’s because
DBNull.ToStringreturns an empty string.DataColumn’s
AllowDBNullproperty is set to true by default, otherwise you could not addnullvalues.Nullvalues are converted toDBNull.Value, AutoIncrement columns are also incremented whennullis passed.