• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

ASP.net VB Write contents of gridview to text file

Hi. How would I write the contents of a a GridView in ASP.net VB,net code to a text file on the user's computer?
I need to gather only the data from the rows where the additional column is checked as in the code below

    Protected Sub Button_Load_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_Load.Click

        Try
            Dim gvr As GridViewRow
            Dim chk As CheckBox
            For i As Integer = 0 To GridView1.Rows.Count
                gvr = GridView1.Rows(i)
                chk = gvr.FindControl("chkSelected")
                If (chk.Checked) Then

                    Response.Write(GridView1.Rows(i).Cells(3).Text)
                End If
            Next
        Catch ex As Exception

        End Try

    End Sub

0
Murray Brown
Asked:
Murray Brown
1 Solution
 
gamarrojgqCommented:
Hi,

Forsecurity restrictions we cannot write that text file to the client's hard-drive, but you can allows the user to download the file, in the attached code the process will write the textfile and ask the users to download it.

Just be aware that the code assumes that your gridview1 have the columns defined and use the columns count to write a textfile with the column values delimited by an  space char, and intents to write ALL THE COLUMNS VALUES.

Hope that helps
Dim strFileName As String = "FileName.txt"
        Dim strLine As String
        Dim intCol As Integer

        Response.ContentType = "text/html"

        Try
            Dim gvr As GridViewRow
            Dim chk As CheckBox
            For i As Integer = 0 To GridView1.Rows.Count
                gvr = GridView1.Rows(i)
                chk = gvr.FindControl("chkSelected")
                If (chk.Checked) Then
                    strLine = ""
                    For intCol = 0 To GridView1.Columns.Count - 1
                        strLine &= GridView1.Rows(i).Cells(intCol).Text & " "
                    Next
                    Response.Output.WriteLine(strLine)
                End If
            Next
        Catch ex As Exception
        End Try

        Response.AppendHeader("content-disposition", "attachment; filename=" & strFileName)
        Response.End()

Open in new window

0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
thanks a lot for the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now