Datatable to CSV Export RRS feed

  • Question

  • User1203305613 posted


    I have code where in load gridview from datatable,I like to use the same datatable to export to excel

    The thing is Datatable is empty when I do a button click,the code works perfectly in Public Sub RenderReport(),how do I retain the value in Datatable

    The Datatable is loaded in  Public Sub RenderReport()

    Public Sub RenderReport()


    grdClaims.DataSource = dt
    grdClaims.Visible = True

    End Sub 

    Protected Sub btntoExcel_Click(ByVal sender As Object, ByVal e As EventArgs)

    'export to excel

    Dim strDownloadFileName As String = DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv"

    ' Your Function for Retrieving Data

    Response.ContentType = "text/plain"
    Response.AppendHeader("content-disposition", "attachment; filename=" + strDownloadFileName) ' myData - file name (also can be bring from DB)

    'MyData - your data ...

    Dim myData As Byte() = csvBytesWriter(dt)

    Response.BinaryWrite(myData) ' Binary data - see myData -
    'export to excel ends

    End Sub

    Saturday, February 17, 2018 10:39 AM


  • User1203305613 posted

    I believe I figured it put I declared Datatable as Static in VB.NET Shared , it works

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 18, 2018 7:03 AM