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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
| Public Class afdedit
Inherits ControlBase
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Conn = New System.Data.SqlClient.SqlConnection
Me.DA = New System.Data.SqlClient.SqlDataAdapter
Me.SelComm = New System.Data.SqlClient.SqlCommand
Me.UpdComm = New System.Data.SqlClient.SqlCommand
Me.DS = New System.Data.DataSet
Me.DV = New System.Data.DataView
Me.DT = New System.Data.DataTable
CType(Me.DS, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DV, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DT, System.ComponentModel.ISupportInitialize).BeginInit()
'
'Conn
'
Me.Conn.ConnectionString = "server=SHAREPOINT;Database=Intranet;uid=sa;pwd=VAB7vtb5$"
'
'DA
'
Me.DA.SelectCommand = Me.SelComm
Me.DA.UpdateCommand = Me.UpdComm
'
'SelComm
'
Me.SelComm.CommandText = "SELECT afdelingid, naam, omschrijving, url FROM Afdelingen"
Me.SelComm.Connection = Me.Conn
'
'UpdComm
'
Me.UpdComm.CommandText = "UPDATE Afdelingen SET naam = @naam, omschrijving = @omschrijving, url = @url WHER" & _
"E (afdelingid = @afdelingid); SELECT naam, omschrijving, url, afdelingid FROM Af" & _
"delingen WHERE (afdelingid = @afdelingid)"
Me.UpdComm.Connection = Me.Conn
Me.UpdComm.Parameters.Add(New System.Data.SqlClient.SqlParameter("@afdelingid", System.Data.SqlDbType.Int, 4, "afdelingid"))
Me.UpdComm.Parameters.Add(New System.Data.SqlClient.SqlParameter("@naam", System.Data.SqlDbType.VarChar, 100, "naam"))
Me.UpdComm.Parameters.Add(New System.Data.SqlClient.SqlParameter("@omschrijving", System.Data.SqlDbType.VarChar, 100, "omschrijving"))
Me.UpdComm.Parameters.Add(New System.Data.SqlClient.SqlParameter("@url", System.Data.SqlDbType.VarChar, 100, "url"))
'
'DS
'
Me.DS.DataSetName = "DataS"
Me.DS.Locale = New System.Globalization.CultureInfo("nl-BE")
'
'DT
'
Me.DT.TableName = "afdelingen"
CType(Me.DS, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.DV, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.DT, System.ComponentModel.ISupportInitialize).EndInit()
End Sub
Protected WithEvents DG As System.Web.UI.WebControls.DataGrid
Protected WithEvents DA As System.Data.SqlClient.SqlDataAdapter
Protected WithEvents DS As System.Data.DataSet
Protected WithEvents DV As System.Data.DataView
Protected WithEvents DT As System.Data.DataTable
Protected WithEvents Conn As System.Data.SqlClient.SqlConnection
Protected WithEvents SelComm As System.Data.SqlClient.SqlCommand
Protected WithEvents UpdComm As System.Data.SqlClient.SqlCommand
Protected WithEvents divAfdeling As System.Web.UI.HtmlControls.HtmlGenericControl
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
Session("EditID") = -1
Session("Direction") = " ASC"
Session("SortField") = "naam"
BindGrid()
End If
End Sub
' Sorteren op een bepaalde kolom door kolomhoofding aan te klikken
Public Sub DG_sort(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DG.SortCommand
' Als een rij in "edit-modus" is
If DG.EditItemIndex <> -1 Then
FindEditPosKey()
End If
' Als er geklikt is op de kolom waarop al gesorteerd is
If CType(Session("SortField"), String) = e.SortExpression Then
' Indien oplopend wordt gesorteerd, aflopend sorteren, en analoog andersom
If CType(Session("Direction"), String) = " ASC" Then
Session("Direction") = " DESC"
Else
Session("Direction") = " ASC"
End If
Else
Session("Direction") = " ASC"
End If
' Kolom onthouden waarop gesorteerd wordt
Session("SortField") = e.SortExpression
BindGrid()
End Sub
' Edit-layout voor een bepaalde rij activeren
Public Sub DG_Edit(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG.EditCommand
DG.EditItemIndex = e.Item.ItemIndex
BindGrid()
End Sub
' Updaten van een gewijzigde rij
Public Sub DG_update(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG.UpdateCommand
' definieren van de textboxes gecreeerd door de edit-layout te openen voor een rij
Dim tb_omschrijving As TextBox = CType(e.Item.Cells(3).Controls(0), TextBox)
Dim tb_url As TextBox = CType(e.Item.Cells(4).Controls(0), TextBox)
' vastleggen van alle cellen van de te wijzigen rij
Dim afdelingid As Integer = CInt(e.Item.Cells(1).Text)
Dim naam As String = e.Item.Cells(2).Text
Dim omschrijving As String = tb_omschrijving.Text
Dim url As String = tb_url.Text
If omschrijving.Length = 0 Or omschrijving.Length > 30 Or Not Geen_SQL_Injection(omschrijving) Then
' invoercontrole van omschrijving
Fout("Ongeldige omschrijving... De omschrijving moet minstens 1 teken en mag maximum 29 tekens zijn... " & _
"Volgende tekens zijn niet toegelaten: ', "", *, <, >")
ElseIf url.Length < 8 Then
' invoercontrole van url
Fout("Ongeldige URL... Een URL begint met 'http://'...")
ElseIf url.Substring(0, 7) <> "http://" Or Not Geen_SQL_Injection(url) Then
' verdere invoercontrole van url
Fout("Ongeldige URL... Een URL begint met 'http://' en mag devolgende tekens niet bevatten: ', "", *, <, >")
Else
' als alle invoercontrole doorlopen is
' sql parameters laden
Me.UpdComm.Parameters("@afdelingid").Value() = afdelingid
Me.UpdComm.Parameters("@naam").Value() = naam
Me.UpdComm.Parameters("@omschrijving").Value() = omschrijving
Me.UpdComm.Parameters("@url").Value() = url
' connectie openen, rij updaten, connectie sluiten, edit-layout ongedaan maken en datagrid herladen
Conn.Open()
Me.UpdComm.ExecuteNonQuery()
Conn.Close()
DG.EditItemIndex = -1
BindGrid()
End If
End Sub
' Invoercontrole tegen SQL Injection
Private Function Geen_SQL_Injection(ByVal code As String) As Boolean
Return code.IndexOf("'") = -1 And code.IndexOf("*") = -1 And _
code.IndexOf("<") = -1 And code.IndexOf(">") = -1 And code.IndexOf("""") = -1
End Function
' Foutmelding bij het wijzigen van een veld
Private Sub Fout(ByVal bericht As String)
DG.EditItemIndex = -1 ' edit-layout ongedaan maken
BindGrid() ' datagrid herladen
Response.Write(bericht) ' foutmelding tonen
End Sub
' Edit-layout ongedaan maken en datagrid herladen
Public Sub DG_Cancel(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG.CancelCommand
DG.EditItemIndex = -1
BindGrid()
End Sub
' Zoekt de primary key van het veld in "edit-modus"
Private Sub FindEditPosKey()
' Dataview vullen met gesorteerd op oude sorteer kolom
DA.Fill(DS, "afdelingen")
DV = DS.Tables("afdelingen").DefaultView
DV.Sort = CType(Session("SortField"), String) & CType(Session("Direction"), String)
' Primary key van rij in "edit-modus" opslaan
Session("EditID") = DV.Item(DG.EditItemIndex).Row.Item(0)
' DataSet leegmaken
DS.Clear()
End Sub
' Herlaad de grid gesorteerd op SortField
Private Sub BindGrid()
' Dataview vullen en datagrid herladen
DA.Fill(DS, "afdelingen")
DV = DS.Tables("afdelingen").DefaultView
DV.Sort = CType(Session("SortField"), String) & CType(Session("Direction"), String)
DG.DataSource = DV
DG.DataBind()
' Indien een rij in "edit-modus" stond
If CInt(Session("EditID")) <> -1 Then
Dim RijIndex As Integer = 0
' zolang er rijen zijn en zolang de primary key niet gevonden is, RijIndex verhogen
While RijIndex < DG.Items.Count And CInt(DG.DataKeys(RijIndex)) <> CInt(Session("EditID"))
RijIndex += 1
End While
' zoeken naar rij in "edit-modus" uitschakelen
Session("EditID") = -1
' de juiste rij in de "edit-modus" zetten
DG.EditItemIndex = RijIndex
' DataSet leegmaken
DS.Clear()
BindGrid()
End If
End Sub
End Class |