Saturday, March 14, 2009

How to Display content of text file using Datagrid

<%@ page debug=true%>
<%@ import namespace="system.data,system.io"%>


<script runat=server>

sub page_load()
dim dt as new DataTable()

dim c_name as new DataColumn("Name", Type.GetType("System.String"))
dim c_email as new DataColumn("Email", Type.GetType("System.String"))

dt.columns.add(c_name)
dt.columns.add(c_email)


' read data from file in the current directory
' file contains emails in the format name:email

dim sr as new StreamReader( server.mappath("emails.txt"))

dim line as string
dim parts(2) as string
dim dr as datarow

line = sr.Readline()

' read lines until you reach EOF

do until line is nothing

' split the line into two - name and email

parts = line.split(":")

if parts.length = 2 then

' add a new row to datatable
dr = dt.NewRow()
dr.item(0) = parts(0)
dr.item(1) = parts(1)

dt.rows.add(dr)
end if

' read next line

line = sr.ReadLine()

loop

sr.close()

dg1.datasource = dt
dg1.DataBind()


end sub

</script>

<form runat=server>
<h2>Email Addresses </h2>
<asp:datagrid id=dg1 runat=server />
</form>

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home