-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolice.vb
159 lines (150 loc) · 7.06 KB
/
Police.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Imports System.Data.SqlClient
Imports System.IO
Public Class Police
Friend police_id As Integer
Private connString As String = (New ConfigHelper).ConnectionString
Private connection As New SqlConnection(connString)
Private command As New SqlCommand("", connection)
Private imgsPath_Police As String = (New ConfigHelper).GetPoliceSignatureImgPath
Private fileName As String
Private imgFileToUpload As String = ""
Private fileSavePath As String = ""
Private fileOldPath As String = ""
Private Sub MaterialButton1_Click(sender As Object, e As EventArgs) Handles MaterialButton1.Click
If Not Directory.Exists(imgsPath_Police) Then
Directory.CreateDirectory(imgsPath_Police)
End If
If police_id Then
Try
SqlData("UPDATE police SET fname = @fname, mname = @mname, lname = @lname, contact_no = @contactno, police_sig = @policesig, rank_id = @rankid, position_id = @positionid WHERE police_id =" & police_id)
If fileSavePath <> fileOldPath Then
File.Copy(imgFileToUpload, fileSavePath, True)
End If
Me.Dispose()
Catch ex As Exception
connection.Close()
MsgBox("Update Police error" & vbCrLf & String.Format("Error: {0}", ex.Message))
End Try
Else
Try
SqlData("INSERT INTO police(fname,mname,lname,contact_no,police_sig,rank_id,position_id,deleted) VALUES(@fname,@mname,@lname,@contactno,@policesig,@rankid,@positionid,0)")
File.Copy(imgFileToUpload, fileSavePath, True)
Me.Dispose()
Catch ex As Exception
connection.Close()
MsgBox("Insert Police error" & vbCrLf & String.Format("Error: {0}", ex.Message))
End Try
End If
If connection.State = ConnectionState.Open Then connection.Close()
End Sub
Private Sub SqlData(sqlCommand As String)
connection.Open()
command = New SqlCommand("", connection)
command.CommandText = sqlCommand
command.Parameters.AddWithValue("@fname", txtFname.Text.Trim)
command.Parameters.AddWithValue("@mname", txtMname.Text.Trim)
command.Parameters.AddWithValue("@lname", txtLname.Text.Trim)
command.Parameters.AddWithValue("@contactno", txtContactNo.Text.Trim)
command.Parameters.AddWithValue("@policesig", fileSavePath)
command.Parameters.AddWithValue("@rankid", cbRank.SelectedValue.ToString)
command.Parameters.AddWithValue("@positionid", cbPosition.SelectedValue.ToString)
command.ExecuteNonQuery()
connection.Close()
command = Nothing
End Sub
Private Sub PopulateCombobox()
PopulateComboboxRank()
PopulateComboboxPosition()
End Sub
Private Sub PopulateComboboxRank()
Try
connection.Open()
command = New SqlCommand("", connection)
command.CommandText = "SELECT rank_id AS 'RANK ID', name AS 'RANK NAME' FROM rank"
Dim da As New SqlDataAdapter(command)
Dim dt As New DataTable()
da.Fill(dt)
cbRank.DisplayMember = dt.Columns("RANK NAME").ToString
cbRank.ValueMember = dt.Columns("RANK ID").ToString
cbRank.DataSource = dt
connection.Close()
command = Nothing
Catch ex As Exception
MsgBox("Loading Rank error" & vbCrLf & String.Format("Error: {0}", ex.Message))
End Try
End Sub
Private Sub PopulateComboboxPosition()
Try
connection.Open()
command = New SqlCommand("", connection)
command.CommandText = "SELECT position_id AS 'POSITION ID', name AS 'POSITION NAME' FROM position"
Dim da As New SqlDataAdapter(command)
Dim dt As New DataTable()
da.Fill(dt)
cbPosition.DisplayMember = dt.Columns("POSITION NAME").ToString
cbPosition.ValueMember = dt.Columns("POSITION ID").ToString
cbPosition.DataSource = dt
connection.Close()
command = Nothing
Catch ex As Exception
MsgBox("Loading Rank error" & vbCrLf & String.Format("Error: {0}", ex.Message))
End Try
End Sub
Private Sub Police_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Preload data
PopulateCombobox()
If police_id Then
connection.Open()
command = New SqlCommand("", connection)
command.CommandText = "SELECT * FROM police WHERE deleted = 0 AND police_id = " & police_id
Dim da As New SqlDataAdapter(command)
Dim dt As New DataTable()
da.Fill(dt)
If (dt.Rows.Count > 0) Then
txtFname.Text = dt.Rows(0).Item("fname")
txtMname.Text = dt.Rows(0).Item("mname")
txtLname.Text = dt.Rows(0).Item("lname")
txtContactNo.Text = dt.Rows(0).Item("contact_no")
If System.IO.File.Exists(dt.Rows(0).Item("police_sig")) Then
'The file exists
imgFileToUpload = dt.Rows(0).Item("police_sig")
fileName = Path.GetFileName(imgFileToUpload)
fileSavePath = Path.Combine(imgsPath_Police, fileName)
fileOldPath = fileSavePath
Using fs As New FileStream(imgFileToUpload, FileMode.Open, FileAccess.Read)
PictureBox1.Image = Image.FromStream(fs)
End Using
Else
'the file doesn't exist
imgFileToUpload = ""
fileName = ""
fileSavePath = ""
End If
cbRank.SelectedValue = dt.Rows(0).Item("rank_id")
cbPosition.SelectedValue = dt.Rows(0).Item("position_id")
End If
connection.Close()
command = Nothing
Else
'MsgBox("No police_ID")
End If
If connection.State = ConnectionState.Open Then connection.Close()
End Sub
Private Sub MaterialButton3_Click(sender As Object, e As EventArgs) Handles MaterialButton3.Click
Dim opfDialog As New OpenFileDialog
opfDialog.Filter = "Choose Image(*.jpg;*.png) | *.jpg; *.png"
If opfDialog.ShowDialog = DialogResult.OK Then
imgFileToUpload = opfDialog.FileName
fileName = Path.GetFileName(imgFileToUpload)
fileSavePath = Path.Combine(imgsPath_Police, fileName)
Using fs As New FileStream(imgFileToUpload, FileMode.Open, FileAccess.Read)
PictureBox1.Image = Image.FromStream(fs)
End Using
End If
End Sub
Private Sub CancelButton_Click(sender As Object, e As EventArgs) Handles CancelButton.Click
If connection.State = ConnectionState.Open Then connection.Close()
Me.Dispose()
End Sub
End Class