<%@ Page Language="vb" %>

<%@ Import Namespace="System.IO" %>

<html>

  <head>

  <title>Reading Data from a Text File</title>

 

<script runat="server">

    Sub WriteToFile(ByVal sender As System.Object, ByVal e As System.EventArgs)

        'Put user code to initialize the page here

        Dim fp As StreamReader

        Try

            fp = File.OpenText(Server.MapPath(".\Upload\") & "test.txt")

            txtMyFile.Text = fp.ReadToEnd()

            lblStatus.Text = "File Succesfully Read!"

            fp.Close()

        Catch err As Exception

            lblStatus.Text = "File Read failed. Reason is as follows <br><br>" & err.ToString()

        Finally

 

        End Try

    End Sub

</script>

 

 

</head>

<body style="font: 10pt verdana">

            <h3 align="center">Reading Data from a Text File in ASP .NET</h3>

    <form id="Form1" method="post" runat="server">

                        <asp:TextBox

                                    ID="txtMyFile"

                                    TextMode="MultiLine"

                                    Rows="10"

                                    Columns="60"

                                    Runat="server" /><br>

                        <asp:button

                                    ID="btnSubmit"

                                    Text="Read File"

                                    OnClick="WriteToFile"

                                    Runat="server" />

                        <asp:Label

                                    ID="lblStatus"

                                    Font-Bold="True"

                                    ForeColor="#ff0000"

                                    Runat="server" />

    </form>

 

            <br><br><br>

            <p align=center><font size=2>

            Send your comments to <a href="mailto:das@aspalliance.com">das@aspalliance.com</a>

            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=default.aspx>Back to Tutorial</a>

            <br><br>

            <center><a id="LogoLink" href="http://aspalliance.com"><img src="http://aspalliance.com/libraryaspa/logocounter.asp?article_id=875" border="0" /></a></center>

            </font></p>   

</body>

</html>

Send your comments to das@aspalliance.com        Back to Tutorial