membuat dan membaca file txt

hmmm…
baru posting tentang ASP.NET bukannya perkenalan tentang ASP.NET dan dasar-dasar form atau koneksi database(insert, update dan delete) malah langsung membuat file txt :D
ya, mungkin kalau untuk form2 pada ASP.NET sama dengan HTML dan bisa menggunakan Visual Studio tinggal drag and drop :p
kalau untuk koneksi, mungkin sambil berjalan :p.

source pada vb script untuk membuat file txt :


Imports System
Imports System.Data
Imports System.IO
Imports System.Data.OracleClient
Imports System.Collections

Partial Public Class FormKu
    Inherits System.Web.UI.Page
    Dim myConnection As New OracleConnection
    Dim dstam As New DataSet
    Dim sql As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim sw As New System.IO.StreamWriter(Server.MapPath("yourfile.txt"), False)

myConnection = New OracleConnection("data source=rifkyiip;User ID=punyaku;password=pass;")

sql = "select x.a,x.b,x.c from tabelku x where x.id='" + LBLno.Text + "'"
myConnection.Open()

Dim i As Integer
Dim penghubungku As OracleDataAdapter
penghubungku = New OracleDataAdapter(sql, myConnection)
penghubungku.Fill(dstam, "tabelku x")
Try
  For i = 0 To dstam.Tables(0).Rows.Count - 1
     With dstam.Tables(0).Rows(i)
        sw.WriteLine("Nama :" & .Item(0) & "<BR>")
        sw.WriteLine("Alamat :" & .Item(1) & "<BR>")
     End With
  Next
Catch myException As Exception
     Response.Write("Error...")
 Finally
     sw.Close()
End Try
myConnection.Close()
End Sub
End Class

membaca file txt :


Dim FileName As String
Dim fp As StreamReader
FileName = "yourfile.txt"
Dim myWriter As System.IO.TextWriter

Try
    fp = File.OpenText(Server.MapPath(FileName))
    myWriter = Response.Output
    myWriter.WriteLine(fp.ReadToEnd())
    'txtBOX.Text = fp.ReadToEnd()
    fp.Close()
    ClientScript.RegisterStartupScript(Me.GetType(), "onclick", "<script language=javascript>window.print();</script>")
   Catch err As Exception
   lblIIP.Text = "File Read Failed. Reason is as follows " & err.ToString()
Finally
End Try

semoga bermanfaat :)

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.