-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefault.aspx.vb
135 lines (100 loc) · 3.92 KB
/
Default.aspx.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
Imports System.Data.SqlClient
Imports System.Data
Imports System.Configuration
Partial Class _Default
Inherits Page
Const DASH As String = "-"
Const ZERO As String = "0"
Const COMMA As String = ","
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
ddlDOW.Text = 1
End If
End Sub
Protected Sub lbGet_Click(sender As Object, e As EventArgs) Handles lbGet.Click
Dim sbGet As New StringBuilder
Dim strDOW As String = ddlDOW.SelectedValue.ToString
Dim strTime As String = ddlTime.SelectedValue.ToString
Dim strTown As String = ddlTown.SelectedValue.ToString
' ? SqlDsList.EnableCaching = "True"
' Get Day of week
If strDOW <> DASH Then
If strDOW <> ZERO Then
sbGet.Append(String.Format(" DOW = {0}", strDOW))
Else
sbGet.Append(String.Format(" DOW > {0}", 0))
End If
End If
' Get time
If strTime <> ZERO Then
If sbGet.Length > 0 Then
sbGet.Append(String.Format(" AND TimeID = {0}", strTime))
End If
End If
' Get town
If strTown <> DASH Then
If sbGet.Length > 0 Then
sbGet.Append(String.Format(" AND Town = '{0}'", strTown))
End If
Else
sbGet.Append(String.Format(" AND Town like '{0}'", "%"))
End If
' Set the filter
If sbGet.Length > 0 Then
SqlDsList.FilterExpression = sbGet.ToString()
' lblFilter.Text = "Filter: " & SqlDsList.FilterExpression.ToString()
Else
SqlDsList.FilterExpression = vbNull
' lblFilter.Text = "Filter: " & SqlDsList.FilterExpression.ToString()
End If
End Sub
Protected Sub lnkExport_Click(sender As Object, e As EventArgs) Handles lnkExport.Click
' Stored procedure: GetMeetingList
' Modified from
' http: //www.aspsnippets.com/Articles/Export-data-from-SQL-Server-to-Text-file-in-C-and-VBNet.aspx
Dim rc As Integer = -1
Dim cnstr As String = ConfigurationManager.ConnectionStrings("cnDEIG").ConnectionString
Dim cn As New SqlConnection(cnstr)
Dim cmd As New SqlCommand("GetMeetingList")
cmd.CommandType = CommandType.StoredProcedure
Dim da As New SqlDataAdapter()
' Get the connection into the SqlCommand object
cmd.Connection = cn
da.SelectCommand = cmd
Dim dt As New DataTable()
Try
rc = da.Fill(dt)
Catch ex As Exception
' lblInfo.Text = ex.ToString()
End Try
' Read the table to create a file for export
Dim sb As New StringBuilder()
' Get the column name
For Each column As DataColumn In dt.Columns
'Add the Header row for Text file.
sb.Append(column.ColumnName & COMMA)
Next
'Add new line.
sb.Append(vbCr & vbLf)
' Get the data
For Each row As DataRow In dt.Rows
For Each column As DataColumn In dt.Columns
'Add the Data rows.
' DataTable.rows(i)(j)
' (row(column).tostring() works as well.
sb.Append(row(column.ColumnName).ToString() & COMMA)
Next
'Add new line.
sb.Append(vbCr & vbLf)
Next
'Download the Text file.
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=MeetingList.csv")
Response.Charset = ""
Response.ContentType = "application/text"
Response.Output.Write(sb.ToString())
Response.Flush()
Response.End()
End Sub
End Class