The example that I have provided is little bit complicated but you can get an idea about checking the null values in C# programming language. I know the code is bit large but will definitely help you.
Code:
static private void WorkWithSqlNulls()
{
DataTable table = new DataTable();
DataColumn idColumn =
table.Columns.Add("Roll", typeof(SqlInt32));
DataColumn descColumn =
table.Columns.Add("Information", typeof(SqlString));
DataRow nRow = table.NewRow();
nRow["Roll"] = 123;
nRow["Information"] = "Side Mirror";
table.Rows.Add(nRow);
nRow = table.NewRow();
nRow["Roll"] = SqlInt32.Null;
nRow["Information"] = SqlString.Null;
table.Rows.Add(nRow);
SqlBoolean isColumnNull = false;
SqlInt32 rollValue = SqlInt32.Zero;
SqlString informationValue = SqlString.Null;
foreach (DataRow row in table.Rows)
{
rollValue = (SqlInt32)row["Roll"];
informationValue = (SqlString)row["Information"];
isColumnNull = rollValue.IsNull;
Console.Write("isColumnNull={0}, Roll={1}, Information={2}",
isColumnNull, rollValue, informationValue);
Console.WriteLine();
}
If you use the above code, it will generate a DataTable with two columns defined as SqlInt32 and SqlString. The code adds one row of known values, one row of null values and then iterates through the DataTable, assigning the values to variables and displaying the output in the console window.
Bookmarks