codehutch.com

Online Code Repository

I was digging through my files when I found this subroutine and I thought, maybe somebody out there might need it. It’s not my original code though (and I forgot where I got it). Anyhow, I just wanted to share it and here it is:

Public Sub ExportToExcel()

Dim table as Datatable = “Query your datatable here”
Dim name as String  = “filename”

Dim context As HttpContext = HttpContext.Current
context.Response.Clear()
For Each column As DataColumn In table.Columns
context.Response.Write(column.ColumnName & “,”)
Next
context.Response.Write(Environment.NewLine)
For Each row As DataRow In table.Rows
For i As Integer = 0 To table.Columns.Count - 1
context.Response.Write(row(i).ToString().Replace(”,”, String.Empty) & “,”)
Next
context.Response.Write(Environment.NewLine)
Next
context.Response.ContentType = “text/csv”
context.Response.AppendHeader(”Content-Disposition”, “attachment; filename=” & name & “.csv”)
context.Response.[End]()

LoadData_Base()
End Sub



Tags:

Timer in VB .Net

A simple timer class written in VB .Net

Imports System.Timers

Public Class TestTimer
Shared _timer As New Timer()
Dim _timerInterval As Integer = 10000 ‘in millisecconds

Public Sub New()
Initialize()
End Sub

Public Sub Initialize()
AddHandler _timer.Elapsed, AddressOf _timer_Elapsed
_timer.Interval = _timerInterval
End Sub

Public Sub _timer_Elapsed(ByVal sender As Object, ByVal e As ElapsedEventArgs)
‘As long as timer is ‘enabled’ this function will be triggered every time interval set (in this case every 10 seconds)
‘do something here
End Sub

Public Sub start()
_timer.Enabled = True
End Sub

Public Sub stopTimer()
_timer.Enabled = False
End Sub

End Class



Tags:

  Next Entries »