?
Solved

returning   for nulls in tables

Posted on 2011-10-04
5
Medium Priority
?
315 Views
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" "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

0
Comment
Question by:Tagom
5 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36914833
replace this with Trim

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

with

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

Expert Comment

by:Eyal
ID: 36914858
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
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 36914862
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

0
 

Author Closing Comment

by:Tagom
ID: 36916571
worked perfect! thank you
0
 
LVL 75

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

862 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