forked from mohany203/Spectra-Laboratory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_patients.vb
285 lines (230 loc) · 12.2 KB
/
view_patients.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
Imports System.Data.SqlClient
Public Class view_patients
Private Sub view_patients_Load(sender As Object, e As EventArgs) Handles MyBase.Load
database.GetSQLConnection()
'sqlcon.Open()
End Sub
Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Label10_Click(sender As Object, e As EventArgs) Handles Label10.Click
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If TextBox1.Text.Trim() <> "" Then
' Define the SQL query to select the record based on patientID
Dim query As String = "SELECT * FROM patient WHERE patientID =" & TextBox1.Text.Trim() & ""
Using command As New SqlCommand(query, sqlcon)
' Create a DataTable to store the result of the query
Dim dataTable As New DataTable()
Try
' Execute the query and fill the DataTable with the result
Dim adapter As New SqlDataAdapter(command)
adapter.Fill(dataTable)
' Check if any rows were returned
If dataTable.Rows.Count > 0 Then
' Bind the DataTable to the DataGridView
DataGridView1.DataSource = dataTable
Else
' If no record is found, display a message
MessageBox.Show("No record found with the specified patientID.", "Not Found", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
' Handle any exceptions here
MessageBox.Show("Error retrieving data: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Using
Else
' If TextBox1 is empty, display a message
MessageBox.Show("Please enter a patientID.", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
If TextBox2.Text.Trim() <> "" Then
' Define the SQL query to select the record based on patientID
Dim query As String = "SELECT * FROM patient WHERE Name like '%" & TextBox2.Text.Trim() & "%'"
' Create a SqlCommand object with the query and connection
Using command As New SqlCommand(query, sqlcon)
' Create a DataTable to store the result of the query
Dim dataTable As New DataTable()
Try
' Execute the query and fill the DataTable with the result
Dim adapter As New SqlDataAdapter(command)
adapter.Fill(dataTable)
' Check if any rows were returned
If dataTable.Rows.Count > 0 Then
' Bind the DataTable to the DataGridView
DataGridView1.DataSource = dataTable
Else
' If no record is found, display a message
MessageBox.Show("No record found with the specified Name.", "Not Found", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
' Handle any exceptions here
MessageBox.Show("Error retrieving data: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Using
Else
' If TextBox2 is empty, display a message
MessageBox.Show("Please enter a Name.", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim query As String = "INSERT INTO patient (Name, DateofBirth, Gender, Address, PhoneNumber, MedicalHistory, EmergencyContactName, EmergencyContactPhone) VALUES ('" & TextBox2.Text.ToString.Trim() & "', '" & TextBox3.Text.ToString.Trim() & "', '" & TextBox4.Text.ToString.Trim() & "', '" & TextBox5.Text.ToString.Trim() & "', '" & TextBox6.Text.ToString.Trim() & "', '" & TextBox7.Text.ToString.Trim() & "', '" & TextBox8.Text.ToString.Trim() & "', '" & TextBox9.Text.ToString.Trim() & "')"
' Create a SqlCommand object with the query and connection
Using command As New SqlCommand(query, sqlcon)
' Add parameters to the query and set their values from TextBoxes
Try
' Open the connection and execute the query
'sqlcon.Open()
Dim rowsAffected As Integer = command.ExecuteNonQuery()
' Check if the query was successful
If rowsAffected > 0 Then
MessageBox.Show("Data inserted successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("No rows were affected. Data insertion failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
' Handle any exceptions here
MessageBox.Show("Error inserting data: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
' Close the connection
'sqlcon.Close()
End Try
End Using
End Sub
Private Sub TextBox10_TextChanged(sender As Object, e As EventArgs) Handles TextBox9.TextChanged
End Sub
Private Sub TextBox9_TextChanged(sender As Object, e As EventArgs) Handles TextBox8.TextChanged
End Sub
Private Sub Label9_Click(sender As Object, e As EventArgs) Handles Label9.Click
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim query As String = "Delete From Patient WHERE PatientID =" & TextBox1.Text() & ""
' Create a SqlCommand object with the query and connection
Using command As New SqlCommand(query, sqlcon)
Try
' Open the connection and execute the query
'sqlcon.Open()
Dim rowsAffected As Integer = command.ExecuteNonQuery()
' Check if the query was successful
If rowsAffected > 0 Then
MessageBox.Show("Data Delted successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("No rows were affected. Data Deletion failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
' Handle any exceptions here
MessageBox.Show("Error deleting data: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
' Close the connection
'sqlcon.Close()
End Try
End Using
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim query As String = "UPDATE patient SET "
' Get the patientID from TextBox1
Dim patientID As String = TextBox1.Text.Trim()
' Check if TextBox2 has a value
If TextBox2.Text.Trim() <> "" Then
query &= "Name = '" & TextBox2.Text.ToString.Trim() & "', "
End If
' Check if TextBox3 has a value
If TextBox3.Text.Trim() <> "" Then
query &= "DateofBirth = '" & TextBox3.Text.ToString.Trim() & "', "
End If
' Check if TextBox4 has a value
If TextBox4.Text.Trim() <> "" Then
query &= "Gender = '" & TextBox4.Text.ToString.Trim() & "', "
End If
' Check if TextBox5 has a value
If TextBox5.Text.Trim() <> "" Then
query &= "Address = '" & TextBox5.Text.ToString.Trim() & "', "
End If
' Check if TextBox6 has a value
If TextBox6.Text.Trim() <> "" Then
query &= "PhoneNumber = '" & TextBox6.Text.ToString.Trim() & "', "
End If
' Check if TextBox7 has a value
If TextBox7.Text.Trim() <> "" Then
query &= "MedicalHistory = '" & TextBox7.Text.ToString.Trim() & "', "
End If
' Check if TextBox8 has a value
If TextBox8.Text.Trim() <> "" Then
query &= "EmergencyContactName = '" & TextBox8.Text.ToString.Trim() & "', "
End If
' Check if TextBox9 has a value
If TextBox9.Text.Trim() <> "" Then
query &= "EmergencyContactPhone = '" & TextBox9.Text.ToString.Trim() & "', "
End If
' Remove the trailing comma and space if values have been added to the query
If query.EndsWith(", ") Then
query = query.Substring(0, query.Length - 2)
End If
' Append the WHERE clause to identify the record to be updated
query &= " WHERE PatientID = " & TextBox1.Text.Trim() & ""
' Create a SqlCommand object with the query and connection
Using command As New SqlCommand(query, sqlcon)
' Add parameters to the query and set their values from TextBoxes
'command.Parameters.AddWithValue("@PatientID", patientID)
'
'If TextBox2.Text.Trim() <> "" Then
' command.Parameters.AddWithValue("@Name", TextBox2.Text.Trim())
'End If
'
'If TextBox3.Text.Trim() <> "" Then
' command.Parameters.AddWithValue("@DateofBirth", TextBox3.Text.Trim())
'End If
'
'If TextBox4.Text.Trim() <> "" Then
' command.Parameters.AddWithValue("@Gender", TextBox4.Text.Trim())
'End If
'
'If TextBox5.Text.Trim() <> "" Then
' command.Parameters.AddWithValue("@Address", TextBox5.Text.Trim())
'End If
'
'If TextBox6.Text.Trim() <> "" Then
' command.Parameters.AddWithValue("@PhoneNumber", TextBox6.Text.Trim())
'End If
'
'If TextBox7.Text.Trim() <> "" Then
' command.Parameters.AddWithValue("@MedicalHistory", TextBox7.Text.Trim())
'End If
'
'If TextBox8.Text.Trim() <> "" Then
' command.Parameters.AddWithValue("@EmergencyContactName", TextBox8.Text.Trim())
'End If
'
'If TextBox9.Text.Trim() <> "" Then
' command.Parameters.AddWithValue("@EmergencyContactPhone", TextBox9.Text.Trim())
'End If
Try
' Open the connection and execute the query
'sqlcon.Open()
Dim rowsAffected As Integer = command.ExecuteNonQuery()
' Check if the query was successful
If rowsAffected > 0 Then
MessageBox.Show("Record updated successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("No record was updated. No matching record found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
' Handle any exceptions here
MessageBox.Show("Error updating record: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
' Close the connection
'sqlcon.Close()
End Try
End Using
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Me.Hide()
admin_page.Show()
End Sub
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
End Sub
End Class