locked
ASP.NET VB Read in TXT file and Display in Table RRS feed

  • Question

  • User-517398772 posted

     ASP.NET VB Read in TXT file and Display in Table Gridview 

    Hi Guys and Gals,

    I have a question that I'm guessing is straigh forward but I might be making a bit of a mess of this.

    I have a text file that is sperated by spaces into 3 collumns I wont to read this in and then display it in a web page in a table is this possible?

    Sunday, December 13, 2020 11:13 PM

Answers

  • User-1330468790 posted

    Hi abdullah1122,

     

    Thank you, but can I put the ID in value and Name in value and Property in value

    Could you please specify the problem?

    Sorry that I do not get the question very clear. You orginal question is 

    I have a text file that is sperated by spaces into 3 collumns 
    I wont (want I guess) to read this in and then display it in a web page in a table is this possible?

    How do you want to "put ID in value and Name in value and Property in value"?

    The code just assigns variables "ID", "Name" and "Property" to strings "a", "b" and "c".

      

    If you want to rewrite values to the .txt file, you could refer to below codes and documentation.

    • Construct a string array or one string and then write it to the .txt file.
    // Example #1: Write an array of strings to a file.
            // Create a string array that consists of three lines.
            string[] lines = { "First line", "Second line", "Third line" };
            // WriteAllLines creates a file, writes a collection of strings to the file,
            // and then closes the file.  You do NOT need to call Flush() or Close().
            System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
    
    // Example #2: Write one string to a text file.
            string text = "A class is the most powerful data type in C#. Like a structure, " +
                           "a class defines the data and behavior of the data type. ";
            // WriteAllText creates a file, writes the specified string to the file,
            // and then closes the file.    You do NOT need to call Flush() or Close().
            System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);

    More examples, you could refer to:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file

     

    Hope this helps.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 15, 2020 11:14 AM

All replies

  • User-1330468790 posted

    Hi abdullah1122,

     

    It is definitely possible.

    Steps:

    • Read the text file first and fetch the columns correspondingly into a DataTable (other types of collections are feasible as well). 
    • Bind DataTable to the GridView control.

    My solution below focus on reading contents from the text file and render them inside a GridView Control.

    The actually implementation depends on the structure of your text files and you should still need to provide some validations on the text file to avoid unexpected inputs.

     

    More details, you could refer to below codes:

    .aspx Page:

     <form id="form1" runat="server">
           <div>
                <h3>Display Txt from Server Side</h3>
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
            </div>
            <div><h3>Upload Txt and Display</h3>
                <asp:FileUpload ID="Uploader1" runat="server" />
                <asp:Button ID="UploadBtn" runat="server" OnClick="UploadBtn_Click" Text="Submit And Display" />
                <asp:GridView ID="GridView2" runat="server"></asp:GridView>
            </div>
        </form>

    Code behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                BindGridView1()
            End If
        End Sub
        Protected Sub BindGridView1()
            Dim dt As DataTable = New DataTable()
            dt.Columns.Add("ID", GetType(Integer))
            dt.Columns.Add("Name", GetType(String))
            dt.Columns.Add("Property", GetType(String))
            Dim txtPath As String = "C:\Path\To\File\testData.txt"
            Dim lines As String() = File.ReadAllLines(txtPath)
    
            For Each line As String In lines
                Dim cols = line.Split(" "c)
                Dim dr As DataRow = dt.NewRow()
    
                For i As Integer = 0 To 3 - 1
                    dr(i) = cols(i)
                Next
    
                dt.Rows.Add(dr)
            Next
    
            GridView1.DataSource = dt
            GridView1.DataBind()
        End Sub
    
        Protected Sub BindGridView2()
            Dim dt As DataTable = New DataTable()
            dt.Columns.Add("ID", GetType(Integer))
            dt.Columns.Add("Name", GetType(String))
            dt.Columns.Add("Property", GetType(String))
            Dim file As HttpPostedFile = Uploader1.PostedFile
    
            Using reader As StreamReader = New StreamReader(file.InputStream)
                Dim line As String = reader.ReadLine()
    
                While line IsNot Nothing
                    Dim cols = line.Split(" "c)
                    Dim dr As DataRow = dt.NewRow()
    
                    For i As Integer = 0 To 3 - 1
                        dr(i) = cols(i)
                    Next
    
                    dt.Rows.Add(dr)
                    line = reader.ReadLine()
                End While
            End Using
    
            GridView2.DataSource = dt
            GridView2.DataBind()
        End Sub
    
        Protected Sub UploadBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
            BindGridView2()
        End Sub

    Text file content:

    1 Name1 Property1
    2 Name2 Property2
    3 Name3 Property3
    4 Name4 Property4

    Demo:

     

    Hope this helps.

    Best regards,

    Sean

    Monday, December 14, 2020 3:15 AM
  • User-517398772 posted

    Thank you, but can I put the ID in value and Name in value and Property in value

    Example
    
    

    Dim a as string = ID

    Dim b as string = name

    Dim c as string = Property

    ID NAME Property
    a b c
    a b c
    a b c
    a b c
    Tuesday, December 15, 2020 6:40 AM
  • User-1330468790 posted

    Hi abdullah1122,

     

    Thank you, but can I put the ID in value and Name in value and Property in value

    Could you please specify the problem?

    Sorry that I do not get the question very clear. You orginal question is 

    I have a text file that is sperated by spaces into 3 collumns 
    I wont (want I guess) to read this in and then display it in a web page in a table is this possible?

    How do you want to "put ID in value and Name in value and Property in value"?

    The code just assigns variables "ID", "Name" and "Property" to strings "a", "b" and "c".

      

    If you want to rewrite values to the .txt file, you could refer to below codes and documentation.

    • Construct a string array or one string and then write it to the .txt file.
    // Example #1: Write an array of strings to a file.
            // Create a string array that consists of three lines.
            string[] lines = { "First line", "Second line", "Third line" };
            // WriteAllLines creates a file, writes a collection of strings to the file,
            // and then closes the file.  You do NOT need to call Flush() or Close().
            System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
    
    // Example #2: Write one string to a text file.
            string text = "A class is the most powerful data type in C#. Like a structure, " +
                           "a class defines the data and behavior of the data type. ";
            // WriteAllText creates a file, writes the specified string to the file,
            // and then closes the file.    You do NOT need to call Flush() or Close().
            System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);

    More examples, you could refer to:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file

     

    Hope this helps.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 15, 2020 11:14 AM