Link to home
Start Free TrialLog in
Avatar of Tagom
Tagom

asked on

returning   for nulls in tables

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="cmdSend" runat="server" Text="Download File" 
            onclick="cmdSend_Click" /></br>
        </br>
        <asp:GridView ID="GridView1" AutoGenerateColumns="True" runat="server" />
    </div>
    </form>
</body>
</html>

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";
         if(!Page.IsPostBack){ 
        // Create Connection object
        OleDbConnection dbConn = new OleDbConnection(GetConnectionString());

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

        // Open Connection
        dbConn.Open();

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

        GridView1.DataSource = dbRead;
        GridView1.DataBind();

        dbConn.Close();
        
         }
    }
    
    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.ClearContent();
        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
        strbldr.Append("\n");
        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
            strbldr.Append("\n");
        }
        Response.Write(strbldr.ToString());
        Response.End();
    }
}

Open in new window

Avatar of Pratima
Pratima
Flag of India image

replace this with Trim

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

with

   strbldr.Append(cell.Text.Trim() + ',');
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
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tagom
Tagom

ASKER

worked perfect! thank you
NP. Glad to help  = )