0

I am trying to export gridview data into csv file using asp.net. I used below codes i've got from a website, it downloads successfully a csv file but there's no content, meaning the entire csv file was empty.

    Response.ClearContent()
    Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "Data.csv"))
    Response.ContentType = "application/text"
    GridView1.AllowPaging = False
    GridView1.DataBind()
    Dim strbldr As New StringBuilder()
    For i As Integer = 0 To GridView1.Columns.Count - 1
        'separting header columns text with comma operator
        strbldr.Append(GridView1.Columns(i).HeaderText + ","c)
    Next
    'appending new line for gridview header row
    strbldr.Append(vbLf)
    For j As Integer = 0 To GridView1.Rows.Count - 1
        For k As Integer = 0 To GridView1.Columns.Count - 1
            'separating gridview columns with comma
            strbldr.Append(GridView1.Rows(j).Cells(k).Text + ","c)
        Next
        'appending new line for gridview rows
        strbldr.Append(vbLf)
    Next
    Response.Write(strbldr.ToString())
    Response.[End]()

What shall I do?

Thanks in advance.

J-J
  • 1,063
  • 5
  • 22
  • 47
  • Have you tried to debug the code? Do you understand what it is doing or did you just copy and paste and hoped it will work? – luke2012 Jan 30 '14 at 02:56
  • i copied it, trying if it will work – J-J Jan 30 '14 at 02:58
  • You probably won't get much help from others unless you have tried debugging the issue and provide some clearer error messages or an understanding of what you are trying to do. – luke2012 Jan 30 '14 at 03:18
  • i debugged it already and there is no error – J-J Jan 30 '14 at 03:20
  • How is your GridView populated? from the looks of it, it should be taking the content from your GridView and writing it to the CSV file. – luke2012 Jan 30 '14 at 03:23
  • i populated it using a datasource – J-J Jan 30 '14 at 03:24
  • Can you see the data in your GridView? Is the ID of the GridView, `GridView1`? – luke2012 Jan 30 '14 at 03:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46394/discussion-between-j-j-and-luke2012) – J-J Jan 30 '14 at 03:30

1 Answers1

0

Seems like the same problem as Trying to export asp.net gridview to a .csv file

GridView1.AllowPaging = False
GridView1.DataBind()

Are the lines causing issues and should be removed.

Community
  • 1
  • 1
Chris
  • 805
  • 6
  • 19