Sukumar Raju's Blog

MCP

Sponsors

Tags

News

SharePoint SharePoint

More reading these days Patterns and practicces


Interesting to work with ASP.NET Membership provider

Suggested Reading C# Book


patterns & practices Application Architecture Guide 2.0


MVP Blog Badge.

Grab this badge here!


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:             }

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)