Replace DBNull with Zero while exporting to CSV or Excel file
Exporting data from DataTale to CSV file is explained in this article.
When the data contains NULL values, rows in CSV or Excel file prints nothing. In order to replace DBNull with zero or desired text it is feasible to check the row value and assign desired text as shown below.
1: //Loop through data rows
2: foreach (DataRow row in dtResults.Rows)
3: {
4: for (int i = 0; i < dtResults.Columns.Count; i++)
5: {
6: //check if the row value is DBNull
7: if (row[i] is DBNull)
8: {
9: \\Assigning zero to row value
10: row[i] = "0";
11: }
12: context.Response.Write(row[i].ToString().Replace("\n", " ") + “,”);
13: }
14: context.Response.Write(Environment.NewLine);
15: }