Assign Null value to the Integer Column in the DataTable
A null/empty string is in the wrong format; you would need to detect that scenario and compensate:
DR["CustomerID"] = string.IsNullOrWhiteSpace(text)
? DBNull.Value : (object)Convert.ToInt32(text);
DR["CustomerID"] = !string.IsNullOrEmpty(TextBox1.Text)
? Convert.ToInt32(TextBox1.Text)
: DBNull.Value;
But you should check also that the value is a valid integer:
int value;
if(int.TryParse(TextBox1.Text, out value))
{
DR["CustomerID"] = value;
}
else
{
DR["CustomerID"] = DBNull.Value;
}
you could do it like that:
DR["CustomerID"] = string.IsNullOrEmpty(TextBox1.Text) ?
null : Convert.ToInt32(TextBox1.Text);