returning   for nulls in tables

Posted on 2011-10-04
Last Modified: 2012-05-12
I created a file to download a table - it downloads however when I check it, the nulls have been filled with  
I need to leave these blank in the file.
front code: aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Button ID="cmdSend" runat="server" Text="Download File" 
            onclick="cmdSend_Click" /></br>
        <asp:GridView ID="GridView1" AutoGenerateColumns="True" runat="server" />

Open in new window

back end aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Text;

public partial class _Default : System.Web.UI.Page
    private const string MDBFILE = "~/hgdeposition/App_Data/hgweb.mdb";

    private string GetConnectionString()
            return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(MDBFILE) + ";";

    protected void Page_Load(object sender, EventArgs e)
        string SQL = "SELECT * FROM RainEvents";
        // Create Connection object
        OleDbConnection dbConn = new OleDbConnection(GetConnectionString());

        // Create Command Object
        OleDbCommand dbComm = new OleDbCommand(SQL, dbConn);

        // Open Connection

        // Execute command and receive DataReader
        OleDbDataReader dbRead = dbComm.ExecuteReader();

        GridView1.DataSource = dbRead;

    public override void VerifyRenderingInServerForm(Control control)
        /* Verifies that the control is rendered */
    /// <summary>
    /// This event is used to export gridview data to CSV document
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmdSend_Click(object sender, EventArgs e)
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "File.csv"));
        Response.ContentType = "application/text";
        GridView1.AllowPaging = false;

        StringBuilder strbldr = new StringBuilder();
        foreach (TableCell cell in GridView1.HeaderRow.Cells)
            //separting header columns text with comma operator
            strbldr.Append(cell.Text + ',');
        //appending new line for gridview header row
        foreach (GridViewRow gvr in GridView1.Rows)
            foreach (TableCell cell in gvr.Cells)
                //separating gridview columns with comma
                strbldr.Append(cell.Text + ',');

            //appending new line for gridview rows

Open in new window

Question by:Tagom
    LVL 39

    Expert Comment

    by:Pratima Pharande
    replace this with Trim

       strbldr.Append(cell.Text + ',');


       strbldr.Append(cell.Text.Trim() + ',');
    LVL 15

    Expert Comment

    first it's not recommended to use * in queries

    string SQL = "SELECT id,isnull(name,'&nbsp;') as name FROM RainEvents";

    I don't remember if access has the isnull function, try it
    LVL 74

    Accepted Solution

    Try changing this:

    strbldr.Append(cell.Text + ',');

    Open in new window

    to this:

    strbldr.Append(cell.Text.Replace("&nbsp;", String.Empty) + ',');

    Open in new window


    Author Closing Comment

    worked perfect! thank you
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    NP. Glad to help  = )

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    This article will show, step by step, how to integrate R code into a R Sweave document
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now